Posted By

dafilipaj on 01/06/11


Tagged

tree binary pokazivac binarno stablo


Versions (?)

bstablo_pokazivac.h


 / Published in: C++
 

  1. struct element {
  2. int label;
  3. element *left,*right;
  4. };
  5. typedef element *cvor;
  6. typedef element bt;
  7.  
  8. bt* InitB(int x, bt* T){
  9. T = new element;
  10. T->label = x;
  11. T->left = NULL;
  12. T->right = NULL;
  13. return T;
  14. }//initB
  15.  
  16. void CreateLeftB(int x, cvor n, bt* T){
  17. if(n->left) {
  18. cout << "Greska" << endl << endl;
  19. return;
  20. }
  21. cvor novi = new element;
  22. n->left = novi;
  23. novi->label = x;
  24. novi->left = NULL;
  25. novi->right = NULL;
  26. }//CreateLeftB
  27.  
  28. void CreateRightB(int x, cvor n, bt* T){
  29. if(n->right) {
  30. cout << "Greska" << endl << endl;
  31. return;
  32. }
  33. cvor novi = new element;
  34. n->right = novi;
  35. novi->label = x;
  36. novi->left = NULL;
  37. novi->right = NULL;
  38. }//createRightB
  39.  
  40. cvor ParentB(cvor n, bt* T){
  41. if (n == T) return NULL;
  42. cvor roditelj = NULL;
  43. if (T->left){
  44. if (T->left == n) return T;
  45. else roditelj = ParentB(n, T->left);
  46. }
  47. if(T->right){
  48. if (T->right == n) return T;
  49. else roditelj = ParentB(n, T->right);
  50. }
  51. return roditelj;
  52. }//parentB
  53.  
  54. int LabelB(cvor n, bt* T){
  55. return n->label;
  56. }//labelB
  57.  
  58. void ChangeLabelB(int x, cvor n, bt* T){
  59. if(!n) return;
  60. n->label = x;
  61. }//changeLabelB
  62.  
  63. cvor LeftChildB(cvor n, bt* T){
  64. if(!n || !n->left) return NULL;
  65. return n->left;
  66. }//leftCildB
  67.  
  68. cvor RightChildB(cvor n, bt* T){
  69. if(!n || !n->right) return NULL;
  70. return n->right;
  71. }//rightCildB
  72.  
  73. cvor RootB(bt* T){
  74. if(!T) return NULL;
  75. return T;
  76. }//rootB
  77.  
  78. void DeleteB(cvor n, bt* T){
  79. static bool jednom = false;
  80. if(!jednom) {
  81. cvor roditelj = ParentB(n, T);
  82. if(roditelj->left == n) roditelj->left = NULL;
  83. else roditelj->right = NULL;
  84. jednom = true;
  85. }
  86.  
  87. if(n->left) DeleteB(n->left, T);
  88. if(n->right) DeleteB(n->right, T);
  89. delete n;
  90. }//deleteB

Report this snippet  

You need to login to post a comment.