1 #define MAX_ARRAY_SIZE 21
 2 int array[MAX_ARRAY_SIZE] = {1,2,4,8,0,0,9,0,0,5,10,0,0,0,3,6,0,0,7,0,0};
 3 static int index = 0;
 4 
 5 typedef struct binary_tree_t
 6 {
 7     int data;
 8     struct binary_tree_t *left;
 9     struct binary_tree_t *right;
10 }binary_tree;
11 
12 struct binary_tree_t *create_binary_tree()
13 {
14     if(array[index] == 0)
15     {
16         ++index;
17         return NULL;
18     }
19 
20     struct binary_tree_t *node = (struct binary_tree_t *)malloc(sizeof(struct binary_tree_t));
21     if(node == NULL)
22     {
23         exit(1);
24     }
25 
26     node->data = array[index++];
27     node->left = create_binary_tree();
28     node->right = create_binary_tree();
29 
30     return node;
31 }
32 
33 void visit(struct binary_tree_t *node)
34 {
35     printf("%d ",node->data);
36 }
37 
38 
39 void pre_traverse(struct binary_tree_t *root)
40 {
41     if(root == NULL) return ;
42 
43     visit(root);
44 
45     pre_traverse(root->left);
46     pre_traverse(root->right);
47 }
48 
49 void mid_traverse(struct binary_tree_t *root)
50 {
51     if(root == NULL) return;
52 
53     mid_traverse(root->left);
54     visit(root);
55     mid_traverse(root->right);
56 }
57 
58 void order_traverse(struct binary_tree_t *root)
59 {
60     if(root == NULL) return;
61 
62     order_traverse(root->left);
63     order_traverse(root->right);
64 
65     visit(root);
66 }

 

posted on 2018-03-04 22:06  gtxvs  阅读(163)  评论(0编辑  收藏  举报