Guest User

Untitled

a guest
Jul 20th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  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) return NULL;
  15. node parent = NULL;
  16. if (T->left) {
  17. if (T->left == n) return T;
  18. else parent = ParentB(n, T->left);
  19. }
  20. if(T->right) {
  21. if (T->right == n) return T;
  22. else parent = ParentB(n, T->right);
  23. }
  24. return parent;
  25. }
  26.  
  27. node LeftChildB(node n, bt* T) {
  28. if (!n) {
  29. cout << "Cvor ne postoji!" << endl;
  30. return NULL;
  31. }
  32. if (!n->left) {
  33. cout << "Cvor nema dijete!" << endl;
  34. return NULL;
  35. }
  36. return n->left;
  37. }
  38.  
  39. node RightChildB(node n, bt* T) {
  40. if (!n) {
  41. cout << "Cvor ne postoji!" << endl;
  42. return NULL;
  43. }
  44. if (!n->right) {
  45. cout << "Cvor nema dijete!" << endl;
  46. return NULL;
  47. }
  48. return n->right;
  49. }
  50.  
  51. int LabelB(node n, bt* T) {
  52. return n->label;
  53. }
  54.  
  55. void ChangeLabelB(int x, node n, bt* T) {
  56. if(!n) {
  57. cout << "Taj element stabla ne postoji!" << endl;
  58. return;
  59. }
  60. n->label = x;
  61. }
  62.  
  63. node RootB(bt* T) {
  64. if(!T) return NULL;
  65. return T;
  66. }
  67.  
  68. void CreateLeftB(int x, node n, bt* T) {
  69. if(n->left) {
  70. cout << "Greska!" << endl << endl;
  71. return;
  72. }
  73. node novi = new element;
  74. n->left = novi;
  75. novi->label = x;
  76. novi->left = NULL;
  77. novi->right = NULL;
  78. }
  79.  
  80. void CreateRightB(int x, node n, bt* T) {
  81. if(n->right) {
  82. cout << "Greska!" << endl << endl;
  83. return;
  84. }
  85. node novi = new element;
  86. n->right = novi;
  87. novi->label = x;
  88. novi->left = NULL;
  89. novi->right = NULL;
  90. }
  91.  
  92. void DeleteB1(node n, bt* T) {
  93. if(n->left) DeleteB1(n->left, T);
  94. if(n->right) DeleteB1(n->right, T);
  95. delete n;
  96. }
  97.  
  98. void DeleteB(node n, bt* T) {
  99. node roditelj = ParentB(n, T);
  100.  
  101. if(n->left) DeleteB1(n->left, T);
  102. if(n->right) DeleteB1(n->right, T);
  103.  
  104. if(roditelj->left == n) roditelj->left = NULL;
  105. else roditelj->right = NULL;
  106.  
  107. delete n;
  108. }
  109.  
  110. bt* InitB(int x, bt* T) {
  111. T = new element;
  112. T->label = x;
  113. T->left = NULL;
  114. T->right = NULL;
  115. return T;
  116. }
Add Comment
Please, Sign In to add comment