Chinaunix首页 | 论坛 | 博客
  • 博客访问: 298437
  • 博文数量: 134
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 118
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:02
文章分类

全部博文(134)

文章存档

2015年(2)

2014年(4)

2013年(128)

分类: C/C++

2013-08-01 14:16:40

原文地址:二叉树 _0313 作者:丫叩酱


点击(此处)折叠或打开

  1. //bin_tree_create.c

  2. //以先序和中序唯一确定一个二叉树,然后以先序、中序、后序和层次遍历输出二叉树(遍历输出时向先序、中序、后序和层次传递一个(输出)函数指针)

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <assert.h>

  6. typedef struct btree
  7. {
  8.     int item;
  9.     struct btree *l, *r;
  10. }btree_t;


  11. int menu(void)
  12. {
  13.    int choice;

  14.    printf("menu:\n");
  15.    printf("0: exit\n1: print\n");
  16.    printf("Please make a choice:");
  17.    scanf("%d", &choice);

  18.    return choice;
  19. }

  20. int menu_print(void)
  21. {
  22.     int choice;

  23.     printf("0: exit\n1: pre_order\n2: in_order\n3: post_order\n4: dep_order\n");
  24.     printf("Please maka a print_choice:");
  25.     scanf("%d", &choice);

  26.     return choice;
  27. }

  28. btree_t *make_node(int item)
  29. {
  30.     btree_t *bt;

  31.     bt = malloc(sizeof(btree_t));
  32.     assert(bt);
  33.     
  34.     bt->item = item;
  35.     bt->l = NULL;
  36.     bt->r = NULL;

  37.     return bt;
  38. }

  39. btree_t *create_tree(int *pre, int *in, int n)
  40. {
  41.    int k;
  42.    btree_t *bt;

  43.    if(n == 0)
  44.         return NULL;

  45.    for(k = 0; pre[0] != in[k]; k++);

  46.    bt = make_node(pre[0]) ;
  47.    bt->l = create_tree(pre + 1, in, k);
  48.    bt->r = create_tree(pre + k + 1, in + k + 1, n - k -1 );

  49.    return bt;
  50. }

  51. void pre_order(btree_t *root, void (*visit)(btree_t *))
  52. {
  53.     if(root == NULL)
  54.         return ;
  55.     visit(root);
  56.     pre_order(root->l, visit);
  57.     pre_order(root->r, visit);
  58. }

  59. void in_order(btree_t *root, void (*visit)(btree_t *))
  60. {
  61.     if(root == NULL)
  62.         return ;
  63.     in_order(root->l, visit);
  64.     visit(root);
  65.     in_order(root->r, visit);
  66. }

  67. void post_order(btree_t *root, void (*visit)(btree_t *))
  68. {
  69.     if(root == NULL)
  70.         return ;
  71.     post_order(root->l, visit);
  72.     post_order(root->r, visit);
  73.     visit(root);
  74. }

  75. void dep_ord(btree_t *root, void (*visit)(btree_t *))
  76. {
  77.    if(root->l != NULL)
  78.         visit(root->l);
  79.    if(root->r != NULL)
  80.         visit(root->r);

  81.    if(root->l != NULL)
  82.         dep_ord(root->l, visit);
  83.    if(root->r != NULL)
  84.         dep_ord(root->r, visit);
  85. }

  86. void dep_order(btree_t *root, void (*visit)(btree_t *))
  87. {
  88.     if(root == NULL)
  89.         return ;
  90.     visit(root);
  91.     dep_ord(root, visit);
  92. }

  93. void print(btree_t *bt)
  94. {
  95.     printf("%d ", bt->item);
  96. }


  97. int main(int argc, const char *argv[])
  98. {
  99.     btree_t *root;

  100.     int pre[] = {4, 2, 1, 3, 5, 6};
  101.     int in[] = {1, 2, 3, 4, 5, 6};

  102.     root = create_tree(pre, in, sizeof(pre)/sizeof(int));
  103.     while(1)
  104.     {
  105.         switch(menu())
  106.         {
  107.             case 0:return 0;
  108.             case 1:
  109.             {
  110.                 while(1)
  111.                 {
  112.                     int flag = 0;
  113.                     switch(menu_print())
  114.                     {
  115.                         case 0:flag = 1;break;
  116.                         case 1:
  117.                                {
  118.                                    printf("pre_order:");
  119.                                    pre_order(root, print);
  120.                                    putchar('\n');
  121.                                    break;
  122.                                }
  123.                         case 2:
  124.                                {
  125.                                    printf("in_order:");
  126.                                    in_order(root, print);
  127.                                    putchar('\n');
  128.                                    break;
  129.                                }
  130.                         case 3:
  131.                                {
  132.                                    printf("post_order:");
  133.                                    post_order(root, print);
  134.                                    putchar('\n');
  135.                                    break;
  136.                         case 4:
  137.                                {
  138.                                    printf("dep_order:");
  139.                                    dep_order(root, print);
  140.                                    putchar('\n');
  141.                                    break;
  142.                                }
  143.                                }
  144.                         default:;
  145.                     }
  146.                     if(flag == 1)
  147.                         break;
  148.                 }
  149.                 break;
  150.             }

  151.             default:;
  152.         }
  153.     }

  154.     return 0;
  155. }

点击(此处)折叠或打开

  1. //btree.c

  2. //要求:
  3. //以先序和中序唯一确定一个二叉树
  4. //实现以先序、中序、后序和层次输出二叉树
  5. //实现插入和删除节点
  6. //求二叉树的深度
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>

  4. typedef struct btree
  5. {
  6.     int item;
  7.     struct btree *l, *r;
  8. }btree_t;


  9. int menu(void)
  10. {
  11.    int choice;

  12.    printf("menu:\n");
  13.    printf("0: exit\n1: print\n2: insert\n3: delete\n4: depth\n");
  14.    printf("Please make a choice:");
  15.    scanf("%d", &choice);

  16.    return choice;
  17. }

  18. int menu_print(void)
  19. {
  20.     int choice;

  21.     printf("print menu:\n0: exit\n1: pre_order\n2: in_order\n3: post_order\n4: dep_order\n");
  22.     printf("Please maka a print_choice:");
  23.     scanf("%d", &choice);

  24.     return choice;
  25. }

  26. btree_t *make_node(int item)
  27. {
  28.     btree_t *bt;

  29.     bt = malloc(sizeof(btree_t));
  30.     assert(bt);
  31.     
  32.     bt->item = item;
  33.     bt->l = NULL;
  34.     bt->r = NULL;

  35.     return bt;
  36. }

  37. btree_t *create_tree(int *pre, int *in, int n)
  38. {
  39.    int k;
  40.    btree_t *bt;

  41.    if(n == 0)
  42.         return NULL;

  43.    for(k = 0; pre[0] != in[k]; k++);

  44.    bt = make_node(pre[0]) ;
  45.    bt->l = create_tree(pre + 1, in, k);
  46.    bt->r = create_tree(pre + k + 1, in + k + 1, n - k -1 );

  47.    return bt;
  48. }

  49. void pre_order(btree_t *root)
  50. {
  51.     if(root == NULL)
  52.         return ;
  53.     printf("%d ", root->item);
  54.     pre_order(root->l);
  55.     pre_order(root->r);
  56. }

  57. void in_order(btree_t *root)
  58. {
  59.     if(root == NULL)
  60.         return ;
  61.     in_order(root->l);
  62.     printf("%d ", root->item);
  63.     in_order(root->r);
  64. }

  65. void post_order(btree_t *root)
  66. {
  67.     if(root == NULL)
  68.         return ;
  69.     post_order(root->l);
  70.     post_order(root->r);
  71.     printf("%d ", root->item);
  72. }

  73. void dep_ord(btree_t *root)
  74. {
  75.    if(root->l != NULL)
  76.         printf("%d ", root->l->item);
  77.    if(root->r != NULL)
  78.         printf("%d ", root->r->item);

  79.    if(root->l != NULL)
  80.         dep_ord(root->l);
  81.    if(root->r != NULL)
  82.         dep_ord(root->r);
  83. }

  84. void dep_order(btree_t *root)
  85. {
  86.     if(root == NULL)
  87.         return ;
  88.     printf("%d ", root->item);
  89.     dep_ord(root);
  90. }

  91. int depth(btree_t *root)
  92. {
  93.     int dl, dr;

  94.     if(root == NULL)
  95.         return 0;
  96.     dl = depth(root->l) ;
  97.     dr = depth(root->r);

  98.     return 1 + (dl > dr? dl : dr);
  99. }


  100. btree_t *insert(btree_t *root, int key)
  101. {
  102.     if(! root)
  103.         return make_node(key);
  104.     if(root->item > key)
  105.         root->l = insert(root->l, key);
  106.     if(root->item < key)
  107.         root->r = insert(root->r, key);
  108.     return root;
  109. }

  110. btree_t *delete(btree_t *root, int key)
  111. {
  112.     if(! root)
  113.         return NULL;
  114.     if(root->item > key)
  115.         root->l = delete(root->l, key);
  116.     else
  117.         if(root->item < key)
  118.             root->r = delete(root->r, key);
  119.         else
  120.         {
  121.             if(root->l == NULL && root->r == NULL)
  122.             {
  123.                 free(root);
  124.                 root = NULL;
  125.             }
  126.             else
  127.                 if(root->l)
  128.                 {
  129.                     btree_t *p;
  130.                     for(p = root->l; p->r; p = p->r);
  131.                     root->item = p->item;

  132.                     root->l = delete(root->l, p->item);
  133.                 }
  134.                 else if(root->r)
  135.                 {
  136.                     btree_t *p;
  137.                     for(p = root->r; p->l; p = p->l);
  138.                     root->item = p->item;

  139.                     root->r = delete(root->r, p->item);
  140.                 }
  141.         }
  142.     return root;
  143. }

  144. int main(int argc, const char *argv[])
  145. {
  146.     btree_t *root;

  147.     int pre[] = {40, 20, 10, 30, 50, 60};
  148.     int in[] = {10, 20, 30, 40, 50, 60};

  149.     root = create_tree(pre, in, sizeof(pre)/sizeof(int));
  150.     while(1)
  151.     {
  152.         switch(menu())
  153.         {
  154.             case 0:return 0;
  155.             case 1:
  156.             {
  157.                 while(1)
  158.                 {
  159.                     int flag = 0;
  160.                     switch(menu_print())
  161.                     {
  162.                         case 0:flag = 1;break;
  163.                         case 1:
  164.                                {
  165.                                    printf("pre_order:");
  166.                                    pre_order(root);
  167.                                    putchar('\n');
  168.                                    break;
  169.                                }
  170.                         case 2:
  171.                                {
  172.                                    printf("in_order:");
  173.                                    in_order(root);
  174.                                    putchar('\n');
  175.                                    break;
  176.                                }
  177.                         case 3:
  178.                                {
  179.                                    printf("post_order:");
  180.                                    post_order(root);
  181.                                    putchar('\n');
  182.                                    break;
  183.                         case 4:
  184.                                {
  185.                                    printf("dep_order:");
  186.                                    dep_order(root);
  187.                                    putchar('\n');
  188.                                    break;
  189.                                }
  190.                                }
  191.                         default:;
  192.                     }
  193.                     if(flag == 1)
  194.                         break;
  195.                 }
  196.                 break;
  197.             }
  198.             case 2:
  199.             {

  200.                 while(1)
  201.                 {
  202.                     int key;
  203.                     printf("insert menu:\n");
  204.                     printf("0: exit\n");
  205.                     printf("key = ");
  206.                     scanf("%d", &key);
  207.                     if(key == 0)
  208.                         break;
  209.                     root = insert(root, key) ;
  210.                 }
  211.                 break;
  212.             }
  213.             case 3:
  214.             {
  215.                 while(1)
  216.                 {
  217.                     int key;
  218.                     printf("delete menu:\n");
  219.                     printf("0: exit\n");
  220.                     printf("key = ");
  221.                     scanf("%d", &key);
  222.                     if(key == 0)
  223.                         break;
  224.                     root = delete(root, key);
  225.                 }
  226.                 break;
  227.             }
  228.             case 4:
  229.             {
  230.                 printf("depth = %d\n", depth(root));
  231.                 break;
  232.             }
  233.             default:;
  234.         }
  235.     }

  236.     return 0;
  237. }

点击(此处)折叠或打开

  1. //bt_search.c

  2. //生成一个26个英文字母的二叉树,输入一个字母,若找到输出;否则输出没找到

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <assert.h>

  7. typedef struct btree
  8. {
  9.     char ch;
  10.     struct btree *l, *r;
  11. }btree_t;

  12. btree_t *make_node(char ch)
  13. {
  14.     btree_t *bt;

  15.     bt = malloc(sizeof(btree_t));
  16.     assert(bt);

  17.     bt->ch = ch;
  18.     bt->l = NULL;
  19.     bt->r = NULL;

  20.     return bt;
  21. }

  22. btree_t *insert(btree_t *root, char ch)
  23. {
  24.     if(! root)
  25.         return make_node(ch);
  26.     if(root->ch > ch)
  27.         root->l = insert(root->l, ch);
  28.     if(root->ch < ch)
  29.         root->r = insert(root->r, ch);
  30.     return root;
  31. }

  32. int counter(btree_t *root)
  33. {
  34.     int counter_l, counter_r;
  35.     if(root == NULL)
  36.         return 0;
  37.     counter_l = counter(root->l);
  38.     counter_r = counter(root->r);

  39.     return counter_l + counter_r + 1;

  40. }

  41. btree_t *create(btree_t *root)
  42. {

  43.     srand(time(NULL));
  44.     while(counter(root) < 26)
  45.     {
  46.         root = insert(root, rand() % 26 + 'A');
  47.     }

  48.     return root;
  49. }

  50. void in_order(btree_t *root)
  51. {
  52.     if(! root)
  53.         return ;
  54.     in_order(root->l);
  55.     printf("%c ", root->ch);
  56.     in_order(root->r);
  57. }

  58. btree_t *search(btree_t *root, char ch)
  59. {
  60.    if(! root)
  61.         return NULL;

  62.    if(root->ch > ch)
  63.         return root->l = search(root->l, ch);
  64.    if(root->ch < ch)
  65.        return root->r = search(root->r, ch);

  66.    return root;
  67. }

  68. int main(int argc, const char *argv[])
  69. {
  70.     btree_t *root = NULL;
  71.     char ch;

  72.     root = create(root);
  73.     in_order(root);
  74.     putchar('\n');

  75.     printf("search:");
  76.     scanf("%c", &ch);

  77.     btree_t *bt;
  78.     bt = search(root, ch);
  79.     if(bt)
  80.         printf("find:ch = %c\n", bt->ch);
  81.     else
  82.         printf("not find\n");
  83.     return 0;
  84. }

阅读(654) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~