Advertisement
Ladies_Man

3_10 Ranks of binary search tree (ранги вершин бин.дерева)

Dec 30th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define cmm_len 7
  5.  
  6. char ins[cmm_len] = "INSERT";
  7. char sch[cmm_len] = "SEARCH";
  8. char lkp[cmm_len] = "LOOKUP";
  9. char del[cmm_len] = "DELETE";
  10. char cmm[6] = { 0 };
  11.  
  12. int n;
  13.  
  14. struct node {
  15.   int k, count;
  16.   struct node *parent, *left, *right;
  17.   char v[10];
  18. } *tree;
  19.  
  20. struct node *y_ins, *y_rot, *y_del, *y_scc, *p, *x_ins, *x_seq, *x_rep, *x_rot, *x_rea, *x_del, *x_dsc, *x_lkp, *x_min, *x_sch;
  21.  
  22. void inittree (struct node *s)
  23. {
  24.     s = NULL;
  25. }
  26.  
  27. void insert(void)
  28. {
  29.     int key;
  30.     struct node *x = tree, *y_ins = (struct node*)malloc(sizeof(struct node));
  31.     scanf("%d %s", &y_ins->k, y_ins->v);
  32.     key = y_ins->k;
  33.     y_ins->count = 1;
  34.     y_ins->parent = y_ins->left = y_ins->right = NULL;
  35.     if (tree == NULL)
  36.         tree = y_ins;
  37.     else
  38.         for (;;) {
  39.             if (key < x->k) {
  40.                 if (x->left == NULL) {
  41.                     x->left = y_ins;
  42.                     y_ins->parent = x;
  43.                     x->count++;
  44.                     break;
  45.                 }
  46.                 x->count++;
  47.                 x = x->left;
  48.             }
  49.             else {
  50.                 if (x->right == NULL) {
  51.                     x->right = y_ins;
  52.                     y_ins->parent = x;
  53.                     x->count++;
  54.                     break;
  55.                 }
  56.                 x->count++;
  57.                 x = x->right;
  58.             }
  59.         }
  60. }
  61.  
  62. void replacenode(struct node *x, struct node *y)
  63. {
  64.     struct node *p = x->parent;
  65.     if (x == tree) tree = y;
  66.     else {
  67.         if (y != NULL)
  68.             y->parent = p;
  69.             if (p->left == x) p->left = y;
  70.             else p->right = y;
  71.     }
  72. }
  73.  
  74. void search(void)
  75. {
  76.     int n;
  77.     scanf ("%d", &n);
  78.     int num = n + 1;
  79.     struct node *elem = tree;
  80.     for (;;) {
  81.         if (elem->left != NULL) {
  82.             if ((elem->left)->count + 1 == num) {
  83.                 printf("%s\n", elem->v);
  84.                 break;
  85.             }
  86.             else
  87.                 if ((elem->left)->count >= num) {
  88.                     elem = elem->left;
  89.                 }
  90.                 else {
  91.                     num = num - (elem->left)->count - 1;
  92.                     elem = elem->right;
  93.                 }
  94.         }
  95.         else {
  96.             if (num == 1) {
  97.                 printf("%s\n", elem->v);
  98.                 break;
  99.             }
  100.             else {
  101.                 elem = elem->right;
  102.                 num = num - 1;
  103.             }
  104.         }
  105.     }
  106. }
  107.  
  108. struct node *descend( int key)
  109. {
  110.     struct node *x_dsc = tree;
  111.     while (x_dsc != NULL && x_dsc->k != key) {
  112.         if (key < x_dsc->k) x_dsc = x_dsc->left;
  113.         else x_dsc = x_dsc->right;
  114.     }
  115.     return x_dsc;
  116. }
  117.  
  118. struct node *minimum(struct node *t)
  119. {
  120.     struct node turn;
  121.     struct node *x_min;
  122.     if (t == NULL) {
  123.         x_min = NULL;
  124.         return x_min;
  125.     }
  126.     else {
  127.         //struct node *x;
  128.         x_min = t;
  129.         while (x_min->left != NULL)
  130.             x_min = x_min->left;
  131.     }
  132.     return x_min;
  133. }
  134.  
  135. struct node *succ(struct node *elem)
  136. {
  137.     struct node turn;
  138.     struct node *y_scc;
  139.     if (elem->right != NULL)
  140.         y_scc = minimum(elem->right);
  141.     else {
  142.         y_scc = elem->parent;
  143.         while (y_scc != NULL && elem == y_scc->right) {
  144.             elem = y_scc;
  145.         y_scc = y_scc->parent;
  146.         }
  147.     }
  148.     return y_scc;
  149. }
  150.  
  151. void delete (void)
  152. {
  153.     int k;
  154.     scanf ("%d", &k);
  155.     struct node turn;
  156.     struct node *x_del = descend( k), *y_del;
  157.     if (x_del == NULL) printf ("panic");
  158.     if (x_del->left == NULL && x_del->right == NULL) {
  159.         edt(x_del);
  160.         replacenode(x_del, NULL);
  161.     }
  162.     else {
  163.         if (x_del->left == NULL) {
  164.             edt(x_del);
  165.             replacenode(x_del, x_del->right);
  166.         }
  167.         else {
  168.             if (x_del->right == NULL) {
  169.                 edt(x_del);
  170.                 replacenode(x_del, x_del->left);
  171.             }
  172.             else {
  173.                 y_del = succ(x_del);
  174.                 edt(y_del);
  175.                 replacenode(y_del, y_del->right);
  176.                 (x_del->left)->parent = y_del;
  177.                 y_del->left = x_del->left;
  178.                 if (x_del->right != NULL) (x_del->right)->parent = y_del;
  179.                 y_del->right = x_del->right;
  180.                 y_del->count = x_del->count;
  181.                 replacenode(x_del, y_del);
  182.             }
  183.         }
  184.     }
  185.     free(x_del);
  186. }
  187.  
  188. void lookup()
  189. {
  190.     int key;
  191.     scanf ("%d", &key);
  192.     struct node turn;
  193.     struct node *elem = descend(key);
  194.     printf("%s\n", elem->v);
  195. }
  196.  
  197. void replace(struct node *x_rep)
  198. {
  199.     if (x_rep != NULL) {
  200.         if (x_rep->left != NULL)
  201.             replace(x_rep->left);
  202.         if (x_rep->right != NULL)
  203.             replace(x_rep->right);
  204.         free(x_rep);
  205.     }
  206. }
  207.  
  208. int main()
  209. {
  210.     struct node turn;
  211.     int i;
  212.     scanf("%d", &n);
  213.     inittree (&turn);
  214.     for (i = 0; i < n; i++) {
  215.         scanf("%s", cmm);
  216.         if (0 == strcmp(cmm, ins)) insert();
  217.         if (0 == strcmp(cmm, sch)) search();
  218.         if (0 == strcmp(cmm, del)) delete();
  219.         if (0 == strcmp(cmm, lkp)) lookup();
  220.     }
  221.  
  222.     replace(tree);
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement