Advertisement
Ne-Biolog

Untitled

Apr 10th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6. int data;
  7. unsigned char height;
  8. Node *Left, *Right;
  9. Node(int data) {
  10. this->data = data;
  11. Left = Right = nullptr;
  12. height = 1;
  13. }
  14. } *Root;
  15.  
  16. unsigned char get_height(Node* temp) {
  17. return temp ? temp->height : 0;
  18. }
  19.  
  20. int b_factor(Node* temp) {
  21. return get_height(temp->Right) - get_height(temp->Left);
  22. }
  23.  
  24. void fix_height(Node* temp) {
  25. unsigned char h_l = get_height(temp->Left);
  26. unsigned char h_r = get_height(temp->Right);
  27. temp->height = (h_l > h_r ? h_l : h_r) + 1;
  28. }
  29.  
  30. Node* rotate_right(Node* temp) {
  31. Node *x = temp->Left;
  32. temp->Left = x->Right;
  33. x->Right = temp;
  34. fix_height(temp);
  35. fix_height(x);
  36. return x;
  37. }
  38.  
  39. Node* rotate_left(Node* temp) {
  40. Node *x = temp->Right;
  41. temp->Right = x->Left;
  42. x->Left = temp;
  43. fix_height(temp);
  44. fix_height(x);
  45. return x;
  46. }
  47.  
  48. Node* balance(Node* temp) {
  49. fix_height(temp);
  50. if(b_factor(temp) == 2) {
  51. if(b_factor(temp->Right) < 0) {
  52. temp->Right = rotate_right(temp->Right);
  53. return rotate_left(temp);
  54. }
  55. } else if(b_factor(temp) == -2) {
  56. if(b_factor(temp->Left) < 0) {
  57. temp->Left = rotate_right(temp->Left);
  58. return rotate_right(temp);
  59. }
  60. }
  61. return temp;
  62. }
  63.  
  64. Node* insert(Node* temp, int key) {
  65. if(!temp) {
  66. return new Node(key);
  67. }
  68. if(key > temp->data) {
  69. temp->Right = insert(temp->Right, key);
  70. } else {
  71. temp->Left = insert(temp->Left, key);
  72. }
  73. return balance(temp);
  74. }
  75.  
  76. Node* find_min(Node* temp) {
  77. return temp->Left ? find_min(temp->Left) : temp;
  78. }
  79.  
  80. Node* remove_min(Node* temp) {
  81. if(temp->Left == nullptr) {
  82. return temp->Right;
  83. }
  84. temp->Left = remove_min(temp->Left);
  85. return balance(temp);
  86. }
  87.  
  88. Node* remove(Node* temp, int key) {
  89. if(!temp) {
  90. return 0;
  91. }
  92. if(key > temp->data) {
  93. temp->Right = remove(temp->Right, key);
  94. } else if(key < temp->data){
  95. temp->Left = remove(temp->Left, key);
  96. } else {
  97. Node* l = temp->Left;
  98. Node* r = temp->Right;
  99. delete temp;
  100. if(!r) {
  101. return l;
  102. }
  103. Node* _min = find_min(r);
  104. _min->Right = remove_min(r);
  105. _min->Left = l;
  106. return balance(_min);
  107. }
  108. return balance(temp);
  109. }
  110.  
  111.  
  112. void preOrderTravers(Node* root) {
  113. if (root) {
  114. printf("%d ", root->data);
  115. preOrderTravers(root->Left);
  116. preOrderTravers(root->Right);
  117. }
  118. }
  119. void postOrderTravers(Node* root) {
  120. if (root) {
  121. postOrderTravers(root->Left);
  122. postOrderTravers(root->Right);
  123. printf("%d ", root->data);
  124. }
  125. }
  126.  
  127.  
  128. void show(Node* temp, int dep) {
  129. if(temp) {
  130. show(temp->Right, dep + 1);
  131. for(int i = 0; i < dep; ++i) {
  132. cout << ' ';
  133. }
  134. cout << temp->data << endl;
  135. show(temp->Left, dep + 1);
  136. }
  137. }
  138.  
  139. int main() {
  140. Root = insert(Root, 10);
  141. Root = insert(Root, 20);
  142. Root = insert(Root, 50);
  143. Root = insert(Root, 5);
  144.  
  145. //show(Root, 0);
  146.  
  147. //Root = remove(Root, 10);
  148.  
  149. //show(Root, 0);
  150. preOrderTravers(Root);
  151. cout << endl;
  152. postOrderTravers(Root);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement