forked from dale48/levawc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avltree.c
694 lines (600 loc) · 17.2 KB
/
avltree.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: avltree.c
* Author : Kyle Loudon/Dan Levin
* Date : Fri Mar 22 12:40:45 GMT 2013
* Version : 0.40
* ---
* Description: An AVL Tree - with some extensions.
*
*/
/**
* @file avltree.c
**/
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#include "avltree.h"
/**
* Macro for level separation when calling AVLTREEprint()
*
* 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
* AVLTREEprint()
**/
#define AVLTREE_PRINT_LEVEL_PADDING 4
struct AvlTreeNode_
{
void *data;
int hidden;
int factor;
struct AvlTreeNode_ *left;
struct AvlTreeNode_ *right;
};
struct AvlTree_
{
int size;
int (*compare)(const void *key1, const void *key2);
void (*destroy)(void *data);
struct AvlTreeNode_ *root;
};
/* STATIC FUNCTION DECLARATIONS */
static void rotate_left(AvlTreeNode *node);
static void rotate_right(AvlTreeNode *node);
static void destroy_left(AvlTree tree, AvlTreeNode node);
static void destroy_right(AvlTree tree, AvlTreeNode node);
static int insert(AvlTree tree, AvlTreeNode *node, const void *data, int *balanced);
static int hide(AvlTree tree, AvlTreeNode node, const void *data);
static int lookup(AvlTree tree, AvlTreeNode node, void **data);
static void print_tree(AvlTreeNode node, int level, void (*callback)(const void *data));
static void preorder(AvlTreeNode node, void (*callback)(const void *data));
static void inorder(AvlTreeNode node, void (*callback)(const void *data));
static void postorder(AvlTreeNode node, void (*callback)(const void *data));
static int treeheight(AvlTreeNode node, int depth);
static int avltree_insleft(AvlTree tree, AvlTreeNode node, const void *data);
static int avltree_insright(AvlTree tree, AvlTreeNode node, const void *data);
/* FUNCTION DEFINITIONS -------------------------------------------------------- */
AvlTree AVLTREEinit(int (*compare)(const void *key1, const void *key2), void (*destroy)(void *data))
{
AvlTree tree;
if ((tree = (AvlTree)malloc(sizeof(struct AvlTree_)))==NULL)
return NULL;
tree->size = 0;
tree->compare = compare;
tree->destroy = destroy;
tree->root = NULL;
return tree;
}
void AVLTREEdestroy(AvlTree tree)
{
destroy_left(tree, NULL);
free(tree);
}
int AVLTREEinsert(AvlTree tree, const void *data)
{
int balanced = 0;
return insert(tree, &tree->root, data, &balanced);
}
int AVLTREEremove(AvlTree tree, const void *data)
{
return hide(tree, tree->root, data);
}
int AVLTREElookup(AvlTree tree, void **data)
{
return lookup(tree, tree->root, data);
}
int AVLTREEsize(AvlTree tree)
{
return tree->size;
}
AvlTreeNode AVLTREEroot(AvlTree tree)
{
return tree->root;
}
int AVLTREEis_eob(AvlTreeNode node)
{
return node == NULL;
}
int AVLTREEis_leaf(AvlTreeNode node)
{
return node->left == NULL && node->right == NULL;
}
void *AVLTREEdata(AvlTreeNode node)
{
return node->data;
}
AvlTreeNode AVLTREEleft(AvlTreeNode node)
{
return node->left;
}
AvlTreeNode AVLTREEright(AvlTreeNode node)
{
return node->right;
}
int AVLTREEheight(AvlTree tree)
{
return treeheight(AVLTREEroot(tree), 0);
}
void AVLTREEprint(AvlTree tree, void (*callback)(const void *data))
{
/* Now - print the entire tree... */
printf("\nAVL TREE STATUS: Size(%d nodes)/Height(%d levels)/(XX=hidden) ---\n", AVLTREEsize(tree), AVLTREEheight(tree));
print_tree(AVLTREEroot(tree), 0, callback);
}
void AVLTREEpreorder(AvlTree tree, void (*callback)(const void *data))
{
preorder(tree->root, callback);
}
void AVLTREEinorder(AvlTree tree, void (*callback)(const void *data))
{
inorder(tree->root, callback);
}
void AVLTREEpostorder(AvlTree tree, void (*callback)(const void *data))
{
postorder(tree->root, callback);
}
/* STATIC FUNCTION DEFINITIONS ------------------------------------------ */
/* --- Function: static void rotate_left(AvlTreeNode *node) --- */
static void rotate_left(AvlTreeNode *node)
{
AvlTreeNode left, grandchild;
left = (*node)->left;
if (left->factor == AVL_LFT_HEAVY)
{
/* Perform an LL rotation... */
(*node)->left = left->right;
left->right = *node;
(*node)->factor = AVL_BALANCED;
left->factor = AVL_BALANCED;
*node = left;
}
else
{
/* Perform an LR rotation... */
grandchild = left->right;
left->right = grandchild->left;
grandchild->left = left;
(*node)->left = grandchild->right;
grandchild->right = *node;
switch (grandchild->factor)
{
case AVL_LFT_HEAVY:
(*node)->factor = AVL_RGT_HEAVY;
left->factor = AVL_BALANCED;
break;
case AVL_BALANCED:
(*node)->factor = AVL_BALANCED;
left->factor = AVL_BALANCED;
break;
case AVL_RGT_HEAVY:
(*node)->factor = AVL_BALANCED;
left->factor = AVL_LFT_HEAVY;
break;
}
grandchild->factor = AVL_BALANCED;
*node = grandchild;
}
}
/* --- Function: static void rotate_right(AvlTreeNode *node) --- */
static void rotate_right(AvlTreeNode *node)
{
AvlTreeNode right, grandchild;
right = (*node)->right;
if (right->factor == AVL_RGT_HEAVY)
{
/* Perform an RR rotation... */
(*node)->right = right->left;
right->left = *node;
(*node)->factor = AVL_BALANCED;
right->factor = AVL_BALANCED;
*node = right;
}
else
{
/* Perform an RL rotation... */
grandchild = right->left;
right->left = grandchild->right;
grandchild->right = right;
(*node)->right = grandchild->left;
grandchild->left = *node;
switch (grandchild->factor)
{
case AVL_LFT_HEAVY:
(*node)->factor = AVL_BALANCED;
right->factor = AVL_RGT_HEAVY;
break;
case AVL_BALANCED:
(*node)->factor = AVL_BALANCED;
right->factor = AVL_BALANCED;
break;
case AVL_RGT_HEAVY:
(*node)->factor = AVL_LFT_HEAVY;
right->factor = AVL_BALANCED;
break;
}
grandchild->factor = AVL_BALANCED;
*node = grandchild;
}
}
/* --- Function: static void destroy_left(AvlTree tree, AvlTreeNode node) --- */
static void destroy_left(AvlTree tree, AvlTreeNode node)
{
AvlTreeNode *position;
/* Destruction of an empty tree is not allowed.. */
if (tree->size == 0)
return;
/* Determine where to destroy nodes... */
if (node == NULL)
position = &tree->root;
else
position = &node->left;
/* Destroy the nodes... */
if (*position != NULL)
{
destroy_left(tree, *position);
destroy_right(tree, *position);
if (tree->destroy != NULL)
{
/* Call a user-defined function to free dynamically allocated data */
tree->destroy((*position)->data);
}
/* Now, free the node itself... */
free(*position);
*position = NULL;
/* Adjust the size of the tree to account for the destroyed node... */
tree->size--;
}
}
/* --- Function: static void destroy_right(AvlTree tree, AvlTreeNode node) --- */
static void destroy_right(AvlTree tree, AvlTreeNode node)
{
AvlTreeNode *position;
/* Destruction of an empty tree is not allowed.. */
if (tree->size == 0)
return;
/* Determine where to destroy nodes... */
if (node == NULL)
position = &tree->root;
else
position = &node->right;
/* Destroy the nodes... */
if (*position != NULL)
{
destroy_left(tree, *position);
destroy_right(tree, *position);
if (tree->destroy != NULL)
{
/* Call a user-defined function to free dynamically allocated data */
tree->destroy((*position)->data);
}
/* Now, free the node itself... */
free(*position);
*position = NULL;
/* Adjust the size of the tree to account for the destroyed node... */
tree->size--;
}
}
/* --- Function: static int insert(AvlTree tree, AvlTreeNode *node, const void *data, int *balanced) --- */
static int insert(AvlTree tree, AvlTreeNode *node, const void *data, int *balanced)
{
// AvlTreeNode avl_data;
int cmpval, retval;
/* Insert the data into the tree... */
if ((*node) == NULL)
{
return avltree_insleft(tree, *node, data);
}
else
{
/* Handle insertion into a tree that is not empty... */
cmpval = tree->compare(data, (*node)->data);
if (cmpval < 0)
{
/* Move to the left... */
if ((*node)->left == NULL)
{
if (avltree_insleft(tree, *node, data) != 0)
return -1;
*balanced = 0;
}
else
{
if ((retval = insert(tree, &(*node)->left, data, balanced)) != 0)
{
return retval;
}
}
/* Ensure that the tree remains balanced... */
if (!(*balanced))
{
switch ((*node)->factor)
{
case AVL_LFT_HEAVY:
rotate_left(node);
*balanced = 1;
break;
case AVL_BALANCED:
(*node)->factor = AVL_LFT_HEAVY;
break;
case AVL_RGT_HEAVY:
(*node)->factor = AVL_BALANCED;
*balanced = 1;
}
}
} /* if (cmpval < 0) - end */
else if (cmpval > 0)
{
/* Move to the right... */
if ((*node)->right == NULL)
{
if (avltree_insright(tree, *node, data) != 0)
return -1;
*balanced = 0;
}
else
{
if ((retval = insert(tree, &(*node)->right, data, balanced)) != 0)
{
return retval;
}
}
/* Ensure that the tree remains balanced... */
if (!(*balanced))
{
switch ((*node)->factor)
{
case AVL_LFT_HEAVY:
(*node)->factor = AVL_BALANCED;
*balanced = 1;
break;
case AVL_BALANCED:
(*node)->factor = AVL_RGT_HEAVY;
break;
case AVL_RGT_HEAVY:
rotate_right(node);
*balanced = 1;
}
}
} /* if (cmpval > 0) - end */
else
{
/* Handle finding a copy of the data... */
if (!((*node)->hidden))
{
/* Do nothing since the data is in the tree - and not hidden... */
return 1;
}
else
{
/* Insert the new data - and mark it as not hidden... */
if (tree->destroy != NULL)
{
/* Destroy the hidden data since it is being replaced.. */
tree->destroy((*node)->data);
}
(*node)->data = (void *)data;
(*node)->hidden = 0;
/* Do not rebalance because the tree structure is unchanged.. */
*balanced = 1;
}
}
}
return 0; /* Successful insertion completed! */
}
/* --- Function: static int hide(AvlTree tree, AvlTreeNode node, const void *data) --- */
static int hide(AvlTree tree, AvlTreeNode node, const void *data)
{
int cmpval, retval;
if (node == NULL)
{
/* Return that the data was not found... */
return -1;
}
cmpval = tree->compare(data, node->data);
if (cmpval < 0)
{
/* Move to the left... */
retval = hide(tree, node->left, data);
}
else if (cmpval > 0)
{
/* Move to the right... */
retval = hide(tree, node->right, data);
}
else /* Node found - hidden or not..! */
{
if (!(node->hidden))
{
/* Mark the node as hidden... */
node->hidden = 1;
/* Return success... */
retval = 0;
}
else
return -1;
}
return retval;
}
/* --- Function: static int lookup(AvlTree tree, AvlTreeNode node, void **data) --- */
static int lookup(AvlTree tree, AvlTreeNode node, void **data)
{
int cmpval, retval;
if (node == NULL)
{
/* Return that the data was not found... */
return -1;
}
cmpval = tree->compare(*data, node->data);
if (cmpval < 0)
{
/* Move to the left... */
retval = lookup(tree, node->left, data);
}
else if (cmpval > 0)
{
/* Move to the right... */
retval = lookup(tree, node->right, data);
}
else
{
/* Node found - or hidden..! */
if (!(node->hidden) )
{
/* Pass back the data from the tree... */
*data = node->data;
retval = 0;
}
else
{
/* Return that the data was not found! */
return -1;
}
}
return retval;
}
static void print_tree(AvlTreeNode node, int level, void (*callback)(const void *data))
{
char *p_msk;
/* Print current element data */
p_msk = (char *)malloc((AVLTREE_PRINT_LEVEL_PADDING*level+1)*sizeof(char));
assert(p_msk);
memset(p_msk, '-', AVLTREE_PRINT_LEVEL_PADDING*level);
p_msk[AVLTREE_PRINT_LEVEL_PADDING*level] = '\0';
printf("%s", p_msk);
free(p_msk);
/* Recursion condition */
if (AVLTREEis_eob(node))
{
printf("NIL");
printf("\n");
return;
}
else
{
if (node->hidden)
printf(" XX");
else
callback(AVLTREEdata(node));
printf("\n");
}
/* Recursively traverse and print both "subtrees"... */
print_tree(AVLTREEleft(node), level+1, callback);
print_tree(AVLTREEright(node), level+1, callback);
}
/* --- Function: void preorder(AvlTreeNode node, void (*callback)(const void *data)) --- */
static void preorder(AvlTreeNode 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(AvlTreeNode node, void (*callback)(const void *data)) --- */
static void inorder(AvlTreeNode 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(AvlTreeNode node, void (*callback)(const void *data)) --- */
static void postorder(AvlTreeNode 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(AvlTreeNode node, int depth) --- */
static int treeheight(AvlTreeNode node, int depth)
{
if (!node)
return depth;
else
return maxval(treeheight(node->left, depth+1),
treeheight(node->right, depth+1));
}
static int avltree_insleft(AvlTree tree, AvlTreeNode node, const void *data)
{
AvlTreeNode new_node, *position;
/* Determine where to insert the node... */
if (node == NULL)
{
/* Allow insertion at the root only in an empty tree */
if (tree->size > 0)
return -1;
position = &tree->root;
}
else
{
/* Normally allow insertion only at the end of a branch */
if (node->left != NULL)
return -1;
position = &node->left;
}
/* Allocate storage for the node */
if ((new_node = (AvlTreeNode)malloc(sizeof(struct AvlTreeNode_))) == NULL)
return -1;
/* Insert the node into the tree */
new_node->data = (void *)data;
new_node->factor = AVL_BALANCED;
new_node->hidden = 0;
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;
}
static int avltree_insright(AvlTree tree, AvlTreeNode node, const void *data)
{
AvlTreeNode new_node, *position;
/* Determine where to insert the node... */
if (node == NULL)
{
/* Allow insertion at the root only in an empty tree */
if (tree->size > 0)
return -1;
position = &tree->root;
}
else
{
/* Normally allow insertion only at the end of a branch */
if (node->right != NULL)
return -1;
position = &node->right;
}
/* Allocate storage for the node */
if ((new_node = (AvlTreeNode)malloc(sizeof(struct AvlTreeNode_))) == NULL)
return -1;
/* Insert the node into the tree */
new_node->data = (void *)data;
new_node->factor = AVL_BALANCED;
new_node->hidden = 0;
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;
}