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
|
#include "btree.h"
#include "stack.h"
#include <stdio.h>
/**
* Prints a visual representation of a binary tree
*
* @param tree The tree or subtree to print
* @param level The level in the tree that the subtree belongs to
*/
static void inspect(BTree *tree, int level) {
if (!tree)
return;
for (int i = 0; i < level; i++)
printf(" ");
printf("%2d\n", tree->data);
inspect(tree->left, level + 1);
inspect(tree->right, level + 1);
}
/**
* Initializes a new instance of a binary tree.
*
* @param data The data to bind to the root of the new subtree.
* @return Returns the new subtree.
*/
BTree *btree_init(int data) {
BTree *tree = malloc(sizeof(BTree));
tree->left = NULL;
tree->right = NULL;
tree->data = data;
return tree;
}
/**
* Performs and caches the result of a pre order traversal
* on a binary tree. Cached results are stored in the root of
* the tree and not on each node of the tree.
*
* @param tree The subtree to perform the traversal on
*/
void btree_pre_order_number(BTree *tree) {
BTree *original = tree;
if (tree == NULL)
return;
Stack *stack = stack_init();
int i = 0;
stack_push(stack, tree);
while (stack_size(stack) > 0) {
tree = stack_pop(stack);
original->pre_order[i++] = tree->data;
if (tree->right != NULL)
stack_push(stack, tree->right);
if (tree->left != NULL)
stack_push(stack, tree->left);
}
}
/**
* Performs and caches the result of an in order traversal
* on a binary tree. Cached results are stored in the root of
* the tree and not on each node of the tree.
*
* @param root The subtree to perform the traversal on
*/
void btree_in_order_number(BTree *root) {
BTree *original = root;
if (root == NULL)
return;
Stack *stack = stack_init();
int i = 0;
while (true) {
if (root) {
stack_push(stack, root);
root = root->left;
} else {
if (stack_size(stack) == 0)
break;
root = stack_pop(stack);
original->in_order[i++] = root->data;
root = root->right;
}
}
}
/**
* Performs and caches the result of an post order traversal
* on a binary tree. Cached results are stored in the root of
* the tree and not on each node of the tree.
*
* @param root The subtree to perform the traversal on
*/
void btree_post_order_number(BTree *root) {
BTree *original = root;
if (root == NULL)
return;
Stack *s1 = stack_init();
Stack *s2 = stack_init();
stack_push(s1, root);
while (stack_size(s1) > 0) {
root = stack_pop(s1);
stack_push(s2, root);
if (root->left)
stack_push(s1, root->left);
if (root->right)
stack_push(s1, root->right);
}
int i = 0;
while (stack_size(s2) > 0) {
root = stack_pop(s2);
original->post_order[i++] = root->data;
}
}
/**
* Insert a new items into a binary tree.
*
* @param tree The tree to insert the data into.
* @param data The data to insert into the tree.
* @return Returns the new root of the tree.
*/
BTree *btree_insert(BTree *tree, int data) {
if (!tree)
return btree_init(data);
if (data <= tree->data)
if (tree->left)
btree_insert(tree->left, data);
else
tree->left = btree_init(data);
else if (tree->right)
btree_insert(tree->right, data);
else
tree->right = btree_init(data);
return tree;
}
/**
* Prints a visual representation of a binary tree.
*
* @param tree The subtree to inspect.
*/
void btree_inspect(BTree *tree) { inspect(tree, 0); }
|