-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathiterative_inorder_traversal.c
More file actions
167 lines (142 loc) · 3.43 KB
/
iterative_inorder_traversal.c
File metadata and controls
167 lines (142 loc) · 3.43 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* iterative_inorder_traversal.c
* Iterative inorder traversal of a binary tree
*
* BONUS: print_tree() prints the binary tree prettily
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ELE 16
typedef struct _NODE NODE;
struct _NODE {
int a;
NODE *left;
NODE *right;
};
typedef enum {LEFT, RIGHT} DIRECTION;
char indent[(MAX_ELE + 1) * 2];
NODE *stack[MAX_ELE + 1] = {NULL,};
int top = 0;
/*
* construct_binary_tree
* Constructs binary tree from preorder and inorder traversal sequence
* @param preorder preorder traversal sequence
* @param inorder inorder traversal sequence
* @return root of the binary tree
*/
NODE *construct_binary_tree(int *preorder, int *inorder, int n)
{
int a;
int i;
if (n <= 0) {
return NULL;
}
a = preorder[0];
NODE *node = malloc(sizeof(NODE));
node->a = a;
node->left = NULL;
node->right = NULL;
if (n == 1) {
return node;
}
else {
for (i = 0; i < n; i++) {
if (inorder[i] == a) {
break;
}
}
node->left = construct_binary_tree(&preorder[1], inorder, i);
node->right = construct_binary_tree(&preorder[i + 1], &inorder[i + 1], n - (i + 1));
return node;
}
}
/*
* print_tree
* Prints out binary tree in a structural manner
* @param tree root of the binary tree
* @param dir left or right child
* @param indent indent string before each row
*/
void print_tree(NODE *tree, DIRECTION dir, char *indent)
{
if (tree != NULL) {
printf("%s", indent);
if (dir == LEFT) {
printf("|-");
strcat(indent, "| ");
}
else {
printf("\\-");
strcat(indent, " ");
}
printf("%d\n", tree->a);
print_tree(tree->left, LEFT, indent);
print_tree(tree->right, RIGHT, indent);
indent[strlen(indent) - 2] = '\0';
}
return;
}
void print_node(NODE *node)
{
printf("%d ", node->a);
}
/*
* _inorder_traverse_binary_tree
* @param tree root of the binary tree
*/
void _inorder_traverse_binary_tree(NODE *tree, void fn(NODE *node))
{
NODE *p = tree;
top = 0;
stack[top++] = tree;
while (top != 0 || p != NULL) {
if (p == NULL) {
p = stack[--top];
print_node(p);
if (p->right != NULL) {
stack[top++] = p->right;
}
p = p->right;
}
else {
if (p->left != NULL) {
stack[top++] = p->left;
}
p = p->left;
}
}
}
/*
* inorder_traverse_binary_tree
* Wrapper for iterative _inorder_traverse_binary_tree
*/
void inorder_traverse_binary_tree(NODE *tree, void fn(NODE *node))
{
printf("Recovered inorder traversal: ");
_inorder_traverse_binary_tree(tree, fn);
printf("\n");
}
int main()
{
int preorder[MAX_ELE + 1];
int inorder[MAX_ELE + 1];
int n;
int i;
NODE *tree;
printf("Number of elements: ");
scanf("%d", &n);
printf("Preorder sequence (max 16): ");
for (i = 0; i < n; i++) {
scanf("%d", &preorder[i]);
}
printf("Inorder sequence (max 16): ");
for (i = 0; i < n; i++) {
scanf("%d", &inorder[i]);
}
tree = construct_binary_tree(preorder, inorder, n);
indent[0] = '\0';
print_tree(tree, RIGHT, indent);
inorder_traverse_binary_tree(tree, print_node);
return 0;
}