bhupixb

Buggy AVL tree

Jan 5th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. struct node {
  5. node *l, *r;
  6. int val, height;
  7. node() : height(1) {}
  8. node (int x) : val(x), height(1) {}
  9. };
  10.  
  11. int height(node* tmp) {
  12. if (tmp == NULL) return 0;
  13. return tmp->height;
  14. }
  15.  
  16. int findDifference(node *tmp) {
  17. if (tmp == NULL) return 0;
  18. return height(tmp->l) - height(tmp->r);
  19. }
  20. /*
  21. node* lRotate(node *cur) {
  22. node *ri = cur->r;
  23. node *riri = ri->l;
  24. ri->l = cur;
  25. cur->r = riri;
  26. ri->height = 1 + max(height(ri->l), height(ri->r));
  27. cur->height = 1 + max(height(cur->l), height(cur->r));
  28. return ri;
  29. }
  30.  
  31. node *rRotate(node *cur) {
  32. node *st = cur->l;
  33. node *strt = st->r;
  34. st->r = cur;
  35. cur->l = strt;
  36. st->height = 1 + max(height(st->l), height(st->r));
  37. cur->height = 1 + max(height(cur->l), height(cur->r));
  38. return st;
  39. }*/
  40.  
  41.  
  42. node *rRotate(node *y)
  43. {
  44. node *x = y->l;
  45. node *T2 = x->r;
  46.  
  47. // Perform rotation
  48. x->r = y;
  49. y->l = T2;
  50.  
  51. // Update heights
  52. y->height = max(height(y->l),
  53. height(y->r)) + 1;
  54. x->height = max(height(x->l),
  55. height(x->r)) + 1;
  56.  
  57. // Return new root
  58. return x;
  59. }
  60.  
  61. // A utility function to l
  62. // rotate subtree rooted with x
  63. // See the diagram given above.
  64. node *lRotate(node *x)
  65. {
  66. node *y = x->r;
  67. node *T2 = y->l;
  68.  
  69. // Perform rotation
  70. y->l = x;
  71. x->r = T2;
  72.  
  73. // Update heights
  74. x->height = max(height(x->l),
  75. height(x->r)) + 1;
  76. y->height = max(height(y->l),
  77. height(y->r)) + 1;
  78.  
  79. // Return new root
  80. return y;
  81. }
  82.  
  83. node* insert(node *tmp, int key) {
  84. if (tmp == NULL) return new node(key);
  85. if (key < tmp->val) {
  86. tmp->l = insert(tmp->l,key);
  87. }
  88. else if (key > tmp->val) {
  89. tmp->r = insert(tmp->r, key);
  90. }
  91. else return tmp;
  92. // update height
  93. tmp->height = 1 + max(height(tmp->l), height(tmp->r));
  94. // return tmp;
  95. int balance = findDifference(tmp);
  96.  
  97. if (balance > 1) {
  98. // for LL
  99. if (key < tmp->l->val) {
  100. return rRotate(tmp);
  101. }
  102. }
  103. if (balance < -1) {
  104. if (key > tmp->r->val) {
  105. return lRotate(tmp);
  106. }
  107. }
  108. if (balance > 1) {
  109. if (key > tmp->l->val) {
  110. tmp->l = lRotate(tmp->l);
  111. return rRotate(tmp);
  112. }
  113. }
  114. if (balance < -1) {
  115. if (key < tmp->r->val){
  116. tmp->r = rRotate(tmp->r);
  117. return lRotate(tmp);
  118. }
  119. }
  120. return tmp;
  121. }
  122.  
  123. void dfs(node *n) {
  124. if (n != NULL) {
  125. dfs(n->l); cout << n->val << ' '; dfs(n->r);
  126. }
  127. }
  128.  
  129. int main() {
  130. int t;
  131. cin >> t;
  132. node *head = NULL;
  133. for (int i = 1; i <= 50; ++i) {
  134. int x;
  135. cin >> x;
  136. head = insert(head,x);
  137. }
  138. dfs(head);
  139. }
  140.  
  141. /*
  142. // I can't tell you what it really is,
  143. // I can only tell you what it feels like.
  144. #include "bits/stdc++.h"
  145. using namespace std;
  146. #define int long long
  147. const int N = 4e5 + 2;
  148.  
  149. signed main() {
  150. ios::sync_with_stdio(false);
  151. cin.tie(0);
  152. int n;
  153. cin >> n;
  154. set<int>st;
  155. while(n--) {
  156. int x;
  157. cin >> x;
  158. st.insert(x);
  159. }
  160. for (int x:st)cout << x << ' ';
  161.  
  162.  
  163. return 0;
  164. }
  165. // 1). Try forming/rearranging the equations if any
  166. // 2). If problem asks to count, try counting in reverse(it can be easier sometimes)
  167. */
Add Comment
Please, Sign In to add comment