-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.2.cpp
More file actions
71 lines (62 loc) · 1.87 KB
/
7.2.cpp
File metadata and controls
71 lines (62 loc) · 1.87 KB
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
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char TElemType;
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
int FindRootIndex(TElemType *in_order, int in_start, int in_end, TElemType root_val) {
for (int i = in_start; i <= in_end; i++) {
if (in_order[i] == root_val) {
return i;
}
}
return -1;
}
// 递归构造二叉树
BiTree BuildTreeFromPreIn(TElemType *pre_order, TElemType *in_order, int in_start, int in_end, int *pre_idx) {
if (in_start > in_end) {
return NULL;
}
TElemType root_val = pre_order[*pre_idx];
(*pre_idx)++;
BiTree root = (BiTree)malloc(sizeof(BiTNode));
root->data = root_val;
root->lchild = root->rchild = NULL;
if (in_start == in_end) {
return root;
}
int root_in_idx = FindRootIndex(in_order, in_start, in_end, root_val);
root->lchild = BuildTreeFromPreIn(pre_order, in_order, in_start, root_in_idx - 1, pre_idx);
root->rchild = BuildTreeFromPreIn(pre_order, in_order, root_in_idx + 1, in_end, pre_idx);
return root;
}
void PostOrderTraverse(BiTree T) {
if (T != NULL) {
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
printf("%c", T->data);
}
}
void DestroyTree(BiTree *T) {
if (*T != NULL) {
DestroyTree(&((*T)->lchild));
DestroyTree(&((*T)->rchild));
free(*T);
*T = NULL;
}
}
int main() {
TElemType pre_order[] = "ABDFCEGH";
TElemType in_order[] = "BFDAGEHC";
int pre_idx = 0;
BiTree T = BuildTreeFromPreIn(pre_order, in_order, 0, strlen(in_order) - 1, &pre_idx);
printf("先序序列:%s\n", pre_order);
printf("中序序列:%s\n", in_order);
printf("构造二叉树的后序遍历序列:");
PostOrderTraverse(T);
printf("\n");
DestroyTree(&T);
return 0;
}