1 /* avl.c - routines to implement an avl tree */
3 * Copyright (c) 1993 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
14 #define DISABLE_BRIDGE
18 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
19 static char avl_version[] = "AVL library version 1.0\n";
24 #include <sys/types.h>
28 #define ROTATERIGHT(x) { \
30 if ( *(x) == NULL || (*(x))->avl_left == NULL ) {\
31 (void) printf("RR error\n"); exit(1); \
33 tmp = (*(x))->avl_left;\
34 (*(x))->avl_left = tmp->avl_right;\
35 tmp->avl_right = *(x);\
38 #define ROTATELEFT(x) { \
40 if ( *(x) == NULL || (*(x))->avl_right == NULL ) {\
41 (void) printf("RL error\n"); exit(1); \
43 tmp = (*(x))->avl_right;\
44 (*(x))->avl_right = tmp->avl_left;\
50 * ravl_insert - called from avl_insert() to do a recursive insert into
51 * and balance of an avl tree.
55 int ravl_insert( iroot, data, taller, fcmp, fdup, depth )
59 IFP fcmp; /* comparison function */
60 IFP fdup; /* function to call for duplicates */
63 int rc, cmp, tallersub;
67 if ( (*iroot = (Avlnode *) malloc( sizeof( Avlnode ) ))
71 (*iroot)->avl_left = 0;
72 (*iroot)->avl_right = 0;
74 (*iroot)->avl_data = data;
79 cmp = (*fcmp)( data, (*iroot)->avl_data );
81 /* equal - duplicate name */
84 return( (*fdup)( (*iroot)->avl_data, data ) );
89 rc = ravl_insert( &((*iroot)->avl_right), data, &tallersub,
92 switch ( (*iroot)->avl_bf ) {
93 case LH : /* left high - balance is restored */
94 (*iroot)->avl_bf = EH;
97 case EH : /* equal height - now right heavy */
98 (*iroot)->avl_bf = RH;
101 case RH : /* right heavy to start - right balance */
102 r = (*iroot)->avl_right;
103 switch ( r->avl_bf ) {
104 case LH : /* double rotation left */
106 switch ( l->avl_bf ) {
107 case LH : (*iroot)->avl_bf = EH;
110 case EH : (*iroot)->avl_bf = EH;
113 case RH : (*iroot)->avl_bf = LH;
119 (*iroot)->avl_right = r;
123 case EH : /* This should never happen */
125 case RH : /* single rotation left */
126 (*iroot)->avl_bf = EH;
140 rc = ravl_insert( &((*iroot)->avl_left), data, &tallersub,
143 switch ( (*iroot)->avl_bf ) {
144 case LH : /* left high to start - left balance */
145 l = (*iroot)->avl_left;
146 switch ( l->avl_bf ) {
147 case LH : /* single rotation right */
148 (*iroot)->avl_bf = EH;
153 case EH : /* this should never happen */
155 case RH : /* double rotation right */
157 switch ( r->avl_bf ) {
158 case LH : (*iroot)->avl_bf = RH;
161 case EH : (*iroot)->avl_bf = EH;
164 case RH : (*iroot)->avl_bf = EH;
170 (*iroot)->avl_left = l;
176 case EH : /* equal height - now left heavy */
177 (*iroot)->avl_bf = LH;
180 case RH : /* right high - balance is restored */
181 (*iroot)->avl_bf = EH;
193 * avl_insert -- insert a node containing data data into the avl tree
194 * with root root. fcmp is a function to call to compare the data portion
195 * of two nodes. it should take two arguments and return <, >, or == 0,
196 * depending on whether its first argument is <, >, or == its second
197 * argument (like strcmp, e.g.). fdup is a function to call when a duplicate
198 * node is inserted. it should return 0, or -1 and its return value
199 * will be the return value from avl_insert in the case of a duplicate node.
200 * the function will be called with the original node's data as its first
201 * argument and with the incoming duplicate node's data as its second
202 * argument. this could be used, for example, to keep a count with each
205 * NOTE: this routine may malloc memory
208 int avl_insert( root, data, fcmp, fdup )
216 return( ravl_insert( root, data, &taller, fcmp, fdup, 0 ) );
220 * right_balance() - called from delete when root's right subtree has
221 * been shortened because of a deletion.
225 right_balance( root )
231 switch( (*root)->avl_bf ) {
232 case RH: /* was right high - equal now */
233 (*root)->avl_bf = EH;
236 case EH: /* was equal - left high now */
237 (*root)->avl_bf = LH;
240 case LH: /* was right high - balance */
241 l = (*root)->avl_left;
242 switch ( l->avl_bf ) {
243 case RH : /* double rotation left */
245 switch ( r->avl_bf ) {
247 (*root)->avl_bf = EH;
251 (*root)->avl_bf = EH;
255 (*root)->avl_bf = RH;
261 (*root)->avl_left = l;
265 case EH : /* right rotation */
266 (*root)->avl_bf = LH;
271 case LH : /* single rotation right */
272 (*root)->avl_bf = EH;
285 * left_balance() - called from delete when root's left subtree has
286 * been shortened because of a deletion.
290 int left_balance( root )
296 switch( (*root)->avl_bf ) {
297 case LH: /* was left high - equal now */
298 (*root)->avl_bf = EH;
301 case EH: /* was equal - right high now */
302 (*root)->avl_bf = RH;
305 case RH: /* was right high - balance */
306 r = (*root)->avl_right;
307 switch ( r->avl_bf ) {
308 case LH : /* double rotation left */
310 switch ( l->avl_bf ) {
312 (*root)->avl_bf = EH;
316 (*root)->avl_bf = EH;
320 (*root)->avl_bf = LH;
326 (*root)->avl_right = r;
330 case EH : /* single rotation left */
331 (*root)->avl_bf = RH;
336 case RH : /* single rotation left */
337 (*root)->avl_bf = EH;
350 * ravl_delete() - called from avl_delete to do recursive deletion of a
351 * node from an avl tree. It finds the node recursively, deletes it,
352 * and returns shorter if the tree is shorter after the deletion and
357 ravl_delete( root, data, fcmp, shorter )
363 int shortersubtree = 0;
366 Avlnode *minnode, *savenode;
368 if ( *root == NULLAVL )
371 cmp = (*fcmp)( data, (*root)->avl_data );
376 savedata = savenode->avl_data;
378 /* simple cases: no left child */
379 if ( (*root)->avl_left == 0 ) {
380 *root = (*root)->avl_right;
382 free( (char *) savenode );
385 } else if ( (*root)->avl_right == 0 ) {
386 *root = (*root)->avl_left;
388 free( (char *) savenode );
393 * avl_getmin will return to us the smallest node greater
394 * than the one we are trying to delete. deleting this node
395 * from the right subtree is guaranteed to end in one of the
396 * simple cases above.
399 minnode = (*root)->avl_right;
400 while ( minnode->avl_left != NULLAVL )
401 minnode = minnode->avl_left;
404 (*root)->avl_data = minnode->avl_data;
405 minnode->avl_data = savedata;
407 savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
410 if ( shortersubtree )
411 *shorter = right_balance( root );
415 } else if ( cmp < 0 ) {
416 if ( (savedata = ravl_delete( &(*root)->avl_left, data, fcmp,
417 &shortersubtree )) == 0 ) {
422 /* left subtree shorter? */
423 if ( shortersubtree )
424 *shorter = left_balance( root );
429 if ( (savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
430 &shortersubtree )) == 0 ) {
435 if ( shortersubtree )
436 *shorter = right_balance( root );
445 * avl_delete() - deletes the node containing data (according to fcmp) from
446 * the avl tree rooted at root.
450 avl_delete( root, data, fcmp )
457 return( ravl_delete( root, data, fcmp, &shorter ) );
461 int avl_inapply( root, fn, arg, stopflag )
468 return( AVL_NOMORE );
470 if ( root->avl_left != 0 )
471 if ( avl_inapply( root->avl_left, fn, arg, stopflag )
475 if ( (*fn)( root->avl_data, arg ) == stopflag )
478 if ( root->avl_right == 0 )
479 return( AVL_NOMORE );
481 return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
485 int avl_postapply( root, fn, arg, stopflag )
492 return( AVL_NOMORE );
494 if ( root->avl_left != 0 )
495 if ( avl_postapply( root->avl_left, fn, arg, stopflag )
499 if ( root->avl_right != 0 )
500 if ( avl_postapply( root->avl_right, fn, arg, stopflag )
504 return( (*fn)( root->avl_data, arg ) );
508 int avl_preapply( root, fn, arg, stopflag )
515 return( AVL_NOMORE );
517 if ( (*fn)( root->avl_data, arg ) == stopflag )
520 if ( root->avl_left != 0 )
521 if ( avl_preapply( root->avl_left, fn, arg, stopflag )
525 if ( root->avl_right == 0 )
526 return( AVL_NOMORE );
528 return( avl_preapply( root->avl_right, fn, arg, stopflag ) );
532 * avl_apply -- avl tree root is traversed, function fn is called with
533 * arguments arg and the data portion of each node. if fn returns stopflag,
534 * the traversal is cut short, otherwise it continues. Do not use -6 as
535 * a stopflag, as this is what is used to indicate the traversal ran out
539 int avl_apply( root, fn, arg, stopflag, type )
548 return( avl_inapply( root, fn, arg, stopflag ) );
550 return( avl_preapply( root, fn, arg, stopflag ) );
552 return( avl_postapply( root, fn, arg, stopflag ) );
554 fprintf( stderr, "Invalid traversal type %d\n", type );
562 * avl_prefixapply - traverse avl tree root, applying function fprefix
563 * to any nodes that match. fcmp is called with data as its first arg
564 * and the current node's data as its second arg. it should return
565 * 0 if they match, < 0 if data is less, and > 0 if data is greater.
566 * the idea is to efficiently find all nodes that are prefixes of
567 * some key... Like avl_apply, this routine also takes a stopflag
568 * and will return prematurely if fmatch returns this value. Otherwise,
569 * AVL_NOMORE is returned.
572 int avl_prefixapply( root, data, fmatch, marg, fcmp, carg, stopflag )
584 return( AVL_NOMORE );
586 cmp = (*fcmp)( data, root->avl_data, carg );
588 if ( (*fmatch)( root->avl_data, marg ) == stopflag )
591 if ( root->avl_left != 0 )
592 if ( avl_prefixapply( root->avl_left, data, fmatch,
593 marg, fcmp, carg, stopflag ) == stopflag )
596 if ( root->avl_right != 0 )
597 return( avl_prefixapply( root->avl_right, data, fmatch,
598 marg, fcmp, carg, stopflag ) );
600 return( AVL_NOMORE );
602 } else if ( cmp < 0 ) {
603 if ( root->avl_left != 0 )
604 return( avl_prefixapply( root->avl_left, data, fmatch,
605 marg, fcmp, carg, stopflag ) );
607 if ( root->avl_right != 0 )
608 return( avl_prefixapply( root->avl_right, data, fmatch,
609 marg, fcmp, carg, stopflag ) );
612 return( AVL_NOMORE );
616 * avl_free -- traverse avltree root, freeing the memory it is using.
617 * the dfree() is called to free the data portion of each node. The
618 * number of items actually freed is returned.
621 int avl_free( root, dfree )
631 if ( root->avl_left != 0 )
632 nleft = avl_free( root->avl_left, dfree );
634 if ( root->avl_right != 0 )
635 nright = avl_free( root->avl_right, dfree );
638 (*dfree)( root->avl_data );
640 return( nleft + nright + 1 );
644 * avl_find -- search avltree root for a node with data data. the function
645 * cmp is used to compare things. it is called with data as its first arg
646 * and the current node data as its second. it should return 0 if they match,
647 * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
651 avl_find( root, data, fcmp )
658 while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
660 root = root->avl_left;
662 root = root->avl_right;
665 return( root ? root->avl_data : 0 );
669 * avl_find_lin -- search avltree root linearly for a node with data data.
670 * the function cmp is used to compare things. it is called with data as its
671 * first arg and the current node data as its second. it should return 0 if
672 * they match, non-zero otherwise.
676 avl_find_lin( root, data, fcmp )
686 if ( (*fcmp)( data, root->avl_data ) == 0 )
687 return( root->avl_data );
689 if ( root->avl_left != 0 )
690 if ( (res = avl_find_lin( root->avl_left, data, fcmp ))
694 if ( root->avl_right == 0 )
697 return( avl_find_lin( root->avl_right, data, fcmp ) );
700 static caddr_t *avl_list;
701 static int avl_maxlist;
702 static int avl_nextlist;
704 #define AVL_GRABSIZE 100
708 int avl_buildlist( data, arg )
714 if ( avl_list == (caddr_t *) 0 ) {
715 avl_list = (caddr_t *) malloc(AVL_GRABSIZE * sizeof(caddr_t));
716 slots = AVL_GRABSIZE;
718 } else if ( avl_maxlist == slots ) {
719 slots += AVL_GRABSIZE;
720 avl_list = (caddr_t *) realloc( (char *) avl_list,
721 (unsigned) slots * sizeof(caddr_t));
724 avl_list[ avl_maxlist++ ] = data;
730 * avl_getfirst() and avl_getnext() are provided as alternate tree
731 * traversal methods, to be used when a single function cannot be
732 * provided to be called with every node in the tree. avl_getfirst()
733 * traverses the tree and builds a linear list of all the nodes,
734 * returning the first node. avl_getnext() returns the next thing
735 * on the list built by avl_getfirst(). This means that avl_getfirst()
736 * can take a while, and that the tree should not be messed with while
737 * being traversed in this way, and that multiple traversals (even of
738 * different trees) cannot be active at once.
746 free( (char *) avl_list);
747 avl_list = (caddr_t *) 0;
755 (void) avl_apply( root, avl_buildlist, (caddr_t) 0, -1, AVL_INORDER );
757 return( avl_list[ avl_nextlist++ ] );
766 if ( avl_nextlist == avl_maxlist ) {
767 free( (caddr_t) avl_list);
768 avl_list = (caddr_t *) 0;
772 return( avl_list[ avl_nextlist++ ] );