Chinaunix首页 | 论坛 | 博客
  • 博客访问: 144384
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 218
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:02
文章分类
文章存档

2017年(1)

2016年(20)

我的朋友

分类: LINUX

2016-09-06 17:12:59

common.h 为常用的头文件,代码实现了add、show。

点击(此处)折叠或打开

  1. #include "common.h"

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

  8. TNode *tree_init(int value)
  9. {
  10.     TNode *node = NULL;
  11.     node = (TNode *)malloc(sizeof(TNode));
  12.     if(!node){
  13.         printf("head malloc failed\n");
  14.         goto err;
  15.     }
  16.     node->data = value;
  17.     node->left = NULL;
  18.     node->right = NULL;

  19. err:
  20.     return node;
  21. }

  22. int tree_add(TNode *root ,TNode *node)
  23. {
  24.     TNode *head = root;
  25.     TNode *cur = head;
  26.     while(cur){
  27.         if(node->data < cur->data){
  28.             //left
  29.             if(!(cur->left)){
  30.                 cur->left = node;
  31.                 break;
  32.             }
  33.             cur = cur->left;    
  34.         }else{
  35.             //right
  36.             if(!(cur->right)){
  37.                 cur->right = node;
  38.                 break;
  39.             }
  40.             cur = cur->right;
  41.         }
  42.     }

  43.     return 0;    
  44. }
  45. #define ARRAY_MAX 40
  46. TNode *array[ARRAY_MAX];

  47. void tree_init_right()
  48. {
  49.     int i = 0;
  50.     while(i < ARRAY_MAX){
  51.         array[i++] = NULL;
  52.     }
  53. }
  54. TNode *tree_get_right()
  55. {
  56.     int i = -1;
  57.     TNode *rt = NULL;
  58.     while(!array[++i]){
  59.         if(i >= ARRAY_MAX){
  60.             return NULL;
  61.         }
  62.     }

  63.     rt = array[i];
  64.     array[i] = NULL;
  65.     return rt;
  66. }

  67. void tree_set_right(TNode *pointer)
  68. {
  69.     int i = 0;
  70.     while(i < ARRAY_MAX){
  71.         if(!array[i]){
  72.             array[i] = pointer;
  73.             break;
  74.         }
  75.         i++;
  76.     }
  77. }
  78. void tree_show(TNode *root)
  79. {
  80.     TNode *head = root;
  81.     TNode *cur = head;    
  82.     tree_init_right();

  83.     while(cur){
  84.         printf("data:[%d]\n" ,cur->data);
  85.         if(cur->left){
  86.             tree_set_right(cur->right);
  87.             cur = cur->left;
  88.         }else if(cur->right){
  89.             tree_set_right((cur->left));
  90.             cur = cur->right;
  91.         }else{
  92.             cur = tree_get_right();
  93.             if(!cur){
  94.                 goto out;
  95.             }
  96.         }
  97.     }
  98. out:
  99.     return;
  100. }
  101. int main(void)
  102. {
  103.     TNode *head = NULL;
  104.     TNode *node = NULL;
  105.     int value[10] = {6,7,2,1,4,3,8,10,9,5};
  106.     int i = 1;

  107.     head = tree_init(value[0]);
  108.     if(!head){
  109.         goto err;
  110.     }

  111.     while(i < 10){
  112.         node = tree_init(value[i]);
  113.         if(!node){
  114.             goto err;
  115.         }

  116.         tree_add(head ,node);
  117.         i++;
  118.     }
  119.     tree_show(head);
  120.     return 0;

  121. err:
  122.     printf("err msg\n");
  123.     return -1;
  124. }

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