1 /* avl.c - routines to implement an avl tree */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * Copyright (c) 1993 Regents of the University of Michigan.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that this notice is preserved and that due credit is given
13 * to the University of Michigan at Ann Arbor. The name of the University
14 * may not be used to endorse or promote products derived from this
15 * software without specific prior written permission. This software
16 * is provided ``as is'' without express or implied warranty.
22 #include <ac/stdlib.h>
25 #define ber_memalloc malloc
26 #define ber_memrealloc realloc
32 #define ROTATERIGHT(x) { \
34 if ( *(x) == NULL || (*(x))->avl_left == NULL ) {\
35 (void) fputs("RR error\n", stderr); exit( EXIT_FAILURE ); \
37 tmp = (*(x))->avl_left;\
38 (*(x))->avl_left = tmp->avl_right;\
39 tmp->avl_right = *(x);\
42 #define ROTATELEFT(x) { \
44 if ( *(x) == NULL || (*(x))->avl_right == NULL ) {\
45 (void) fputs("RL error\n", stderr); exit( EXIT_FAILURE ); \
47 tmp = (*(x))->avl_right;\
48 (*(x))->avl_right = tmp->avl_left;\
49 tmp->avl_left = *(x);\
54 * ravl_insert - called from avl_insert() to do a recursive insert into
55 * and balance of an avl tree.
63 AVL_CMP fcmp, /* comparison function */
64 AVL_DUP fdup, /* function to call for duplicates */
68 int rc, cmp, tallersub;
72 if ( (*iroot = (Avlnode *) ber_memalloc( sizeof( Avlnode ) ))
76 (*iroot)->avl_left = 0;
77 (*iroot)->avl_right = 0;
79 (*iroot)->avl_data = data;
84 cmp = (*fcmp)( data, (*iroot)->avl_data );
86 /* equal - duplicate name */
89 return( (*fdup)( (*iroot)->avl_data, data ) );
94 rc = ravl_insert( &((*iroot)->avl_right), data, &tallersub,
97 switch ( (*iroot)->avl_bf ) {
98 case LH : /* left high - balance is restored */
99 (*iroot)->avl_bf = EH;
102 case EH : /* equal height - now right heavy */
103 (*iroot)->avl_bf = RH;
106 case RH : /* right heavy to start - right balance */
107 r = (*iroot)->avl_right;
108 switch ( r->avl_bf ) {
109 case LH : /* double rotation left */
111 switch ( l->avl_bf ) {
112 case LH : (*iroot)->avl_bf = EH;
115 case EH : (*iroot)->avl_bf = EH;
118 case RH : (*iroot)->avl_bf = LH;
124 (*iroot)->avl_right = r;
128 case EH : /* This should never happen */
130 case RH : /* single rotation left */
131 (*iroot)->avl_bf = EH;
145 rc = ravl_insert( &((*iroot)->avl_left), data, &tallersub,
148 switch ( (*iroot)->avl_bf ) {
149 case LH : /* left high to start - left balance */
150 l = (*iroot)->avl_left;
151 switch ( l->avl_bf ) {
152 case LH : /* single rotation right */
153 (*iroot)->avl_bf = EH;
158 case EH : /* this should never happen */
160 case RH : /* double rotation right */
162 switch ( r->avl_bf ) {
163 case LH : (*iroot)->avl_bf = RH;
166 case EH : (*iroot)->avl_bf = EH;
169 case RH : (*iroot)->avl_bf = EH;
175 (*iroot)->avl_left = l;
181 case EH : /* equal height - now left heavy */
182 (*iroot)->avl_bf = LH;
185 case RH : /* right high - balance is restored */
186 (*iroot)->avl_bf = EH;
198 * avl_insert -- insert a node containing data data into the avl tree
199 * with root root. fcmp is a function to call to compare the data portion
200 * of two nodes. it should take two arguments and return <, >, or == 0,
201 * depending on whether its first argument is <, >, or == its second
202 * argument (like strcmp, e.g.). fdup is a function to call when a duplicate
203 * node is inserted. it should return 0, or -1 and its return value
204 * will be the return value from avl_insert in the case of a duplicate node.
205 * the function will be called with the original node's data as its first
206 * argument and with the incoming duplicate node's data as its second
207 * argument. this could be used, for example, to keep a count with each
210 * NOTE: this routine may malloc memory
214 avl_insert( Avlnode **root, void* data, AVL_CMP fcmp, AVL_DUP fdup )
218 return( ravl_insert( root, data, &taller, fcmp, fdup, 0 ) );
222 * right_balance() - called from delete when root's right subtree has
223 * been shortened because of a deletion.
227 right_balance( Avlnode **root )
232 switch( (*root)->avl_bf ) {
233 case RH: /* was right high - equal now */
234 (*root)->avl_bf = EH;
237 case EH: /* was equal - left high now */
238 (*root)->avl_bf = LH;
241 case LH: /* was right high - balance */
242 l = (*root)->avl_left;
243 switch ( l->avl_bf ) {
244 case RH : /* double rotation left */
246 switch ( r->avl_bf ) {
248 (*root)->avl_bf = EH;
252 (*root)->avl_bf = EH;
256 (*root)->avl_bf = RH;
262 (*root)->avl_left = l;
266 case EH : /* right rotation */
267 (*root)->avl_bf = LH;
272 case LH : /* single rotation right */
273 (*root)->avl_bf = EH;
286 * left_balance() - called from delete when root's left subtree has
287 * been shortened because of a deletion.
291 left_balance( Avlnode **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( Avlnode **root, void* data, AVL_CMP fcmp, int *shorter )
359 int shortersubtree = 0;
362 Avlnode *minnode, *savenode;
364 if ( *root == NULLAVL )
367 cmp = (*fcmp)( data, (*root)->avl_data );
372 savedata = savenode->avl_data;
374 /* simple cases: no left child */
375 if ( (*root)->avl_left == 0 ) {
376 *root = (*root)->avl_right;
378 free( (char *) savenode );
381 } else if ( (*root)->avl_right == 0 ) {
382 *root = (*root)->avl_left;
384 free( (char *) savenode );
389 * avl_getmin will return to us the smallest node greater
390 * than the one we are trying to delete. deleting this node
391 * from the right subtree is guaranteed to end in one of the
392 * simple cases above.
395 minnode = (*root)->avl_right;
396 while ( minnode->avl_left != NULLAVL )
397 minnode = minnode->avl_left;
400 (*root)->avl_data = minnode->avl_data;
401 minnode->avl_data = savedata;
403 savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
406 if ( shortersubtree )
407 *shorter = right_balance( root );
411 } else if ( cmp < 0 ) {
412 if ( (savedata = ravl_delete( &(*root)->avl_left, data, fcmp,
413 &shortersubtree )) == 0 ) {
418 /* left subtree shorter? */
419 if ( shortersubtree )
420 *shorter = left_balance( root );
425 if ( (savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
426 &shortersubtree )) == 0 ) {
431 if ( shortersubtree )
432 *shorter = right_balance( root );
441 * avl_delete() - deletes the node containing data (according to fcmp) from
442 * the avl tree rooted at root.
446 avl_delete( Avlnode **root, void* data, AVL_CMP fcmp )
450 return( ravl_delete( root, data, fcmp, &shorter ) );
454 avl_inapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
457 return( AVL_NOMORE );
459 if ( root->avl_left != 0 )
460 if ( avl_inapply( root->avl_left, fn, arg, stopflag )
464 if ( (*fn)( root->avl_data, arg ) == stopflag )
467 if ( root->avl_right == 0 )
468 return( AVL_NOMORE );
470 return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
474 avl_postapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
477 return( AVL_NOMORE );
479 if ( root->avl_left != 0 )
480 if ( avl_postapply( root->avl_left, fn, arg, stopflag )
484 if ( root->avl_right != 0 )
485 if ( avl_postapply( root->avl_right, fn, arg, stopflag )
489 return( (*fn)( root->avl_data, arg ) );
493 avl_preapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
496 return( AVL_NOMORE );
498 if ( (*fn)( root->avl_data, arg ) == stopflag )
501 if ( root->avl_left != 0 )
502 if ( avl_preapply( root->avl_left, fn, arg, stopflag )
506 if ( root->avl_right == 0 )
507 return( AVL_NOMORE );
509 return( avl_preapply( root->avl_right, fn, arg, stopflag ) );
513 * avl_apply -- avl tree root is traversed, function fn is called with
514 * arguments arg and the data portion of each node. if fn returns stopflag,
515 * the traversal is cut short, otherwise it continues. Do not use -6 as
516 * a stopflag, as this is what is used to indicate the traversal ran out
521 avl_apply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag, int type )
525 return( avl_inapply( root, fn, arg, stopflag ) );
527 return( avl_preapply( root, fn, arg, stopflag ) );
529 return( avl_postapply( root, fn, arg, stopflag ) );
531 fprintf( stderr, "Invalid traversal type %d\n", type );
539 * avl_prefixapply - traverse avl tree root, applying function fprefix
540 * to any nodes that match. fcmp is called with data as its first arg
541 * and the current node's data as its second arg. it should return
542 * 0 if they match, < 0 if data is less, and > 0 if data is greater.
543 * the idea is to efficiently find all nodes that are prefixes of
544 * some key... Like avl_apply, this routine also takes a stopflag
545 * and will return prematurely if fmatch returns this value. Otherwise,
546 * AVL_NOMORE is returned.
563 return( AVL_NOMORE );
565 cmp = (*fcmp)( data, root->avl_data /* , carg */);
567 if ( (*fmatch)( root->avl_data, marg ) == stopflag )
570 if ( root->avl_left != 0 )
571 if ( avl_prefixapply( root->avl_left, data, fmatch,
572 marg, fcmp, carg, stopflag ) == stopflag )
575 if ( root->avl_right != 0 )
576 return( avl_prefixapply( root->avl_right, data, fmatch,
577 marg, fcmp, carg, stopflag ) );
579 return( AVL_NOMORE );
581 } else if ( cmp < 0 ) {
582 if ( root->avl_left != 0 )
583 return( avl_prefixapply( root->avl_left, data, fmatch,
584 marg, fcmp, carg, stopflag ) );
586 if ( root->avl_right != 0 )
587 return( avl_prefixapply( root->avl_right, data, fmatch,
588 marg, fcmp, carg, stopflag ) );
591 return( AVL_NOMORE );
595 * avl_free -- traverse avltree root, freeing the memory it is using.
596 * the dfree() is called to free the data portion of each node. The
597 * number of items actually freed is returned.
601 avl_free( Avlnode *root, AVL_FREE dfree )
609 if ( root->avl_left != 0 )
610 nleft = avl_free( root->avl_left, dfree );
612 if ( root->avl_right != 0 )
613 nright = avl_free( root->avl_right, dfree );
616 (*dfree)( root->avl_data );
619 return( nleft + nright + 1 );
623 * avl_find -- search avltree root for a node with data data. the function
624 * cmp is used to compare things. it is called with data as its first arg
625 * and the current node data as its second. it should return 0 if they match,
626 * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
630 avl_find( Avlnode *root, const void* data, AVL_CMP fcmp )
634 while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
636 root = root->avl_left;
638 root = root->avl_right;
641 return( root ? root->avl_data : 0 );
645 * avl_find_lin -- search avltree root linearly for a node with data data.
646 * the function cmp is used to compare things. it is called with data as its
647 * first arg and the current node data as its second. it should return 0 if
648 * they match, non-zero otherwise.
652 avl_find_lin( Avlnode *root, const void* data, AVL_CMP fcmp )
659 if ( (*fcmp)( data, root->avl_data ) == 0 )
660 return( root->avl_data );
662 if ( root->avl_left != 0 )
663 if ( (res = avl_find_lin( root->avl_left, data, fcmp ))
667 if ( root->avl_right == 0 )
670 return( avl_find_lin( root->avl_right, data, fcmp ) );
673 /* NON-REENTRANT INTERFACE */
675 static void* *avl_list;
676 static int avl_maxlist;
677 static int avl_nextlist;
679 #define AVL_GRABSIZE 100
683 avl_buildlist( void* data, void* arg )
687 if ( avl_list == (void* *) 0 ) {
688 avl_list = (void* *) ber_memalloc(AVL_GRABSIZE * sizeof(void*));
689 slots = AVL_GRABSIZE;
691 } else if ( avl_maxlist == slots ) {
692 slots += AVL_GRABSIZE;
693 avl_list = (void* *) ber_memrealloc( (char *) avl_list,
694 (unsigned) slots * sizeof(void*));
697 avl_list[ avl_maxlist++ ] = data;
703 * avl_getfirst() and avl_getnext() are provided as alternate tree
704 * traversal methods, to be used when a single function cannot be
705 * provided to be called with every node in the tree. avl_getfirst()
706 * traverses the tree and builds a linear list of all the nodes,
707 * returning the first node. avl_getnext() returns the next thing
708 * on the list built by avl_getfirst(). This means that avl_getfirst()
709 * can take a while, and that the tree should not be messed with while
710 * being traversed in this way, and that multiple traversals (even of
711 * different trees) cannot be active at once.
715 avl_getfirst( Avlnode *root )
718 free( (char *) avl_list);
719 avl_list = (void* *) 0;
727 (void) avl_apply( root, avl_buildlist, (void*) 0, -1, AVL_INORDER );
729 return( avl_list[ avl_nextlist++ ] );
738 if ( avl_nextlist == avl_maxlist ) {
739 free( (void*) avl_list);
740 avl_list = (void* *) 0;
744 return( avl_list[ avl_nextlist++ ] );
747 /* end non-reentrant code */
751 avl_dup_error( void* left, void* right )
757 avl_dup_ok( void* left, void* right )