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";
26 #define ROTATERIGHT(x) { \
28 if ( *(x) == NULL || (*(x))->avl_left == NULL ) {\
29 (void) fputs("RR error\n", stderr); exit(1); \
31 tmp = (*(x))->avl_left;\
32 (*(x))->avl_left = tmp->avl_right;\
33 tmp->avl_right = *(x);\
36 #define ROTATELEFT(x) { \
38 if ( *(x) == NULL || (*(x))->avl_right == NULL ) {\
39 (void) fputs("RL error\n", stderr); exit(1); \
41 tmp = (*(x))->avl_right;\
42 (*(x))->avl_right = tmp->avl_left;\
43 tmp->avl_left = *(x);\
48 * ravl_insert - called from avl_insert() to do a recursive insert into
49 * and balance of an avl tree.
57 AVL_CMP fcmp, /* comparison function */
58 AVL_DUP 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
208 avl_insert( Avlnode **root, void* data, AVL_CMP fcmp, AVL_DUP fdup )
212 return( ravl_insert( root, data, &taller, fcmp, fdup, 0 ) );
216 * right_balance() - called from delete when root's right subtree has
217 * been shortened because of a deletion.
221 right_balance( Avlnode **root )
226 switch( (*root)->avl_bf ) {
227 case RH: /* was right high - equal now */
228 (*root)->avl_bf = EH;
231 case EH: /* was equal - left high now */
232 (*root)->avl_bf = LH;
235 case LH: /* was right high - balance */
236 l = (*root)->avl_left;
237 switch ( l->avl_bf ) {
238 case RH : /* double rotation left */
240 switch ( r->avl_bf ) {
242 (*root)->avl_bf = EH;
246 (*root)->avl_bf = EH;
250 (*root)->avl_bf = RH;
256 (*root)->avl_left = l;
260 case EH : /* right rotation */
261 (*root)->avl_bf = LH;
266 case LH : /* single rotation right */
267 (*root)->avl_bf = EH;
280 * left_balance() - called from delete when root's left subtree has
281 * been shortened because of a deletion.
285 left_balance( Avlnode **root )
290 switch( (*root)->avl_bf ) {
291 case LH: /* was left high - equal now */
292 (*root)->avl_bf = EH;
295 case EH: /* was equal - right high now */
296 (*root)->avl_bf = RH;
299 case RH: /* was right high - balance */
300 r = (*root)->avl_right;
301 switch ( r->avl_bf ) {
302 case LH : /* double rotation left */
304 switch ( l->avl_bf ) {
306 (*root)->avl_bf = EH;
310 (*root)->avl_bf = EH;
314 (*root)->avl_bf = LH;
320 (*root)->avl_right = r;
324 case EH : /* single rotation left */
325 (*root)->avl_bf = RH;
330 case RH : /* single rotation left */
331 (*root)->avl_bf = EH;
344 * ravl_delete() - called from avl_delete to do recursive deletion of a
345 * node from an avl tree. It finds the node recursively, deletes it,
346 * and returns shorter if the tree is shorter after the deletion and
351 ravl_delete( Avlnode **root, void* data, AVL_CMP fcmp, int *shorter )
353 int shortersubtree = 0;
356 Avlnode *minnode, *savenode;
358 if ( *root == NULLAVL )
361 cmp = (*fcmp)( data, (*root)->avl_data );
366 savedata = savenode->avl_data;
368 /* simple cases: no left child */
369 if ( (*root)->avl_left == 0 ) {
370 *root = (*root)->avl_right;
372 free( (char *) savenode );
375 } else if ( (*root)->avl_right == 0 ) {
376 *root = (*root)->avl_left;
378 free( (char *) savenode );
383 * avl_getmin will return to us the smallest node greater
384 * than the one we are trying to delete. deleting this node
385 * from the right subtree is guaranteed to end in one of the
386 * simple cases above.
389 minnode = (*root)->avl_right;
390 while ( minnode->avl_left != NULLAVL )
391 minnode = minnode->avl_left;
394 (*root)->avl_data = minnode->avl_data;
395 minnode->avl_data = savedata;
397 savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
400 if ( shortersubtree )
401 *shorter = right_balance( root );
405 } else if ( cmp < 0 ) {
406 if ( (savedata = ravl_delete( &(*root)->avl_left, data, fcmp,
407 &shortersubtree )) == 0 ) {
412 /* left subtree shorter? */
413 if ( shortersubtree )
414 *shorter = left_balance( root );
419 if ( (savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
420 &shortersubtree )) == 0 ) {
425 if ( shortersubtree )
426 *shorter = right_balance( root );
435 * avl_delete() - deletes the node containing data (according to fcmp) from
436 * the avl tree rooted at root.
440 avl_delete( Avlnode **root, void* data, AVL_CMP fcmp )
444 return( ravl_delete( root, data, fcmp, &shorter ) );
448 avl_inapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
451 return( AVL_NOMORE );
453 if ( root->avl_left != 0 )
454 if ( avl_inapply( root->avl_left, fn, arg, stopflag )
458 if ( (*fn)( root->avl_data, arg ) == stopflag )
461 if ( root->avl_right == 0 )
462 return( AVL_NOMORE );
464 return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
468 avl_postapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
471 return( AVL_NOMORE );
473 if ( root->avl_left != 0 )
474 if ( avl_postapply( root->avl_left, fn, arg, stopflag )
478 if ( root->avl_right != 0 )
479 if ( avl_postapply( root->avl_right, fn, arg, stopflag )
483 return( (*fn)( root->avl_data, arg ) );
487 avl_preapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
490 return( AVL_NOMORE );
492 if ( (*fn)( root->avl_data, arg ) == stopflag )
495 if ( root->avl_left != 0 )
496 if ( avl_preapply( root->avl_left, fn, arg, stopflag )
500 if ( root->avl_right == 0 )
501 return( AVL_NOMORE );
503 return( avl_preapply( root->avl_right, fn, arg, stopflag ) );
507 * avl_apply -- avl tree root is traversed, function fn is called with
508 * arguments arg and the data portion of each node. if fn returns stopflag,
509 * the traversal is cut short, otherwise it continues. Do not use -6 as
510 * a stopflag, as this is what is used to indicate the traversal ran out
515 avl_apply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag, int type )
519 return( avl_inapply( root, fn, arg, stopflag ) );
521 return( avl_preapply( root, fn, arg, stopflag ) );
523 return( avl_postapply( root, fn, arg, stopflag ) );
525 fprintf( stderr, "Invalid traversal type %d\n", type );
533 * avl_prefixapply - traverse avl tree root, applying function fprefix
534 * to any nodes that match. fcmp is called with data as its first arg
535 * and the current node's data as its second arg. it should return
536 * 0 if they match, < 0 if data is less, and > 0 if data is greater.
537 * the idea is to efficiently find all nodes that are prefixes of
538 * some key... Like avl_apply, this routine also takes a stopflag
539 * and will return prematurely if fmatch returns this value. Otherwise,
540 * AVL_NOMORE is returned.
557 return( AVL_NOMORE );
559 cmp = (*fcmp)( data, root->avl_data /* , carg */);
561 if ( (*fmatch)( root->avl_data, marg ) == stopflag )
564 if ( root->avl_left != 0 )
565 if ( avl_prefixapply( root->avl_left, data, fmatch,
566 marg, fcmp, carg, stopflag ) == stopflag )
569 if ( root->avl_right != 0 )
570 return( avl_prefixapply( root->avl_right, data, fmatch,
571 marg, fcmp, carg, stopflag ) );
573 return( AVL_NOMORE );
575 } else if ( cmp < 0 ) {
576 if ( root->avl_left != 0 )
577 return( avl_prefixapply( root->avl_left, data, fmatch,
578 marg, fcmp, carg, stopflag ) );
580 if ( root->avl_right != 0 )
581 return( avl_prefixapply( root->avl_right, data, fmatch,
582 marg, fcmp, carg, stopflag ) );
585 return( AVL_NOMORE );
589 * avl_free -- traverse avltree root, freeing the memory it is using.
590 * the dfree() is called to free the data portion of each node. The
591 * number of items actually freed is returned.
595 avl_free( Avlnode *root, AVL_FREE dfree )
603 if ( root->avl_left != 0 )
604 nleft = avl_free( root->avl_left, dfree );
606 if ( root->avl_right != 0 )
607 nright = avl_free( root->avl_right, dfree );
610 (*dfree)( root->avl_data );
613 return( nleft + nright + 1 );
617 * avl_find -- search avltree root for a node with data data. the function
618 * cmp is used to compare things. it is called with data as its first arg
619 * and the current node data as its second. it should return 0 if they match,
620 * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
624 avl_find( Avlnode *root, void* data, AVL_CMP fcmp )
628 while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
630 root = root->avl_left;
632 root = root->avl_right;
635 return( root ? root->avl_data : 0 );
639 * avl_find_lin -- search avltree root linearly for a node with data data.
640 * the function cmp is used to compare things. it is called with data as its
641 * first arg and the current node data as its second. it should return 0 if
642 * they match, non-zero otherwise.
646 avl_find_lin( Avlnode *root, void* data, AVL_CMP fcmp )
653 if ( (*fcmp)( data, root->avl_data ) == 0 )
654 return( root->avl_data );
656 if ( root->avl_left != 0 )
657 if ( (res = avl_find_lin( root->avl_left, data, fcmp ))
661 if ( root->avl_right == 0 )
664 return( avl_find_lin( root->avl_right, data, fcmp ) );
667 static void* *avl_list;
668 static int avl_maxlist;
669 static int avl_nextlist;
671 #define AVL_GRABSIZE 100
675 avl_buildlist( void* data, void* arg )
679 if ( avl_list == (void* *) 0 ) {
680 avl_list = (void* *) malloc(AVL_GRABSIZE * sizeof(void*));
681 slots = AVL_GRABSIZE;
683 } else if ( avl_maxlist == slots ) {
684 slots += AVL_GRABSIZE;
685 avl_list = (void* *) realloc( (char *) avl_list,
686 (unsigned) slots * sizeof(void*));
689 avl_list[ avl_maxlist++ ] = data;
695 * avl_getfirst() and avl_getnext() are provided as alternate tree
696 * traversal methods, to be used when a single function cannot be
697 * provided to be called with every node in the tree. avl_getfirst()
698 * traverses the tree and builds a linear list of all the nodes,
699 * returning the first node. avl_getnext() returns the next thing
700 * on the list built by avl_getfirst(). This means that avl_getfirst()
701 * can take a while, and that the tree should not be messed with while
702 * being traversed in this way, and that multiple traversals (even of
703 * different trees) cannot be active at once.
707 avl_getfirst( Avlnode *root )
710 free( (char *) avl_list);
711 avl_list = (void* *) 0;
719 (void) avl_apply( root, avl_buildlist, (void*) 0, -1, AVL_INORDER );
721 return( avl_list[ avl_nextlist++ ] );
730 if ( avl_nextlist == avl_maxlist ) {
731 free( (void*) avl_list);
732 avl_list = (void* *) 0;
736 return( avl_list[ avl_nextlist++ ] );
740 avl_dup_error( void* left, void* right )
746 avl_dup_ok( void* left, void* right )