-
Notifications
You must be signed in to change notification settings - Fork 1
/
binaryTree.c
197 lines (173 loc) · 4.82 KB
/
binaryTree.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* My Binary Tree Implementation */
#include "binaryTree.h"
#include <assert.h>
struct s_btree {
int val;
MyTree* left;
MyTree* right;
};
/* CREATE A NEW TREE OBJECT */
MyTree* newTree(void)
{
MyTree* t = malloc(sizeof(*t));
t->val = -1;
t->left = t;
t->right = t;
return t;
}
/* RETURNS THE AMOUNT OF NODES IN THE TREE */
int treeSize(MyTree* tree)
{
return isEmpty(tree) ? 0 : 1 + treeSize(tree->left) + treeSize(tree->right);
}
/* ADD NODE TO THE TREE */
void addNode(MyTree* tree, int elem)
{
assert(tree);
if (isEmpty(tree)) {
tree->val = elem;
tree->left = NULL;
tree->right = NULL;
} else {
if ((!tree->left && tree->right) || (tree->left == tree->right)) {
tree->left = newTree();
addNode(tree->left, elem);
} else if (tree->left && !tree->right) {
tree->right = newTree();
addNode(tree->right, elem);
} else {
if (treeSize(tree->left) <= treeSize(tree->right))
addNode(tree->left, elem);
else
addNode(tree->right, elem);
}
}
}
/* REMOVE ALL INSTANCES OF ELEM FROM THE TREE */
// To-Do IMPROVE THIS FUNCTION
void removeNode(MyTree* tree, int elem)
{
MyTree* node = tree;
while (isNode(tree, elem)) {
node = tree;
if (treeSize(node) == 1) {
// TAKE TREE TO UNINITIALIZED STEP
node->val = -1;
node->left = node;
node->right = node;
} else {
MyTree* searcher = node;
MyTree* helper = node;
bool side = 0;
while (node->val != elem) {
side = isNode(node->left, elem);
helper = node;
node = side ? node->left : node->right;
}
searcher = node;
while (searcher->left || searcher->right) {
side = treeSize(searcher->left) >= treeSize(searcher->right);
helper = searcher;
searcher = side ? searcher->left : searcher->right;
}
if (searcher != node)
node->val = searcher->val;
if (side) {
free(helper->left);
helper->left = NULL;
} else {
free(helper->right);
helper->right = NULL;
}
}
}
}
/* CHECK IF ELEM EXISTS IN TREE */
bool isNode(MyTree* tree, int elem)
{
return isEmpty(tree)
? false
: (tree->val == elem) || isNode(tree->left, elem) || isNode(tree->right, elem);
}
/* DETERMINES IF TREE IS EMPTY */
bool isEmpty(MyTree* tree)
{
return !tree || (tree->left && (tree->left == tree->right));
}
/* HELPER FOR ORDER FUNCTIONS */
int* helper(MyTree* root, int* arr, int* i, order_t order)
{
if (root == NULL)
return NULL;
switch (order) {
case PREORDER:
arr[(*i)++] = root->val;
helper(root->left, arr, i, order);
helper(root->right, arr, i, order);
break;
case INORDER:
helper(root->left, arr, i, order);
arr[(*i)++] = root->val;
helper(root->right, arr, i, order);
break;
case POSTORDER:
helper(root->left, arr, i, order);
helper(root->right, arr, i, order);
arr[(*i)++] = root->val;
break;
default:
return NULL;
}
return arr;
}
/* ORDER TRAVERSALS
* Returned array must be freed by caller.
* PREORDER = 0, INORDER = 1, POSTORDER = 2
*/
int* orderTraversal(MyTree* tree, order_t order)
{
int treeLen = treeSize(tree);
if (treeLen == 0)
return NULL;
int* ret = malloc(treeLen * sizeof(*ret));
int i = 0;
ret = helper(tree, ret, &i, order);
return ret;
}
/* FREE ALL SPACE USED BY TREE */
void freeTree(MyTree* tree)
{
if (!tree)
return;
if (isEmpty(tree)) {
free(tree);
return;
}
MyTree* killme = tree;
freeTree(tree->left);
freeTree(tree->right);
free(killme);
}
/* PRINT TREE */
void printTree(MyTree* tree, order_t order)
{
int* a = orderTraversal(tree, order);
int size = treeSize(tree);
printf("[ ");
for (int i = 0; i < size; ++i)
printf("%d ", a[i]);
printf("]\n");
fflush(stdout);
free(a);
}
/* PRINT TREE BUT PRETTY */
void visualizeTree(MyTree* tree, unsigned int depth)
{
if (isEmpty(tree))
return;
visualizeTree(tree->right, depth + 1);
for (unsigned int i = 0; i < depth; ++i)
printf(" ");
printf("%d\n", tree->val);
visualizeTree(tree->left, depth + 1);
}