Advertisement
shogoe

Binary tree

Dec 22nd, 2013
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define KEY_LEN     100
  6. #define VALUE_LEN   100
  7. #define BUF_LEN     100
  8.  
  9. typedef struct tnode {
  10.     char key[KEY_LEN];
  11.     char value[VALUE_LEN];
  12.     struct tnode *left;
  13.     struct tnode *right;
  14.     struct tnode *parent;
  15. } tnode;
  16.  
  17. tnode *btree_insert(char *key, char *val, tnode *t, tnode *p)
  18. {
  19.     if (t == NULL) {
  20.         t = calloc(1, sizeof(tnode));
  21.         strcpy(t->key, key);
  22.         strcpy(t->value, val);
  23.         t->parent = p;
  24.     }
  25.     else {
  26.         int cmp = strcmp(key, t->key);
  27.         if (cmp < 0)
  28.             t->left = btree_insert(key, val, t->left, t);
  29.         else if (cmp > 0)
  30.             t->right = btree_insert(key, val, t->right, t);
  31.         else
  32.             printf("key [%s] is already inserted\n", t->key);
  33.     }
  34.     return t;
  35. }
  36.  
  37. tnode *btree_search(char *key, tnode *t)
  38. {
  39.     if (t != NULL) {
  40.         int cmp = strcmp(key, t->key);
  41.         if (cmp < 0)
  42.             return btree_search(key, t->left);
  43.         else if (cmp > 0)
  44.             return btree_search(key, t->right);
  45.     }
  46.     return t;
  47. }
  48.  
  49. void btree_destroy(tnode *t)
  50. {
  51.     if (t == NULL)
  52.         return;
  53.     btree_destroy(t->left);
  54.     btree_destroy(t->right);
  55.     free(t);
  56. }
  57.  
  58. static tnode *reinsert_btree(tnode *dont, tnode *t, tnode *top, tnode *p)
  59. {
  60.     if (t == NULL)
  61.         return top;
  62.     top = reinsert_btree(dont, t->left, top, p);
  63.     if (t != dont)
  64.         top = btree_insert(t->key, t->value, top, p);
  65.     return reinsert_btree(dont, t->right, top, p);
  66. }
  67.  
  68. tnode *btree_delete(char *key, tnode *t)
  69. {
  70.     tnode *top = t;
  71.     tnode *d;
  72.  
  73.     d = btree_search(key, t);
  74.     if (d != NULL) {
  75.         tnode *gp = NULL;
  76.         if (d->parent) {
  77.             if (d->parent->left == d)
  78.                 d->parent->left = NULL;
  79.             else
  80.                 d->parent->right = NULL;
  81.             gp = d->parent->parent;
  82.         }
  83.         t = reinsert_btree(d, d, d->parent, gp);
  84.         if (d == top)
  85.             top = t;
  86.  
  87.         btree_destroy(d);
  88.     }
  89.     return top;
  90. }
  91.  
  92. #define STRING_LEN  9
  93. static void draw_string(char *s, char pad)
  94. {
  95.     int i = 0;
  96.     while (s[i])
  97.         putchar(s[i++]);
  98.     while (i++ < STRING_LEN)
  99.         putchar(pad);
  100. }
  101.  
  102. static void draw_line_to_left(int *line_to_left, int cnest)
  103. {
  104.     while (cnest--)
  105.         draw_string(*line_to_left++ ? "|" : " ", ' ');
  106.     draw_string("+", '-');
  107. }
  108.  
  109. static void draw_btree(tnode *t, int cnest)
  110. {
  111.     static int line_to_left[26] = {0};
  112.  
  113.     if (t != NULL) {
  114.         draw_string(t->key, '-');
  115.  
  116.         line_to_left[cnest] = 1;
  117.         draw_btree(t->right, cnest + 1);
  118.  
  119.         draw_line_to_left(line_to_left, cnest);
  120.  
  121.         line_to_left[cnest] = 0;
  122.         draw_btree(t->left, cnest + 1);
  123.  
  124.     } else {
  125.         printf("(null)\n");
  126.     }
  127. }
  128.  
  129. static void show_node(tnode *t)
  130. {
  131.     printf("key:[%s]\tvalue:[%s]\tleft:[%s]\tright:[%s]\tparent:[%s]\n",
  132.                     t->key, t->value,
  133.                     t->left  == NULL ? "null" : t->left->key,
  134.                     t->right == NULL ? "null" : t->right->key,
  135.                     t->parent == NULL ? "null" : t->parent->key);
  136. }
  137.  
  138. static void show_btree(tnode *t)
  139. {
  140.     if (t == NULL)
  141.         return;
  142.     show_btree(t->left);
  143.     show_node(t);
  144.     show_btree(t->right);
  145. }
  146.  
  147. typedef enum {
  148.     CMD_NONE, CMD_INSERT, CMD_DELETE, CMD_SEARCH, CMD_SHOW, CMD_QUIT
  149. } cmd;
  150.  
  151. static cmd get_cmd()
  152. {
  153.     cmd ret = CMD_NONE;
  154.     char buf[BUF_LEN];
  155.  
  156.     while (ret == CMD_NONE) {
  157.         printf("insert/delete/search/show/quit = ");
  158.         if (scanf(" %s", buf) == EOF)
  159.             ret = CMD_QUIT;
  160.         else if (!strcmp(buf, "insert"))
  161.             ret = CMD_INSERT;
  162.         else if (!strcmp(buf, "delete"))
  163.             ret = CMD_DELETE;
  164.         else if (!strcmp(buf, "search"))
  165.             ret = CMD_SEARCH;
  166.         else if (!strcmp(buf, "show"))
  167.             ret = CMD_SHOW;
  168.         else if (!strcmp(buf, "quit"))
  169.             ret = CMD_QUIT;
  170.     }
  171.     return ret;
  172. }
  173.  
  174. int main(void)
  175. {
  176.     int quit;
  177.     char key[KEY_LEN], val[VALUE_LEN];
  178.     tnode *top = NULL, *search;
  179.  
  180. #ifdef DEBUG
  181.     int i;
  182.     for (i = 0; i < 10; i++) {
  183.         do {
  184.             int n = rand() % 10;
  185.             sprintf(key, "%d", n);
  186.             sprintf(val, "%d%d%d", n, n, n);
  187.         } while (btree_search(key, top) != NULL);
  188.         top = btree_insert(key, val, top, NULL);
  189.     }
  190. #endif
  191.  
  192.     quit = 0;
  193.     while (!quit) {
  194.         switch (get_cmd()) {
  195.         case CMD_INSERT:
  196.             printf("key value: ");
  197.             scanf("%s %s", key, val);  
  198.             top = btree_insert(key, val, top, NULL);
  199.             break;
  200.         case CMD_DELETE:
  201.             printf("key: ");
  202.             scanf("%s", key);
  203.             top = btree_delete(key, top);
  204.             break;
  205.         case CMD_SEARCH:
  206.             printf("key: ");
  207.             scanf("%s", key);
  208.             search = btree_search(key, top);
  209.             if (search != NULL)
  210.                 show_node(search);
  211.             break;
  212.         case CMD_SHOW:
  213.             draw_btree(top, 0);
  214.             show_btree(top);
  215.             break;
  216.         case CMD_QUIT:
  217.             quit = 1;
  218.             break;
  219.         default:
  220.             break;
  221.         }
  222.     }
  223.     btree_destroy(top);
  224.     return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement