Posted By

majugurci on 01/14/11


Tagged


Versions (?)

binarno stablo pokazivac


 / Published in: C++
 

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

Report this snippet  

You need to login to post a comment.