Advertisement
ElooEminem

Untitled

Apr 3rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4.  
  5. struct node{
  6. int val;
  7. int bf;
  8. node *left;
  9. node *right;
  10. node *up;
  11. };
  12.  
  13. void lr(struct node *root,struct node *A){
  14. struct node *B=A->left;
  15. struct node *C=B->right;
  16. struct node *D=A->up;
  17. B->right=C->left;
  18. if(B->right) B->right->up=B;
  19. A->left=C->right;
  20. if(A->left) A->left->up=A;
  21. C->right=A;
  22. C->left=B;
  23. A->up=B->up=C;
  24. C->up=D;
  25. if(D){
  26. if(D->left==A) D->left=C;
  27. else D->right=C;
  28. }
  29. else root=C;
  30. if(C->bf==1) A->bf=-1;
  31. else A->bf=0;
  32. if(C->bf==-1) B->bf=1;
  33. else B->bf=0;
  34. C->bf=0;
  35. return;
  36. }
  37.  
  38. void ll(struct node *root,struct node *A){
  39. struct node *B=A->left;
  40. struct node *C=A->up;
  41. A->left = B->right;
  42. if(A->left) A->left->up=A;
  43. B->right=A;
  44. B->up=C;
  45. A->up=B;
  46. if(C){
  47. if(C->left==A) C->left=B;
  48. else C->right=B;
  49. }
  50. else root=B;
  51. if(B->bf == 1) A->bf=B->bf=0;
  52. else{
  53. A->bf=1;
  54. B->bf=-1;
  55. }
  56. return;
  57. }
  58.  
  59. void rl(struct node *root,struct node *A){
  60. struct node *B=A->right;
  61. struct node *C=B->left;
  62. struct node *D=A->up;
  63. B->left=C->right;
  64. if(B->left) B->left->up=B;
  65. A->right=C->left;
  66. if(A->right) A->right->up=A;
  67. C->left=A;
  68. C->right=B;
  69. A->up=B->up=C;
  70. C->up=D;
  71. if(D){
  72. if(D->left==A) D->left=C;
  73. else D->right=C;
  74. }
  75. else root=C;
  76. if(C->bf==-1) A->bf=1;
  77. else A->bf=0;
  78. if(C->bf==1) B->bf=-1;
  79. else B->bf=0;
  80. C->bf = 0;
  81. return;
  82. }
  83.  
  84. void rr(struct node *root,struct node *A){
  85. struct node *B=A->right;
  86. struct node *C = A->up;
  87. A->right=B->left;
  88. if(A->right) A->right->up=A;
  89. B->left=A;
  90. B->up=C;
  91. A->up=B;
  92. if(C){
  93. if(C->left==A) C->left=B;
  94. else C->right=B;
  95. }
  96. else root=B;
  97. if(B->bf==-1) A->bf=B->bf=0;
  98. else{
  99. A->bf=-1;
  100. B->bf=1;
  101. }
  102. return;
  103. }
  104.  
  105. void node_add(struct node *tree,int b){
  106. struct node *add=new struct node;
  107. struct node *tmp;
  108. struct node *root=tree;
  109. bool t;
  110. if(tree==NULL){
  111. tree=add;
  112. tree->left=NULL;
  113. tree->right=NULL;
  114. tree->up=NULL;
  115. tree->val=b;
  116. return;
  117. }
  118. while(true){
  119. if(b>=tree->val){
  120. if(tree->right==NULL){
  121. tree->right=add;
  122. add->val=b;
  123. add->left=NULL;
  124. add->right=NULL;
  125. add->bf=0;
  126. add->up=tree;
  127. break;
  128. }
  129. else tree=tree->right;
  130. }
  131. else{
  132. if(tree->left==NULL){
  133. tree->left=add;
  134. add->val=b;
  135. add->left=NULL;
  136. add->right=NULL;
  137. add->bf=0;
  138. add->up=tree;
  139. break;
  140. }
  141. else tree=tree->left;
  142. }
  143. }
  144. if(tree->bf) tree->bf=0;
  145. else{
  146. if(tree->left==add) tree->bf=1;
  147. else tree->bf=-1;
  148. tmp=tree->up;
  149. t=false;
  150. while(tmp){
  151. if(tmp->bf){
  152. t=true;
  153. break;
  154. }
  155. if(tmp->left==tree) tmp->bf=1;
  156. else tmp->bf=-1;
  157.  
  158. tree=tmp;
  159. tmp=tmp->up;
  160. }
  161. if(t){
  162. if(tmp->bf==1){
  163. if(tmp->right==tree) tmp->bf = 0;
  164. else if(tree->bf==-1) lr(root,tmp);
  165. else ll(root,tmp);
  166. }
  167. else{
  168. if(tmp->left==tree) tmp->bf=0;
  169. else if(tree->bf==1) rl(root,tmp);
  170. else rr(root,tmp);
  171. }
  172. }
  173. }
  174. return;
  175. }
  176.  
  177. int main(){
  178. struct node *root=new struct node;
  179. root=NULL;
  180. node_add(root,2);
  181. node_add(root,3);
  182. node_add(root,1);
  183. node_add(root,5);
  184. node_add(root,10);
  185. node_add(root,9);
  186. node_add(root,8);
  187. node_add(root,5);
  188.  
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement