Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. typedef struct Avl{
  5. int value;
  6. int height;
  7. int nChild;
  8. struct Avl* left;
  9. struct Avl* right;
  10. }Avl;
  11. Avl* AvlNew(Avl* avl,int value);
  12. Avl* AvlInsert(Avl* avl, int value);
  13. Avl* AvlDelete(Avl* avl,int value);
  14. void KLess(Avl* avl,int k);
  15. void KIndex(Avl* avl,int k);
  16.  
  17. static void RecKIndex(Avl* avl,int k,int* s){
  18. if(avl->left == NULL && *s + 1 == k)
  19. printf("%d" , avl->value);
  20. else{
  21. if(avl->left != NULL &&(avl->left->nChild + 2 + *s) == k)
  22. printf("%d ", avl->value);
  23. if(avl->left != NULL && avl->left->nChild + 2 < k){
  24. *s = avl->left->nChild + 2;
  25. if(avl->right != NULL)
  26. RecKIndex(avl->right,k,s);
  27. }
  28. else
  29. if(avl->left != NULL)
  30. RecKIndex(avl->left,k,s);
  31. }
  32. }
  33. void KIndex(Avl* avl, int k){
  34. if(avl == NULL || avl->nChild < k - 1){
  35. printf("invalid \n");
  36. }
  37. int s = 0;
  38. RecKIndex(avl,k,&s);
  39. }
  40. static void RecKLess(Avl* avl, int k, int* sum){
  41. if(avl == NULL)
  42. return;
  43. if(k > avl->value){
  44. if(avl->left != NULL)
  45. *sum += avl->left->nChild + 1;
  46. (*sum)++;
  47. RecKLess(avl->right,k,sum);
  48. }
  49. if(k <= avl->value){
  50. RecKLess(avl->left,k,sum);
  51. }
  52. }
  53. void KLess(Avl* avl,int k){
  54. int sum = 0;
  55. RecKLess(avl,k,&sum);
  56. printf("%d \n",sum);
  57. }
  58. Avl* AvlNew(Avl* avl, int value){
  59. avl = (Avl*)malloc(sizeof(Avl));
  60. avl->value = value ;
  61. avl->left = NULL;
  62. avl->nChild = 0;
  63. avl->right = NULL;
  64. avl->height = 1;
  65. return avl;
  66. }
  67. static int height(Avl* avl){
  68. return avl == NULL ? 0 : avl->height;
  69. }
  70. static int max(int avl1, int avl2){
  71. return avl1 > avl2 ? avl1 : avl2 ;
  72. }
  73. static int nChildCur(Avl* avl){
  74. int l = 0;
  75. int r = 0;
  76. if(avl ->left != NULL)
  77. l = avl->left->nChild + 1;
  78. if(avl -> right != NULL)
  79. r = avl->right ->nChild +1;
  80. return l+r;
  81. }
  82.  
  83. static Avl* rightRotate(Avl* avl){
  84. Avl* left = avl->left;
  85. Avl* Lright = left->right;
  86. left->right = avl;
  87. avl->left = Lright;
  88. avl->height = max(height(avl->left),height(avl->right))+1;
  89. left->height = max(height(left->left),height(left->right))+1;
  90. avl->nChild = nChildCur(avl);
  91. left->nChild = nChildCur(left);
  92. avl = left;
  93. return avl;
  94. }
  95.  
  96. static Avl* leftRotate(Avl* avl){
  97. Avl* right = avl->right;
  98. Avl* Rleft = right->left;
  99. right->left = avl;
  100. avl->right = Rleft;
  101. avl->height = max(height(avl->left),(height(avl->right)))+1;
  102. right->height = max(height(right->left),height(right->right))+1;
  103. avl->nChild = nChildCur(avl);
  104. right->nChild = nChildCur(right);
  105. avl = right;
  106. return avl;
  107. }
  108. static Avl* minV(Avl* avl){
  109. while(avl->left != NULL)
  110. avl = avl->left;
  111. return avl;
  112.  
  113. }
  114. static Avl* Balance(Avl* avl){
  115. avl->height = max(height(avl->left), height(avl->right)) + 1;
  116. int balance = height(avl->left) - height(avl->right);
  117. int Lbalance;
  118. int Rbalance;
  119. if(avl->left == NULL)
  120. Lbalance = 0;
  121. else
  122. Lbalance = height(avl->left->left) - height(avl->left->right);
  123. if(avl->right == NULL)
  124. Rbalance = 0;
  125. else
  126. Rbalance = height(avl->right->left) - height(avl->right->left);
  127. if (balance > 1 && Lbalance >= 0){
  128. avl = rightRotate(avl);
  129. return avl;
  130. }
  131. if (balance > 1 && Lbalance < 0)
  132. {
  133. avl -> left = leftRotate(avl->left);
  134. avl = rightRotate(avl);
  135. return avl ;
  136. }
  137. if (balance < -1 && Rbalance <= 0){
  138. avl = leftRotate(avl);
  139. return avl;
  140. }
  141. if (balance < -1 && Rbalance > 0)
  142. {
  143. avl->right= rightRotate(avl->right);
  144. avl= leftRotate(avl);
  145. return avl;
  146. }
  147. return avl;
  148. }
  149. Avl* AvlDelete(Avl* avl, int value){
  150. if(avl == NULL)
  151. return NULL;
  152. if(value > avl->value)
  153. avl-> right = AvlDelete(avl->right,value);
  154. else
  155. if(value < avl->value)
  156. avl->left = AvlDelete(avl->left,value);
  157. else
  158. if(avl->left == NULL & avl->right == NULL){
  159. free(avl);
  160. avl = NULL;
  161. }
  162. else
  163. if(avl->left == NULL && avl->right != NULL){
  164. Avl* tmp = avl->right;
  165. avl->value = avl->right->value;
  166. avl->right = NULL;
  167. free(tmp);
  168. }
  169. else
  170. if(avl->left != NULL && avl->right == NULL){
  171. Avl* tmp = avl->left;
  172. avl-> value = avl->left->value;
  173. avl->left = NULL;
  174. free(tmp);
  175. }
  176. else
  177. if(avl->left != NULL && avl->right != NULL){
  178. Avl* tmp = minV(avl->right);
  179. avl->value = tmp->value;
  180. AvlDelete(avl->right,tmp->value);
  181. }
  182. if(avl == NULL)
  183. return NULL;
  184. avl = Balance(avl);
  185. avl->nChild = nChildCur(avl);
  186. return avl;
  187. }
  188.  
  189. Avl* AvlInsert(Avl* avl, int value){
  190. if(avl == NULL){
  191. avl = AvlNew(avl,value);
  192. return avl;
  193. }
  194. if (value < avl->value)
  195. avl->left=AvlInsert(avl->left,value);
  196. if(value > avl-> value)
  197. avl->right = AvlInsert(avl->right, value);
  198. if(value == avl->value)
  199. return avl;
  200. avl = Balance(avl);
  201. avl->nChild = nChildCur(avl);
  202. return avl;
  203. }
  204.  
  205. void preOrder(Avl* avl){
  206. if(avl != NULL){
  207. printf("%d ", avl->value);
  208. preOrder(avl->right);
  209. preOrder(avl->left);
  210. }
  211.  
  212. }
  213. int main(){
  214. Avl* avl = NULL;
  215. int num;
  216. scanf("%d", &num);
  217. int i;
  218.  
  219. for(i=0; i< num; i++){
  220. char type;
  221. char space;
  222. int number;
  223. scanf("%c",&space);
  224. scanf("%c %d",&type,&number);
  225. switch(type){
  226. case 'I':{
  227. printf("I qna\n");
  228. avl = AvlInsert(avl,number);
  229. break;
  230. }
  231. case 'D':{
  232. printf("D qna\n");
  233. avl = AvlDelete(avl,number);
  234. break;
  235. }
  236. case 'K':{
  237. printf("K qna\n");
  238. // KIndex(avl,number);
  239. printf(" %d -- " , avl->left->height - avl->right->height);
  240. break;
  241. }
  242. case 'C':{
  243. printf("C qna\n");
  244. KLess(avl,number);
  245. break;
  246. }
  247. default:{
  248. printf("Error Type \n");
  249. break;
  250. }
  251. }
  252. }
  253. preOrder(avl);
  254. return 0;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement