Posted By

sesveteboy on 01/06/11


Tagged

Strukture podataka


Versions (?)

Implementacija binarnog stabla pomocu pokazivaca


 / Published in: C++
 

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

Report this snippet  

You need to login to post a comment.