-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.c
More file actions
357 lines (267 loc) · 6.84 KB
/
binary.c
File metadata and controls
357 lines (267 loc) · 6.84 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h"
#define valid_tree_type(t) \
(t) >= inorder && (t) < invalid_transversal
#define GET_PRIV(t) \
(node_t *)((t)->priv)
struct node {
void *value;
node_t *child1;
node_t *child2;
};
/*
* node struct function definitions
*/
static node_t *create_node(tree_t *tree, void *value) {
node_t *node = (node_t *)malloc(sizeof(node_t));
node->value = value;
node->child1 = NULL;
node->child2 = NULL;
return node;
}
static void destroy_node(node_t *node) {
if(node == NULL) {
return;
}
if(node->child1 != NULL) {
destroy_node(node->child1);
}
if(node->child2 != NULL) {
destroy_node(node->child2);
}
free(node);
}
/*
* binary tree function definitions
*/
int binary_destroy(tree_t *tree) {
//destroy_node function traverses through tree to destroy each node
destroy_node(GET_PRIV(tree));
return 0;
}
static node_t *binary_insert_impl(tree_t *tree, node_t *check, void *value, bool *present) {
if(check == NULL) {
return create_node(tree, value);
}
int comp = tree->compare_fp(check->value, value);
switch(comp) {
case 0:
printf("Inserted value already present in tree\n");
*present = true;
break;
case 1:
check->child1 = binary_insert_impl(tree, check->child1, value, present);
break;
case -1:
check->child2 = binary_insert_impl(tree, check->child2, value, present);
break;
default:
fprintf(stderr, "Compare function returns invalid value\n");
break;
}
return check;
}
int binary_insert(tree_t *tree, void *value) {
bool present = false;
tree->priv = binary_insert_impl(tree, GET_PRIV(tree), value, &present);
if(!present) {
tree->num_elements += 1;
return 0;
}
return 2;
}
static node_t *find_left_leaf(node_t *node) {
if(node == NULL) {
return NULL;
}
if(node->child1 == NULL) {
return node;
} else return find_left_leaf(node->child1);
}
node_t *binary_remove_impl(tree_t *tree, node_t *check, void *value, bool *present) {
if(check == NULL) {
return NULL;
}
int comp = tree->compare_fp(check->value, value);
switch(comp) {
case 0:
*present = true;
//node to be removed has no children
if(check->child1 == NULL && check->child2 == NULL) {
destroy_node(check);
return NULL;
//node to be removed has one child
} else if((check->child1 != NULL) ^ (check->child2 != NULL)) {
//case for left child
if(check->child1 != NULL) {
node_t *temp = check->child1;
check->child1 = NULL;
destroy_node(check);
return temp;
//case for right child
} else {
node_t *temp = check->child2;
check->child2 = NULL;
destroy_node(check);
return temp;
}
//node to be removed has two children
} else {
node_t *leaf = find_left_leaf(check->child2);
check->value = leaf->value;
check->child2 = binary_remove_impl(tree, check->child2, check->value, present);
return check;
}
break;
case 1:
//left child
check->child1 = binary_remove_impl(tree, check->child1, value, present);
break;
case -1:
//right child
check->child2 = binary_remove_impl(tree, check->child2, value, present);
break;
default:
fprintf(stderr, "Compare function returns invalid value\n");
break;
}
return check;
}
void *binary_remove(tree_t *tree, void *value) {
bool present = false;
tree->priv = binary_remove_impl(tree, GET_PRIV(tree), value, &present);
if(present) {
tree->num_elements -= 1;
return value;
}
return NULL;
}
void *binary_pop(tree_t *tree) {
if(tree->num_elements == 0) {
fprintf(stderr, "Cannot pop empty tree\n");
return NULL;
}
void *retval = (GET_PRIV(tree))->value;
bool present = false;
binary_remove_impl(tree, GET_PRIV(tree), (GET_PRIV(tree))->value, &present);
tree->num_elements -= 1;
return retval;
}
int binary_depth_impl(node_t *node) {
if(node == NULL) {
return 0;
}
int left = binary_depth_impl(node->child1);
int right = binary_depth_impl(node->child2);
if(left > right) {
return left + 1;
}
return right + 1;
}
int binary_depth(tree_t *tree) {
return binary_depth_impl(GET_PRIV(tree));
}
bool binary_present_impl(tree_t *tree, node_t *check, void *value) {
int comp = tree->compare_fp(check->value, value);
switch(comp) {
case 0:
return true;
break;
case 1:
if(check->child1 == NULL) {
return false;
}
return binary_present_impl(tree, check->child1, value);
break;
case -1:
return false;
break;
default:
fprintf(stderr, "Compare function returns invalid value\n");
break;
}
return binary_present_impl(tree, check->child2, value);
}
bool binary_present(tree_t *tree, void *value) {
if(tree->num_elements == 0) {
return false;
}
return binary_present_impl(tree, GET_PRIV(tree), value);
}
static void print_tree_inorder(tree_t *tree, node_t *node) {
if(node == NULL) {
printf("NULL\n");
}
if(node->child1 != NULL) {
print_tree_inorder(tree, node->child1);
}
tree->print_fp(node->value);
if(node->child2 != NULL) {
print_tree_inorder(tree, node->child2);
}
return;
}
static void print_tree_preorder(tree_t *tree, node_t *node) {
if(node == NULL) {
printf("NULL\n");
}
tree->print_fp(node->value);
if(node->child1 != NULL) {
print_tree_preorder(tree, node->child1);
}
if(node->child2 != NULL) {
print_tree_preorder(tree, node->child2);
}
return;
}
static void print_tree_postorder(tree_t *tree, node_t *node) {
if(node == NULL) {
printf("NULL\n");
}
if(node->child1 != NULL) {
print_tree_postorder(tree, node->child1);
}
if(node->child2 != NULL) {
print_tree_postorder(tree, node->child2);
}
tree->print_fp(node->value);
return;
}
void binary_print(tree_t *tree, transversal_e transversal) {
//check for valid transversal type
if(!(valid_tree_type(transversal))) {
fprintf(stderr, "Invalid transversal in print_tree function\n");
return;
}
//check for empty tree
if(GET_PRIV(tree) == NULL) {
printf("NULL\n");
return;
}
switch(transversal) {
case inorder:
print_tree_inorder(tree, GET_PRIV(tree));
break;
case preorder:
print_tree_preorder(tree, GET_PRIV(tree));
break;
case postorder:
print_tree_postorder(tree, GET_PRIV(tree));
break;
default:
fprintf(stderr, "Invalid transversal in print_tree function\n");
return;
}
}
tree_ops_t binary_ops = {
.create = NULL,
.destroy = binary_destroy,
.insert = binary_insert,
.remove = binary_remove,
.pop = binary_pop,
.depth = binary_depth,
.present = binary_present,
.print = binary_print
};