-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.c
323 lines (284 loc) · 7.45 KB
/
bst.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <stdio.h>
#include <stdlib.h>
#include "debug.h"
#include "bst.h"
/* helper functions for tree operations */
bool insert_helper (struct bst_node** node, struct bst_node* parent, void* data,
bst_tree_compare_func* func);
struct bst_node* find_helper (struct bst_node*, const void*, bst_tree_compare_func*);
void dump_helper (struct bst_node* node, unsigned, bst_tree_dump_func*);
void dump_node (struct bst_node* node, unsigned level);
void
bst_init (struct bst_tree* tree, bst_tree_compare_func* func)
{
if (!tree || !func) return;
tree->root = NULL;
tree->comparator = func;
};
bool
bst_insert (struct bst_tree* tree, void* data)
{
if (!data) return false;
return insert_helper (&(tree->root), NULL, data, tree->comparator);
};
void*
bst_delete (struct bst_tree* tree, void* data)
{
if (!data || !tree)
return NULL;
struct bst_node* del = bst_find (tree, data);
/* Case 1: Node is a leaf */
if (del->left == NULL && del->right == NULL)
{
if (!del->parent) tree->root = NULL;
else if (del->parent->right == del) del->parent->right = NULL;
else del->parent->left = NULL;
}
/* Case 2: Node has one child */
else if (del->left)
{
if (del->parent == NULL) tree->root = del->left;
else if (del->parent->right == del) del->parent->right = del->left;
else del->parent->left = del->left;
}
else if (del->right)
{
if (del->parent == NULL) tree->root = del->right;
else if (del->parent->right == del) del->parent->right = del->right;
else del->parent->left = del->left;
}
/* Case 3: Node has both children */
else
{
ASSERT (del->left);
ASSERT (del->right);
struct bst_node* right = del->right;
/* Find the leftmost child */
struct bst_node* n = right;
while (n->left)
n = n->left;
ASSERT (n->left == NULL);
if (n->right)
{
n->parent->left = n->right;
n->right->parent = n->parent;
}
n->right = del->right;
n->left = del->left;
n->parent = del->parent;
if (del->parent == NULL) tree->root = n;
else if (del->parent->right == del) del->parent->right = n;
else del->parent->left = n;
}
void* ret = del->data;
free (del);
return ret;
};
void*
bst_find (struct bst_tree* tree, const void* elem)
{
ASSERT (tree != NULL);
if (elem == NULL) return NULL;
struct bst_node* node = find_helper (tree->root, elem, tree->comparator);
if (node == NULL)
return NULL;
return node->data;
};
void
bst_dump (struct bst_tree* tree, bst_tree_dump_func* func)
{
dump_helper (tree->root, 0, func);
};
/*******
Iterator Functions
*********/
struct bst_iterator*
bst_get_iterator (struct bst_tree* tree)
{
if (tree == NULL || tree->root == NULL) return NULL;
struct bst_iterator* it = malloc (sizeof (struct bst_iterator));
if (it == NULL)
return NULL;
it->node = tree->root;
it->tree = tree;
/* Go to the smallest element in the tree... The left-most. */
while (it->node->left)
it->node = it->node->left;
return it;
};
struct bst_iterator*
bst_get_reverse_iterator (struct bst_tree* tree)
{
if (tree == NULL) return NULL;
struct bst_iterator* it = malloc (sizeof (struct bst_iterator));
if (it == NULL) return NULL;
it->node = tree->root;
it->tree = tree;
/* Go to the largest element in the tree... The right-most. */
while (it->node->right)
it->node = it->node->right;
return it;
};
void*
bst_get (struct bst_iterator* it)
{
if (it == NULL) return NULL;
return it->node->data;
};
void
bst_itr_remove (struct bst_iterator* it)
{
if (it == NULL) return;
struct bst_node* node = it->node;
bst_next (it);
bst_delete (it->tree, node->data);
};
void*
bst_next (struct bst_iterator* it)
{
if (it == NULL) return NULL;
if (it->node->right)
{
it->node = it->node->right;
while (it->node->left)
it->node = it->node->left;
}
else if (it->node->parent)
{
/* We need to go up the tree. */
if (it->node == it->node->parent->left)
it->node = it->node->parent;
else
{
while (it->node == it->node->parent->right)
{
it->node = it->node->parent;
if (!it->node->parent)
return NULL;
}
it->node = it->node->parent;
}
}
else
{
return NULL;
}
return it->node->data;
};
void*
bst_prev (struct bst_iterator* it)
{
if (it == NULL) return NULL;
if (it->node->left)
{
it->node = it->node->left;
while (it->node->right)
it->node = it->node->right;
}
else if (it->node->parent)
{
if (it->node == it->node->parent->right)
it->node = it->node->parent;
else
{
while (it->node == it->node->parent->left)
{
it->node = it->node->parent;
if (!it->node->parent)
return NULL;
}
it->node = it->node->parent;
}
}
else
{
return NULL;
}
return it->node->data;
};
/* === HELPER FUNCTIONS === */
bool
insert_helper (struct bst_node** node, struct bst_node* parent, void* data,
bst_tree_compare_func* func)
{
ASSERT (node != NULL);
ASSERT (data != NULL);
struct bst_node* new_node = NULL;
if (*node == NULL)
{
new_node = malloc (sizeof (struct bst_node));
new_node->left = NULL;
new_node->right = NULL;
new_node->parent = parent;
new_node->data = data;
(*node) = new_node;
return true;
}
else
{
new_node = (*node);
ASSERT (new_node->data != NULL);
int compare_result = func (data, (*node)->data, NULL);
if (compare_result > 0)
{
return insert_helper (&(*node)->right, *node, data, func);
}
else if (compare_result < 0)
{
return insert_helper (&(*node)->left, *node, data, func);
}
else
{
return false;
}
}
};
struct bst_node*
find_helper (struct bst_node* node, const void* elem,
bst_tree_compare_func* func)
{
ASSERT (elem != NULL);
if (node == NULL) return NULL;
ASSERT (node->data != NULL);
int cmp = func (elem, node->data, NULL);
if (cmp > 0)
{
return find_helper (node->right, elem, func);
}
else if (cmp < 0)
{
return find_helper (node->left, elem, func);
}
else
{
return node;
}
};
void
dump_helper (struct bst_node* node, unsigned level,
bst_tree_dump_func* func)
{
if (node)
{
dump_helper (node->left, level + 1, func);
unsigned i = 0;
for (i = 0; i < level; i++)
{
printf (" ");
}
if (node->data)
{
func (node->data);
}
else
{
printf ("NULL");
}
printf ("\n");
dump_helper (node->right, level + 1, func);
}
};
void
dump_node (struct bst_node* node, unsigned level)
{
};