Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <algorithm>
  4. using namespace std;
  5. struct AVL
  6. {
  7. int d,h;
  8. AVL *L,*R;
  9. AVL(int _d=0)
  10. {
  11. d=_d,h=1,L=R=NULL;
  12. }
  13. }*root;
  14. inline int SIZE(AVL *tr)
  15. {
  16. return tr?tr->h:0;
  17. }
  18. inline void up(AVL *tr)
  19. {
  20. tr->h=max(SIZE(tr->L),SIZE(tr->R))+1;
  21. }
  22. inline AVL* RRotate(AVL *tr)
  23. {
  24. AVL *x=tr,*y=tr->L;
  25. x->L=y->R;y->R=x;
  26. up(x);up(y);
  27. return y;
  28. }
  29. inline AVL* LRotate(AVL *tr)
  30. {
  31. AVL *x=tr,*y=tr->R;
  32. x->R=y->L;y->L=x;
  33. up(x);up(y);
  34. return y;
  35. }
  36. inline AVL* RLRotate(AVL *tr)
  37. {
  38. tr->R=RRotate(tr->R);
  39. return LRotate(tr);
  40. }
  41. inline AVL* LRRotate(AVL *tr)
  42. {
  43. tr->L=LRotate(tr->L);
  44. return RRotate(tr);
  45. }
  46. void inorder(AVL* tr)
  47. {
  48. if(!tr)return;
  49. inorder(tr->L);
  50. printf("%d ",tr->d);
  51. inorder(tr->R);
  52. }
  53. void INORDER(AVL *tr)
  54. {
  55. inorder(tr);
  56. printf("size: %d\n",SIZE(tr));
  57. }
  58. AVL* insertnode(AVL *tr,int val)
  59. {
  60. if(tr==NULL)
  61. {
  62. tr=new AVL(val);
  63. return tr;
  64. }
  65. if(tr->d==val)
  66. return tr;
  67. if(val<tr->d)
  68. {
  69. tr->L=insertnode(tr->L,val);
  70. if(SIZE(tr->L)-SIZE(tr->R)>1)
  71. {
  72. if(val<tr->L->d)
  73. tr=RRotate(tr);
  74. else
  75. tr=LRRotate(tr);
  76. }
  77. }
  78. else if(tr->d<val)
  79. {
  80. tr->R=insertnode(tr->R,val);
  81. if(SIZE(tr->R)-SIZE(tr->L)>1)
  82. {
  83. if(val>tr->R->d)
  84. tr=LRotate(tr);
  85. else
  86. tr=RLRotate(tr);
  87. }
  88. }
  89. up(tr);
  90. return tr;
  91. }
  92. int getmax(AVL* tr)
  93. {
  94. int res;
  95. for(res=tr->d;tr;res=max(res,tr->d),tr=tr->R);
  96. return res;
  97. }
  98. int getmin(AVL* tr)
  99. {
  100. int res;
  101. for(res=tr->d;tr;res=min(res,tr->d),tr=tr->L);
  102. return res;
  103. }
  104. AVL* deletenode(AVL* tr,int val)
  105. {
  106. if(!tr)return tr;
  107. if(tr->d==val)
  108. {
  109. if(tr->L&&tr->R)
  110. {
  111. if(SIZE(tr->L)>SIZE(tr->R))
  112. {
  113. tr->d=getmax(tr->L);
  114. tr->L=deletenode(tr->L,tr->d);
  115. }
  116. else
  117. {
  118. tr->d=getmin(tr->R);
  119. tr->R=deletenode(tr->R,tr->d);
  120. }
  121. up(tr);
  122. }
  123. else
  124. {
  125. tr=tr->L?tr->L:tr->R;
  126. }
  127. }
  128. else if(val<tr->d)
  129. {
  130. tr->L=deletenode(tr->L,val);
  131. if(SIZE(tr->R)-SIZE(tr->L)>1)
  132. {
  133. if(SIZE(tr->R->L)>SIZE(tr->R->R))
  134. tr=RLRotate(tr);
  135. else
  136. tr=LRotate(tr);
  137. }
  138. up(tr);
  139. }
  140. else if(tr->d<val)
  141. {
  142. tr->R=deletenode(tr->R,val);
  143. if(SIZE(tr->L)-SIZE(tr->R)>1)
  144. {
  145. if(SIZE(tr->L->R)<SIZE(tr->L->L))
  146. tr=LRotate(tr);
  147. else
  148. tr=RRotate(tr);
  149. }
  150. up(tr);
  151. }
  152. return tr;
  153. }
  154. int main()
  155. {
  156. root=NULL;
  157. srand(time(0));
  158. for(int i=0;i<20;i++)
  159. root=insertnode(root,i);
  160. INORDER(root);
  161. root=deletenode(root,16);
  162. puts("inorder");
  163. INORDER(root);
  164. for(int i=0;i<20;i++)
  165. {
  166. printf("deletenode: %d\n",i);
  167. root=deletenode(root,i);
  168. INORDER(root);
  169. }
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement