Chinaunix首页 | 论坛 | 博客
  • 博客访问: 257116
  • 博文数量: 49
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-13 00:59
个人简介

make it run,make it better,make it fast. https://github.com/liulanghaitun

文章分类

全部博文(49)

文章存档

2023年(1)

2022年(2)

2020年(4)

2019年(4)

2017年(15)

2016年(3)

2014年(3)

2013年(14)

分类: C/C++

2017-10-20 18:38:45


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct tree
  4. {
  5.     int data;
  6.     struct tree *left;
  7.     struct tree *right;
  8. }tree_node;

  9. tree_node* create_tree_node(int data)
  10. {
  11.     tree_node* message = malloc(sizeof(tree_node));
  12.     if(message)
  13.     {
  14.         message->left = NULL;
  15.         message->right = NULL;
  16.         message->data = data;
  17.     }
  18.     return message;
  19. }

  20. void insert_tree_node(tree_node** current,int data)
  21. {
  22.     if(!*current)
  23.     {
  24.         *current = create_tree_node(data);
  25.         return;
  26.     }
  27.     if((*current)->data > data)
  28.     {
  29.         insert_tree_node(&(*current)->left,data);
  30.     }else
  31.     {
  32.         insert_tree_node(&(*current)->right,data);
  33.     }
  34. }

  35. void delete_tree(tree_node* message)
  36. {
  37.     if(message)
  38.     {
  39.         delete_tree(message->left);
  40.         delete_tree(message->right);
  41.         printf("delete:%d\r\n",message->data);
  42.         free(message);
  43.     }
  44. }

  45. void print_pre_order(tree_node* message)
  46. {
  47.     if(message)
  48.     {
  49.         printf("%d ",message->data);
  50.         print_pre_order(message->left);
  51.         print_pre_order(message->right);
  52.     }
  53. }

  54. void print_middle_order(tree_node* message)
  55. {
  56.     if(message)
  57.     {
  58.         print_middle_order(message->left);
  59.         printf("%d ",message->data);
  60.         print_middle_order(message->right);
  61.     }
  62. }

  63. void print_post_value(tree_node* message)
  64. {
  65.     if(message)
  66.     {
  67.         print_post_value(message->left);
  68.         print_post_value(message->right);
  69.         printf("%d ",message->data);
  70.     }
  71. }

  72. unsigned const int get_node_count(tree_node* root){
  73.     if(root){
  74.         return get_node_count(root->left)+get_node_count(root->right) +1;
  75.     }
  76.     return 0;
  77. }

  78. void print_level_order(tree_node *root,const unsigned int nodeCount)
  79. {
  80.     tree_node *current;
  81.     tree_node *queue_array[nodeCount];
  82.     int front = -1,rear = -1;
  83.     queue_array[++rear]=root;
  84.     while(front != rear)
  85.     {
  86.         front=(front+1)%nodeCount;
  87.         current=queue_array[front];
  88.         printf("%d ",current->data);
  89.         
  90.         if(current->left)
  91.         {
  92.             rear=(rear+1)%nodeCount;
  93.             queue_array[rear]=current->left;
  94.         }
  95.  
  96.         if(current->right)
  97.         {
  98.             rear=(rear+1)%nodeCount;
  99.             queue_array[rear]=current->right;
  100.         }
  101.     }
  102. }

  103. tree_node* find_tree_node(tree_node* root,int value)
  104. {
  105.     if(root)
  106.     {
  107.         if(root->data > value)
  108.         {
  109.               return find_tree_node(root->left,value);
  110.         }else if(root->data < value){
  111.               return find_tree_node(root->right,value);
  112.         }
  113.     }
  114.     return root;
  115. }

  116. int main(void)
  117. {
  118.     tree_node* root = NULL;
  119.     const int nodeValue[] = {10,49,24,69,35,4,13};
  120.     int index = 0;
  121.     for(; index<7; index++)
  122.     {
  123.         insert_tree_node(&root,nodeValue[index]);
  124.     }
  125.     print_pre_order(root);
  126.     printf("\r\n");
  127.     print_middle_order(root);
  128.     printf("\r\n");
  129.     print_post_value(root);
  130.     printf("\r\n");
  131.     print_level_order(root,get_node_count(root));
  132.     printf("\r\n");
  133.     printf("count==%d",get_node_count(root));
  134.     printf("\r\n");
  135.     if(find_tree_node(root,13))
  136.     {
  137.         printf("find success!");
  138.     }else{
  139.         printf("find failed!");
  140.     }
  141.     printf("\r\n");
  142.     delete_tree(root);
  143.     return 0;
  144. }

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