蔡子经数据结构5.10

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct node
{
int ltag;
char data;
int rchild;
} node;
#define MAXN 8
node a[MAXN] =
{
{0,'A',4},
{1,'B',2},
{0,'D',-1},
{1,'F',-1},
{0,'C',-1},
{0,'E',-1},
{1,'G',7},
{1,'H',-1}
};

typedef struct TreeNode
{
char data;
struct TreeNode *lchild, *rchild;
} TNode;

TNode* createNode()
{
TNode *ret = (TNode*)malloc(sizeof(TNode));
ret->lchild = ret->rchild = NULL;
return ret;
}
//这个题目要求的功能就在这个函数实现的
TNode* createTree(int cur)
{
TNode *ret = createNode();
ret->data = a[cur].data;
if(a[cur].ltag == 0)
ret->lchild = createTree(cur+1);
if(a[cur].rchild != -1)
ret->rchild = createTree(a[cur].rchild);
return ret;
}

void MidOrder(TNode *rt)
{
if(rt == NULL) return;
MidOrder(rt->lchild);
putchar(rt->data);
MidOrder(rt->rchild);
}

int main()
{
TNode *root = createTree(0);
MidOrder(root);
return 0;
}