forked from dale48/levawc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitree.c
544 lines (449 loc) · 13.1 KB
/
bitree.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: bitree.c
* Author : Kyle Loudon/Dan Levin
* Date : Fri Mar 22 12:40:45 GMT 2013
* Version : 0.40
* ---
* Description : A basic, miminal, binary tree ADT - written in ANSI C.
*
* Date Revision message
* 2013-02-17 Created this file
*
*/
/**
* @file bitree.c
**/
#include <stdio.h>
#include <stdlib.h>
#include "bitree.h"
/**
* Macro for level separation when calling BITREEprint()
*
* This macro sets the distance - measured in column
* positions - between node levels of the tree when
* it is printed on screen as a result of a call to
* BITREEprint()
**/
#define BITREE_PRINT_LEVEL_PADDING 4
struct BiTreeNode_
{
void *data;
struct BiTreeNode_ *left;
struct BiTreeNode_ *right;
};
struct BiTree_
{
int size;
int (*compare)(const void *key1, const void *key2);
void (*destroy)(void *data);
struct BiTreeNode_ *root;
};
/* STATIC FUNCTION DECLARATIONS */
static BiTreeNode remnode(BiTree tree, BiTreeNode node, void **data);
static int treeheight(BiTreeNode node, int depth);
static void print_tree(BiTreeNode node, int level, void (*callback)(const void *data));
static void preorder(BiTreeNode node, void (*callback)(const void *data));
static void inorder(BiTreeNode node, void (*callback)(const void *data));
static void postorder(BiTreeNode node, void (*callback)(const void *data));
/* FUNCTION DEFINITIONS -------------------------------------------------------- */
BiTree BITREEinit(void (*destroy)(void *data))
{
BiTree tree;
if ((tree = (BiTree)malloc(sizeof(struct BiTree_)))==NULL)
return NULL;
tree->size = 0;
tree->destroy = destroy;
tree->root = NULL;
return tree;
}
void BITREEdestroy(BiTree tree)
{
/* Remove all nodes */
BITREEremleft(tree, NULL);
/* Remove tree header */
free(tree);
}
void BITREEsetcompare(BiTree tree, int (*compare)(const void *key1, const void *key2))
{
tree->compare = compare;
}
int BITREEinsleft(BiTree tree, BiTreeNode node, const void *data)
{
BiTreeNode new_node, *position;
/* Determine where to insert the node... */
if (node == NULL)
{
/* Allow insertion at the root only in an empty tree */
if (BITREEsize(tree) > 0)
return -1;
position = &tree->root;
}
else
{
/* Normally allow insertion only at the end of a branch */
if (BITREEleft(node) != NULL)
return -1;
position = &node->left;
}
/* Allocate storage for the node */
if ((new_node = (BiTreeNode)malloc(sizeof(struct BiTreeNode_))) == NULL)
return -1;
/* Insert the node into the tree */
new_node->data = (void *)data;
new_node->left = NULL;
new_node->right = NULL;
*position = new_node;
/* Adjust the size of the tree to account for the inserted node */
tree->size++;
return 0;
}
int BITREEinsright(BiTree tree, BiTreeNode node, const void *data)
{
BiTreeNode new_node, *position;
/* Determine where to insert the node... */
if (node == NULL)
{
/* Allow insertion at the root only in an empty tree */
if (BITREEsize(tree) > 0)
return -1;
position = &tree->root;
}
else
{
/* Normally allow insertion only at the end of a branch */
if (BITREEright(node) != NULL)
return -1;
position = &node->right;
}
/* Allocate storage for the node */
if ((new_node = (BiTreeNode)malloc(sizeof(struct BiTreeNode_))) == NULL)
return -1;
/* Insert the node into the tree */
new_node->data = (void *)data;
new_node->left = NULL;
new_node->right = NULL;
*position = new_node;
/* Adjust the size of the tree to account for the inserted node */
tree->size++;
return 0;
}
int BITREEinsert(BiTree tree, const void *data)
{
BiTreeNode node, prev;
int direction, cmpval;
if (!tree->compare) /* User-defined compare-callback NOT set... */
return -2;
node = BITREEroot(tree); /* Start at the root... */
direction = 0;
while (!BITREEis_eob(node))
{
prev = node;
cmpval = tree->compare(data, BITREEdata(node));
if (cmpval == 0) /* Node found - already in the tree! */
return 1; /* Tell caller, that node is already present... */
else
if (cmpval < 0) /* Go to the left... */
{
node = BITREEleft(node);
direction = 1;
}
else /* Go to the right... */
{
node = BITREEright(node);
direction = 2;
}
}
if (direction == 0) /* Insert a root node in an empty tree... */
return BITREEinsleft(tree, NULL, data);
if (direction == 1) /* Insert a new left subnode... */
return BITREEinsleft(tree, prev, data);
if (direction == 2) /* Insert a new right subnode... */
return BITREEinsright(tree, prev, data);
return -1; /* Should not get here... */
}
int BITREElookup(BiTree tree, void **data)
{
BiTreeNode node;
int cmpval;
if (!tree->compare) /* User-defined compare-callback NOT set... */
return -2;
node = BITREEroot(tree); /* Start at the root... */
while (!BITREEis_eob(node))
{
cmpval = tree->compare(*data, BITREEdata(node));
if (cmpval == 0) /* Node found! */
{
*data = BITREEdata(node);
return 0;
}
else
if (cmpval < 0)
node = BITREEleft(node);
else
node = BITREEright(node);
}
return -1; /* Node not found... */
}
int BITREEremove(BiTree tree, void **data)
{
int tmp;
if ((tmp = BITREElookup(tree, data)) != 0)
return tmp;
else
tree->root = remnode(tree, tree->root, data);
return 0; /* Node successfully removed */
}
void BITREEremleft(BiTree tree, BiTreeNode node)
{
BiTreeNode *position;
/* Do not allow removal from an empty tree... */
if (BITREEsize(tree) == 0)
return;
/* Determine where to remove nodes... */
if (node == NULL)
position = &tree->root;
else
position = &node->left;
/* Remove the nodes... */
if (*position != NULL)
{
BITREEremleft(tree, *position);
BITREEremright(tree, *position);
if (tree->destroy != NULL)
{
/* Call a user-defined function to free dynamically allocated data */
tree->destroy((*position)->data);
}
free(*position);
*position = NULL;
/* Adjust the size of the tree to account for the removed node */
tree->size--;
}
}
void BITREEremright(BiTree tree, BiTreeNode node)
{
BiTreeNode *position;
/* Do not allow removal from an empty tree... */
if (BITREEsize(tree) == 0)
return;
/* Determine where to remove nodes... */
if (node == NULL)
position = &tree->root;
else
position = &node->right;
/* Remove the nodes... */
if (*position != NULL)
{
BITREEremleft(tree, *position);
BITREEremright(tree, *position);
if (tree->destroy != NULL)
{
/* Call a user-defined function to free dynamically allocated data */
tree->destroy((*position)->data);
}
free(*position);
*position = NULL;
/* Adjust the size of the tree to account for the removed node */
tree->size--;
}
}
BiTree BITREEmerge(BiTree left_tree, BiTree right_tree, const void *data)
{
BiTree merge_tree;
BiTreeNode node;
/* Initialize the merged tree */
if ((merge_tree = BITREEinit(left_tree->destroy)) == NULL)
return NULL;
/* Insert the data for the root node of the merged tree */
if (BITREEinsleft(merge_tree, NULL, data) != 0)
{
BITREEdestroy(merge_tree);
return NULL;
}
/* Merge the two binary trees into a single binary tree */
node = BITREEroot(merge_tree);
node->left = BITREEroot(left_tree);
node->right = BITREEroot(right_tree);
/* Adjust the size of the new binary tree */
merge_tree->size = merge_tree->size + BITREEsize(left_tree) + BITREEsize(right_tree);
/* Do not let the original trees access the merged nodes */
left_tree->root = NULL;
left_tree->size = 0;
right_tree->root = NULL;
right_tree->size = 0;
return merge_tree;
}
int BITREEsize(BiTree tree)
{
return tree->size;
}
BiTreeNode BITREEroot(BiTree tree)
{
return tree->root;
}
int BITREEis_eob(BiTreeNode node)
{
return node == NULL;
}
int BITREEis_leaf(BiTreeNode node)
{
return node->left == NULL && node->right == NULL;
}
void *BITREEdata(BiTreeNode node)
{
return node->data;
}
BiTreeNode BITREEleft(BiTreeNode node)
{
return node->left;
}
BiTreeNode BITREEright(BiTreeNode node)
{
return node->right;
}
int BITREEheight(BiTree tree)
{
return treeheight(BITREEroot(tree), 0);
}
void BITREEprint(BiTree tree, void (*callback)(const void *data))
{
/* Now - print the entire tree... */
printf("\nTREE STATUS: Size(%d nodes)/Height(%d levels) ---\n", BITREEsize(tree), BITREEheight(tree));
print_tree(BITREEroot(tree), 0, callback);
}
void BITREEpreorder(BiTree tree, void (*callback)(const void *data))
{
preorder(tree->root, callback);
}
void BITREEinorder(BiTree tree, void (*callback)(const void *data))
{
inorder(tree->root, callback);
}
void BITREEpostorder(BiTree tree, void (*callback)(const void *data))
{
postorder(tree->root, callback);
}
/* STATIC FUNCTION DEFINITIONS ------------------------------------------ */
static BiTreeNode remnode(BiTree tree, BiTreeNode node,
void **data)
{
BiTreeNode ptmp;
void *datatmp;
int cmpval;
if (node != NULL)
{
/* Compare data */
cmpval = tree->compare(*data, node->data);
if(cmpval < 0)
node->left = remnode(tree, node->left, data);
else if (cmpval > 0)
node->right = remnode(tree, node->right, data);
else if(node->left && node->right) /* Node to be deleted was found - and has 2 children..! */
{
ptmp = node->right;
/* Search for "smallest" node in right subtree - i.e. turn constantly left.. */
while (ptmp->left)
ptmp = ptmp->left;
/* Swap data */
datatmp = node->data;
node->data = ptmp->data;
ptmp->data = datatmp;
/* Find and remove the matching node (recursively).. */
node->right = remnode(tree, node->right, data);
}
else /* Matching node has less than 2 children.. */
{
ptmp = node;
assert(ptmp);
/* Link over node 'ptmp' */
if (node->left == NULL)
node = node->right;
else if (node->right == NULL)
node = node->left;
/* Hand node data back to caller */
assert(ptmp->data);
*data = ptmp->data;
free(ptmp);
tree->size--;
}
}
return node;
}
/* --- Function: static void print_tree(BiTree tree, BiTreeNode node, int level, void (*callback)(const void *data)) --- */
static void print_tree(BiTreeNode node, int level, void (*callback)(const void *data))
{
char *p_msk;
/* Print current element data */
p_msk = (char *)malloc((BITREE_PRINT_LEVEL_PADDING*level+1)*sizeof(char));
assert(p_msk);
memset(p_msk, '-', BITREE_PRINT_LEVEL_PADDING*level);
p_msk[BITREE_PRINT_LEVEL_PADDING*level] = '\0';
printf("%s", p_msk);
free(p_msk);
/* Recursion condition */
if (BITREEis_eob(node))
{
printf("NIL");
printf("\n");
return;
}
else
callback(BITREEdata(node));
printf("\n");
/* Recursively traverse and print both "subtrees"... */
print_tree(BITREEleft(node), level+1, callback);
print_tree(BITREEright(node), level+1, callback);
}
/* --- Function: void preorder(BiTreeNode node, void (*callback)(const void *data)) --- */
static void preorder(BiTreeNode node, void (*callback)(const void *data))
{
if (node)
{
/* Handle current node... */
callback(node->data);
/* Traverse left subtree - recursively in preorder... */
preorder(node->left, callback);
/* Traverse right subtree - recursively in preorder... */
preorder(node->right, callback);
}
}
/* --- Function: void inorder(BiTreeNode node, void (*callback)(const void *data)) --- */
static void inorder(BiTreeNode node, void (*callback)(const void *data))
{
if (node)
{
/* Traverse left subtree - recursively in inorder... */
inorder(node->left, callback);
/* Handle current node... */
callback(node->data);
/* Traverse right subtree - recursively in inorder... */
inorder(node->right, callback);
}
}
/* --- Function: void postorder(BiTreeNode node, void (*callback)(const void *data)) --- */
static void postorder(BiTreeNode node, void (*callback)(const void *data))
{
if (node)
{
/* Traverse left subtree - recursively in postorder... */
postorder(node->left, callback);
/* Traverse right subtree - recursively in postorder... */
postorder(node->right, callback);
/* Handle current node... */
callback(node->data);
}
}
/* --- Function: int treeheight(BiTreeNode node, int depth) --- */
static int treeheight(BiTreeNode node, int depth)
{
if (!node)
return depth;
else
return maxval(treeheight(node->left, depth+1),
treeheight(node->right, depth+1));
}