蔡子经数据结构6.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


#include <stdio.h>
#include <stdlib.h>
typedef struct node node;
struct node
{
int val,mark;
node *lchild,*rchild;
};
#define MAXN 100
int a[MAXN] = {1,2,3,4,5,6,7};

node* createNode()
{
node *rt = (node*)malloc(sizeof(node));
rt->lchild = rt->rchild = NULL;
rt->val = rt->mark = 0;
return rt;
}

node *createTree(int l, int r)
{
if(l > r) return NULL;
int mid = (l+r)/2;
node *rt = createNode();
rt->val = a[mid];
rt->lchild = createTree(l, mid-1);
rt->rchild = createTree(mid+1, r);
return rt;
}

void InOrder(node *rt)
{
if(rt == NULL) return;
InOrder(rt->lchild);
printf("%d ",rt->val);
InOrder(rt->rchild);
}

int main()
{
node* root = createTree(0, 6);
InOrder(root);
puts("");
return 0;
}