Posted By

elvis_popovic on 01/19/15


Tagged

list pointer linked


Versions (?)

Binary tree, implementation using pointers and linked lists


 / Published in: C++
 

Here is pointer implementation, not tested finally.

  1. #ifndef __POKAZIVAC__
  2. #define __POKAZIVAC__
  3. #include <iostream>
  4. namespace stablo_pokazivac{
  5.  
  6.  
  7. struct element{
  8. float label;
  9. struct element *left, *right;
  10. };
  11.  
  12. element *LeftChildB(element *n, element *T){
  13. return n->left;
  14. }
  15.  
  16. element *RightChildB(element *n, element *T){
  17. return n->right;
  18. }
  19.  
  20. float LabelB(element *n, element *T){
  21. return n->label;
  22. }
  23.  
  24. void ChangeLabelB(float x, element *n, element *T){
  25. n->label=x;
  26. }
  27.  
  28. element *RootB(element *T)
  29. {
  30. return T;
  31. }
  32.  
  33. bool CreateLeftB(float x, element *n, element *T){
  34. if(n->left) return false;
  35. element *novi=new element;
  36. novi->left=NULL;
  37. novi->right=NULL;
  38. novi->label=x;
  39. n->left=novi;
  40. }
  41. bool CreateRightB(float x, element *n, element *T){
  42. if(n->right) return false;
  43. element *novi=new element;
  44. novi->left=NULL;
  45. novi->right=NULL;
  46. novi->label=x;
  47. n->right=novi;
  48. }
  49.  
  50. void DeleteB(element *n, element *T)
  51. {
  52. if(n->left) DeleteB(n->left,T);
  53. if(n->right) DeleteB(n->right,T);
  54. delete n;
  55. }
  56.  
  57. element *ParentB(element *n, element *T)
  58. {
  59. if(T->left==n||T->right==n) return NULL;
  60. if(T->left->left!=n&&T->left->right!=n) ParentB(n,T->left);
  61. else return T->left;
  62. if(T->right->left!=n&&T->right->right!=n) ParentB(n,T->right);
  63. else return T->right;
  64.  
  65. };
  66.  
  67. };
  68. #endif

Report this snippet  

You need to login to post a comment.