Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int a[2][10000], a_size;
  5.  
  6. struct tree {
  7. int key, value, height;
  8. struct tree *right, *left;
  9. }tree;
  10.  
  11. int height(struct tree *h) {
  12. if (h) {
  13. return h->height;
  14. }
  15. else {
  16. return 0;
  17. }
  18. }
  19.  
  20. int maximum(int a, int b) {
  21. if (a > b) {
  22. return a;
  23. }
  24. else {
  25. return b;
  26. }
  27. }
  28.  
  29. struct tree *single_in_left(struct tree *k) {
  30. struct tree *temp;
  31. temp = k->left;
  32. k->left = temp->right;
  33. temp->right = k;
  34. k->height = maximum(height(k->left), height(k->right)) + 1;
  35. temp->height = maximum(height(temp->left), k->height) + 1;
  36. return temp;
  37. }
  38.  
  39. struct tree *single_in_right(struct tree *k1) {
  40. struct tree *temp;
  41. temp = k1->right;
  42. k1->right = temp->left;
  43. temp->left = k1;
  44. k1->height = maximum(height(k1->left), height(k1->right)) + 1;
  45. temp->height = maximum(height(temp->right), k1->height) + 1;
  46. return temp;
  47. }
  48.  
  49. struct tree *double_in_left(struct tree *k2) {
  50. k2->left = single_in_right(k2->left);
  51. k2 = single_in_left(k2);
  52. return k2;
  53. }
  54.  
  55. struct tree *double_in_right(struct tree *k3) {
  56. k3->right = single_in_left(k3->right);
  57. k3 = single_in_right(k3);
  58. return k3;
  59. }
  60.  
  61. struct tree *ideal_balance(struct tree *head5, int key) {
  62. if ((!head5->left) && (!head5->right)) {
  63. head5->height = 1;
  64. }
  65. else if (!head5->right) {
  66. head5->height = head5->left->height + 1;
  67. }
  68. else if (!head5->left) {
  69. head5->height = head5->right->height + 1;
  70. }
  71. else {
  72. if (head5->left->height > head5->right->height) {
  73. head5->height = head5->left->height + 1;
  74. }
  75. else {
  76. head5->height = head5->right->height + 1;
  77. }
  78. }
  79. if ((height(head5->left) - height(head5->right)) == 2) {
  80. if (key < head5->left->key) {
  81. head5 = single_in_left(head5);
  82. }
  83. else {
  84. head5 = double_in_left(head5);
  85. }
  86. }
  87. if ((height(head5->right) - height(head5->left)) == 2) {
  88. if (key > head5->left->key) {
  89. head5 = single_in_right(head5);
  90. }
  91. else {
  92. head5 = double_in_right(head5);
  93. }
  94. }
  95. return head5;
  96. }
  97.  
  98. struct tree *add(struct tree *head4, int value, int key) {
  99. if (head4 == NULL) {
  100. head4 = malloc(sizeof(struct tree *));
  101. head4->right = NULL;
  102. head4->left = NULL;
  103. head4->key = key;
  104. head4->value = value;
  105. head4->height = 1;
  106. }
  107. else if(head4->key>key){
  108. head4->left = add(head4->left, value, key);
  109. ideal_balance(head4, key);
  110. }
  111. else if (head4->key < key) {
  112. head4->right = add(head4->right, value, key);
  113. ideal_balance(head4, key);
  114. }
  115. else {
  116. head4->value = value;
  117. head4->height = maximum(height(head4->left), height(head4->right)) + 1;
  118. }
  119. return head4;
  120. }
  121.  
  122. void search(struct tree* head3, int key) {
  123. if (!head3) {
  124. return NULL;
  125. }
  126. if (head3->key == key) {
  127. a[0][a_size] = key;
  128. a[1][a_size] = head3->value;
  129. a_size++;
  130. }
  131. else if (head3->key > key) {
  132. search(head3->left, key);
  133. }
  134. else {
  135. search(head3->right, key);
  136. }
  137. }
  138.  
  139. struct tree *min_in_right(struct tree *t) {
  140. if (!t->left) {
  141. return t;
  142. }
  143. t->left = min_in_right(t->left);
  144. }
  145.  
  146. struct tree *del_min_in_right(struct tree *head2) {
  147. if (!head2) {
  148. return NULL;
  149. }
  150. else {
  151. if (!head2->left) {
  152. return head2->right;
  153. }
  154. head2->left = del_min_in_right(head2->left);
  155. }
  156. }
  157.  
  158. struct tree *del(struct tree* head1, int key) {
  159. if (!head1) {
  160. return NULL;
  161. }
  162. else if (key < head1->key) {
  163. head1->left = del(head1->left, key);
  164.  
  165. }
  166. else if (key > head1->key) {
  167. head1->right = del(head1->right, key);
  168. }
  169. else {
  170. struct tree *min;
  171. if (!head1->right) {
  172. return head1->left;
  173. }
  174. min = min_in_right(head1->right);
  175. min->right = del_min_in_right(min->right);
  176. min->left = head1->left;
  177. ideal_balance(min, key);
  178. return min;
  179. }
  180. ideal_balance(head1, head1->key);
  181. return head1;
  182. }
  183.  
  184. void delete_all(struct tree *head) {
  185. if (head) {
  186. delete_all(head->left);
  187. delete_all(head->right);
  188. free(head);
  189. }
  190. return NULL;
  191. }
  192.  
  193. int main(void) {
  194. char op='F';
  195. int key=0, val=0;
  196. struct tree *head=NULL;
  197. scanf("%c", &op);
  198. while (op != 'F') {
  199. if (op == 'A') {
  200. scanf("%d %d", &key, &val);
  201. head = add(head, val, key);
  202. }
  203. else if (op == 'S') {
  204. scanf("%d", &key);
  205. search(head, key);
  206. }
  207. else if (op == 'D') {
  208. scanf("%d", &key);
  209. head = del(head, key);
  210. }
  211. scanf("%c", &op);
  212. }
  213.  
  214. for (int i = 0; i < a_size; i++) {
  215. printf("%d %d\n", a[0][i], a[1][i]);
  216. }
  217. delete_all(head);
  218. return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement