-
Notifications
You must be signed in to change notification settings - Fork 0
/
btreebuild.c
101 lines (86 loc) · 2.57 KB
/
btreebuild.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Copyright © 2021-2023 Chee Bin HOH. All rights reserved.
*
* Build binary tree from inorder and postorder list.
*/
#include "btree.h"
#include "btreetraverse.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i;
struct TreeNode *root = NULL;
int inorder[] = {9, 3, 15, 20, 7};
int postorder[] = {9, 15, 7, 20, 3};
int inorder2[] = {2, 1};
int postorder2[] = {2, 1};
int inorder3[] = {1, 2};
int postorder3[] = {2, 1};
struct ListNode *inorderList;
struct ListNode *postorderList;
inorderList = NULL;
enQueueInt(9, &inorderList);
enQueueInt(3, &inorderList);
enQueueInt(15, &inorderList);
enQueueInt(20, &inorderList);
enQueueInt(7, &inorderList);
printf("Inorder list: ");
printListInt(inorderList);
postorderList = NULL;
enQueueInt(9, &postorderList);
enQueueInt(15, &postorderList);
enQueueInt(7, &postorderList);
enQueueInt(20, &postorderList);
enQueueInt(3, &postorderList);
printf("Postorder list: ");
printListInt(postorderList);
printf("\n");
root = buildBinaryTree(inorderList, postorderList);
printf("Building binary tree from inorder and postorder list, the tree "
"topology:\n");
printf("\n");
printTreeNodeInTreeTopology(root);
printf("\n");
freeList(&inorderList);
freeList(&postorderList);
inorderList = NULL;
enQueueInt(2, &inorderList);
enQueueInt(1, &inorderList);
printf("Inorder list: ");
printListInt(inorderList);
postorderList = NULL;
enQueueInt(2, &postorderList);
enQueueInt(1, &postorderList);
printf("Postorder list: ");
printListInt(postorderList);
printf("\n");
root = buildBinaryTree(inorderList, postorderList);
printf("Building binary tree from inorder and postorder list, the tree "
"topology:\n");
printf("\n");
printTreeNodeInTreeTopology(root);
printf("\n");
printf("\n");
freeList(&inorderList);
freeList(&postorderList);
inorderList = NULL;
enQueueInt(1, &inorderList);
enQueueInt(2, &inorderList);
printf("Inorder list: ");
printListInt(inorderList);
postorderList = NULL;
enQueueInt(2, &postorderList);
enQueueInt(1, &postorderList);
printf("Postorder list: ");
printListInt(postorderList);
printf("\n");
root = buildBinaryTree(inorderList, postorderList);
printf("Building binary tree from inorder and postorder list, the tree "
"topology:\n");
printf("\n");
printTreeNodeInTreeTopology(root);
printf("\n");
printf("\n");
// I do not care about freeing malloced memory, OS will take care of freeing
// heap that is part of process for this one off program.
return 0;
}