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.
17 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
18 static char avl_version[] = "AVL library version 1.0\n";
23 #include <sys/types.h>
27 #define ROTATERIGHT(x) { \
29 if ( *(x) == NULL || (*(x))->avl_left == NULL ) {\
30 (void) printf("RR error\n"); exit(1); \
32 tmp = (*(x))->avl_left;\
33 (*(x))->avl_left = tmp->avl_right;\
34 tmp->avl_right = *(x);\
37 #define ROTATELEFT(x) { \
39 if ( *(x) == NULL || (*(x))->avl_right == NULL ) {\
40 (void) printf("RL error\n"); exit(1); \
42 tmp = (*(x))->avl_right;\
43 (*(x))->avl_right = tmp->avl_left;\
49 * ravl_insert - called from avl_insert() to do a recursive insert into
50 * and balance of an avl tree.
54 int ravl_insert( iroot, data, taller, fcmp, fdup, depth )
58 IFP fcmp; /* comparison function */
59 IFP fdup; /* function to call for duplicates */
62 int rc, cmp, tallersub;
66 if ( (*iroot = (Avlnode *) malloc( sizeof( Avlnode ) ))
70 (*iroot)->avl_left = 0;
71 (*iroot)->avl_right = 0;
73 (*iroot)->avl_data = data;
78 cmp = (*fcmp)( data, (*iroot)->avl_data );
80 /* equal - duplicate name */
83 return( (*fdup)( (*iroot)->avl_data, data ) );
88 rc = ravl_insert( &((*iroot)->avl_right), data, &tallersub,
91 switch ( (*iroot)->avl_bf ) {
92 case LH : /* left high - balance is restored */
93 (*iroot)->avl_bf = EH;
96 case EH : /* equal height - now right heavy */
97 (*iroot)->avl_bf = RH;
100 case RH : /* right heavy to start - right balance */
101 r = (*iroot)->avl_right;
102 switch ( r->avl_bf ) {
103 case LH : /* double rotation left */
105 switch ( l->avl_bf ) {
106 case LH : (*iroot)->avl_bf = EH;
109 case EH : (*iroot)->avl_bf = EH;
112 case RH : (*iroot)->avl_bf = LH;
118 (*iroot)->avl_right = r;
122 case EH : /* This should never happen */
124 case RH : /* single rotation left */
125 (*iroot)->avl_bf = EH;
139 rc = ravl_insert( &((*iroot)->avl_left), data, &tallersub,
142 switch ( (*iroot)->avl_bf ) {
143 case LH : /* left high to start - left balance */
144 l = (*iroot)->avl_left;
145 switch ( l->avl_bf ) {
146 case LH : /* single rotation right */
147 (*iroot)->avl_bf = EH;
152 case EH : /* this should never happen */
154 case RH : /* double rotation right */
156 switch ( r->avl_bf ) {
157 case LH : (*iroot)->avl_bf = RH;
160 case EH : (*iroot)->avl_bf = EH;
163 case RH : (*iroot)->avl_bf = EH;
169 (*iroot)->avl_left = l;
175 case EH : /* equal height - now left heavy */
176 (*iroot)->avl_bf = LH;
179 case RH : /* right high - balance is restored */
180 (*iroot)->avl_bf = EH;
192 * avl_insert -- insert a node containing data data into the avl tree
193 * with root root. fcmp is a function to call to compare the data portion
194 * of two nodes. it should take two arguments and return <, >, or == 0,
195 * depending on whether its first argument is <, >, or == its second
196 * argument (like strcmp, e.g.). fdup is a function to call when a duplicate
197 * node is inserted. it should return 0, or -1 and its return value
198 * will be the return value from avl_insert in the case of a duplicate node.
199 * the function will be called with the original node's data as its first
200 * argument and with the incoming duplicate node's data as its second
201 * argument. this could be used, for example, to keep a count with each
204 * NOTE: this routine may malloc memory
207 int avl_insert( root, data, fcmp, fdup )
215 return( ravl_insert( root, data, &taller, fcmp, fdup, 0 ) );
219 * right_balance() - called from delete when root's right subtree has
220 * been shortened because of a deletion.
224 right_balance( root )
230 switch( (*root)->avl_bf ) {
231 case RH: /* was right high - equal now */
232 (*root)->avl_bf = EH;
235 case EH: /* was equal - left high now */
236 (*root)->avl_bf = LH;
239 case LH: /* was right high - balance */
240 l = (*root)->avl_left;
241 switch ( l->avl_bf ) {
242 case RH : /* double rotation left */
244 switch ( r->avl_bf ) {
246 (*root)->avl_bf = EH;
250 (*root)->avl_bf = EH;
254 (*root)->avl_bf = RH;
260 (*root)->avl_left = l;
264 case EH : /* right rotation */
265 (*root)->avl_bf = LH;
270 case LH : /* single rotation right */
271 (*root)->avl_bf = EH;
284 * left_balance() - called from delete when root's left subtree has
285 * been shortened because of a deletion.
289 int left_balance( root )
295 switch( (*root)->avl_bf ) {
296 case LH: /* was left high - equal now */
297 (*root)->avl_bf = EH;
300 case EH: /* was equal - right high now */
301 (*root)->avl_bf = RH;
304 case RH: /* was right high - balance */
305 r = (*root)->avl_right;
306 switch ( r->avl_bf ) {
307 case LH : /* double rotation left */
309 switch ( l->avl_bf ) {
311 (*root)->avl_bf = EH;
315 (*root)->avl_bf = EH;
319 (*root)->avl_bf = LH;
325 (*root)->avl_right = r;
329 case EH : /* single rotation left */
330 (*root)->avl_bf = RH;
335 case RH : /* single rotation left */
336 (*root)->avl_bf = EH;
349 * ravl_delete() - called from avl_delete to do recursive deletion of a
350 * node from an avl tree. It finds the node recursively, deletes it,
351 * and returns shorter if the tree is shorter after the deletion and
356 ravl_delete( root, data, fcmp, shorter )
362 int shortersubtree = 0;
365 Avlnode *minnode, *savenode;
367 if ( *root == NULLAVL )
370 cmp = (*fcmp)( data, (*root)->avl_data );
375 savedata = savenode->avl_data;
377 /* simple cases: no left child */
378 if ( (*root)->avl_left == 0 ) {
379 *root = (*root)->avl_right;
381 free( (char *) savenode );
384 } else if ( (*root)->avl_right == 0 ) {
385 *root = (*root)->avl_left;
387 free( (char *) savenode );
392 * avl_getmin will return to us the smallest node greater
393 * than the one we are trying to delete. deleting this node
394 * from the right subtree is guaranteed to end in one of the
395 * simple cases above.
398 minnode = (*root)->avl_right;
399 while ( minnode->avl_left != NULLAVL )
400 minnode = minnode->avl_left;
403 (*root)->avl_data = minnode->avl_data;
404 minnode->avl_data = savedata;
406 savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
409 if ( shortersubtree )
410 *shorter = right_balance( root );
414 } else if ( cmp < 0 ) {
415 if ( (savedata = ravl_delete( &(*root)->avl_left, data, fcmp,
416 &shortersubtree )) == 0 ) {
421 /* left subtree shorter? */
422 if ( shortersubtree )
423 *shorter = left_balance( root );
428 if ( (savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
429 &shortersubtree )) == 0 ) {
434 if ( shortersubtree )
435 *shorter = right_balance( root );
444 * avl_delete() - deletes the node containing data (according to fcmp) from
445 * the avl tree rooted at root.
449 avl_delete( root, data, fcmp )
456 return( ravl_delete( root, data, fcmp, &shorter ) );
460 int avl_inapply( root, fn, arg, stopflag )
467 return( AVL_NOMORE );
469 if ( root->avl_left != 0 )
470 if ( avl_inapply( root->avl_left, fn, arg, stopflag )
474 if ( (*fn)( root->avl_data, arg ) == stopflag )
477 if ( root->avl_right == 0 )
478 return( AVL_NOMORE );
480 return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
484 int avl_postapply( root, fn, arg, stopflag )
491 return( AVL_NOMORE );
493 if ( root->avl_left != 0 )
494 if ( avl_postapply( root->avl_left, fn, arg, stopflag )
498 if ( root->avl_right != 0 )
499 if ( avl_postapply( root->avl_right, fn, arg, stopflag )
503 return( (*fn)( root->avl_data, arg ) );
507 int avl_preapply( root, fn, arg, stopflag )
514 return( AVL_NOMORE );
516 if ( (*fn)( root->avl_data, arg ) == stopflag )
519 if ( root->avl_left != 0 )
520 if ( avl_preapply( root->avl_left, fn, arg, stopflag )
524 if ( root->avl_right == 0 )
525 return( AVL_NOMORE );
527 return( avl_preapply( root->avl_right, fn, arg, stopflag ) );
531 * avl_apply -- avl tree root is traversed, function fn is called with
532 * arguments arg and the data portion of each node. if fn returns stopflag,
533 * the traversal is cut short, otherwise it continues. Do not use -6 as
534 * a stopflag, as this is what is used to indicate the traversal ran out
538 int avl_apply( root, fn, arg, stopflag, type )
547 return( avl_inapply( root, fn, arg, stopflag ) );
549 return( avl_preapply( root, fn, arg, stopflag ) );
551 return( avl_postapply( root, fn, arg, stopflag ) );
553 fprintf( stderr, "Invalid traversal type %d\n", type );
561 * avl_prefixapply - traverse avl tree root, applying function fprefix
562 * to any nodes that match. fcmp is called with data as its first arg
563 * and the current node's data as its second arg. it should return
564 * 0 if they match, < 0 if data is less, and > 0 if data is greater.
565 * the idea is to efficiently find all nodes that are prefixes of
566 * some key... Like avl_apply, this routine also takes a stopflag
567 * and will return prematurely if fmatch returns this value. Otherwise,
568 * AVL_NOMORE is returned.
571 int avl_prefixapply( root, data, fmatch, marg, fcmp, carg, stopflag )
583 return( AVL_NOMORE );
585 cmp = (*fcmp)( data, root->avl_data, carg );
587 if ( (*fmatch)( root->avl_data, marg ) == stopflag )
590 if ( root->avl_left != 0 )
591 if ( avl_prefixapply( root->avl_left, data, fmatch,
592 marg, fcmp, carg, stopflag ) == stopflag )
595 if ( root->avl_right != 0 )
596 return( avl_prefixapply( root->avl_right, data, fmatch,
597 marg, fcmp, carg, stopflag ) );
599 return( AVL_NOMORE );
601 } else if ( cmp < 0 ) {
602 if ( root->avl_left != 0 )
603 return( avl_prefixapply( root->avl_left, data, fmatch,
604 marg, fcmp, carg, stopflag ) );
606 if ( root->avl_right != 0 )
607 return( avl_prefixapply( root->avl_right, data, fmatch,
608 marg, fcmp, carg, stopflag ) );
611 return( AVL_NOMORE );
615 * avl_free -- traverse avltree root, freeing the memory it is using.
616 * the dfree() is called to free the data portion of each node. The
617 * number of items actually freed is returned.
620 int avl_free( root, dfree )
630 if ( root->avl_left != 0 )
631 nleft = avl_free( root->avl_left, dfree );
633 if ( root->avl_right != 0 )
634 nright = avl_free( root->avl_right, dfree );
637 (*dfree)( root->avl_data );
639 return( nleft + nright + 1 );
643 * avl_find -- search avltree root for a node with data data. the function
644 * cmp is used to compare things. it is called with data as its first arg
645 * and the current node data as its second. it should return 0 if they match,
646 * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
650 avl_find( root, data, fcmp )
657 while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
659 root = root->avl_left;
661 root = root->avl_right;
664 return( root ? root->avl_data : 0 );
668 * avl_find_lin -- search avltree root linearly for a node with data data.
669 * the function cmp is used to compare things. it is called with data as its
670 * first arg and the current node data as its second. it should return 0 if
671 * they match, non-zero otherwise.
675 avl_find_lin( root, data, fcmp )
685 if ( (*fcmp)( data, root->avl_data ) == 0 )
686 return( root->avl_data );
688 if ( root->avl_left != 0 )
689 if ( (res = avl_find_lin( root->avl_left, data, fcmp ))
693 if ( root->avl_right == 0 )
696 return( avl_find_lin( root->avl_right, data, fcmp ) );
699 static caddr_t *avl_list;
700 static int avl_maxlist;
701 static int avl_nextlist;
703 #define AVL_GRABSIZE 100
707 int avl_buildlist( data, arg )
713 if ( avl_list == (caddr_t *) 0 ) {
714 avl_list = (caddr_t *) malloc(AVL_GRABSIZE * sizeof(caddr_t));
715 slots = AVL_GRABSIZE;
717 } else if ( avl_maxlist == slots ) {
718 slots += AVL_GRABSIZE;
719 avl_list = (caddr_t *) realloc( (char *) avl_list,
720 (unsigned) slots * sizeof(caddr_t));
723 avl_list[ avl_maxlist++ ] = data;
729 * avl_getfirst() and avl_getnext() are provided as alternate tree
730 * traversal methods, to be used when a single function cannot be
731 * provided to be called with every node in the tree. avl_getfirst()
732 * traverses the tree and builds a linear list of all the nodes,
733 * returning the first node. avl_getnext() returns the next thing
734 * on the list built by avl_getfirst(). This means that avl_getfirst()
735 * can take a while, and that the tree should not be messed with while
736 * being traversed in this way, and that multiple traversals (even of
737 * different trees) cannot be active at once.
745 free( (char *) avl_list);
746 avl_list = (caddr_t *) 0;
754 (void) avl_apply( root, avl_buildlist, (caddr_t) 0, -1, AVL_INORDER );
756 return( avl_list[ avl_nextlist++ ] );
765 if ( avl_nextlist == avl_maxlist ) {
766 free( (caddr_t) avl_list);
767 avl_list = (caddr_t *) 0;
771 return( avl_list[ avl_nextlist++ ] );