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
34 #define ROTATERIGHT(x) { \
36 if ( *(x) == NULL || (*(x))->avl_left == NULL ) {\
37 (void) fputs("RR error\n", stderr); exit( EXIT_FAILURE ); \
39 tmp = (*(x))->avl_left;\
40 (*(x))->avl_left = tmp->avl_right;\
41 tmp->avl_right = *(x);\
44 #define ROTATELEFT(x) { \
46 if ( *(x) == NULL || (*(x))->avl_right == NULL ) {\
47 (void) fputs("RL error\n", stderr); exit( EXIT_FAILURE ); \
49 tmp = (*(x))->avl_right;\
50 (*(x))->avl_right = tmp->avl_left;\
51 tmp->avl_left = *(x);\
56 * ravl_insert - called from avl_insert() to do a recursive insert into
57 * and balance of an avl tree.
65 AVL_CMP fcmp, /* comparison function */
66 AVL_DUP fdup, /* function to call for duplicates */
70 int rc, cmp, tallersub;
74 if ( (*iroot = (Avlnode *) ber_memalloc( sizeof( Avlnode ) ))
78 (*iroot)->avl_left = 0;
79 (*iroot)->avl_right = 0;
81 (*iroot)->avl_data = data;
86 cmp = (*fcmp)( data, (*iroot)->avl_data );
88 /* equal - duplicate name */
91 return( (*fdup)( (*iroot)->avl_data, data ) );
96 rc = ravl_insert( &((*iroot)->avl_right), data, &tallersub,
99 switch ( (*iroot)->avl_bf ) {
100 case LH : /* left high - balance is restored */
101 (*iroot)->avl_bf = EH;
104 case EH : /* equal height - now right heavy */
105 (*iroot)->avl_bf = RH;
108 case RH : /* right heavy to start - right balance */
109 r = (*iroot)->avl_right;
110 switch ( r->avl_bf ) {
111 case LH : /* double rotation left */
113 switch ( l->avl_bf ) {
114 case LH : (*iroot)->avl_bf = EH;
117 case EH : (*iroot)->avl_bf = EH;
120 case RH : (*iroot)->avl_bf = LH;
126 (*iroot)->avl_right = r;
130 case EH : /* This should never happen */
132 case RH : /* single rotation left */
133 (*iroot)->avl_bf = EH;
147 rc = ravl_insert( &((*iroot)->avl_left), data, &tallersub,
150 switch ( (*iroot)->avl_bf ) {
151 case LH : /* left high to start - left balance */
152 l = (*iroot)->avl_left;
153 switch ( l->avl_bf ) {
154 case LH : /* single rotation right */
155 (*iroot)->avl_bf = EH;
160 case EH : /* this should never happen */
162 case RH : /* double rotation right */
164 switch ( r->avl_bf ) {
165 case LH : (*iroot)->avl_bf = RH;
168 case EH : (*iroot)->avl_bf = EH;
171 case RH : (*iroot)->avl_bf = EH;
177 (*iroot)->avl_left = l;
183 case EH : /* equal height - now left heavy */
184 (*iroot)->avl_bf = LH;
187 case RH : /* right high - balance is restored */
188 (*iroot)->avl_bf = EH;
200 * avl_insert -- insert a node containing data data into the avl tree
201 * with root root. fcmp is a function to call to compare the data portion
202 * of two nodes. it should take two arguments and return <, >, or == 0,
203 * depending on whether its first argument is <, >, or == its second
204 * argument (like strcmp, e.g.). fdup is a function to call when a duplicate
205 * node is inserted. it should return 0, or -1 and its return value
206 * will be the return value from avl_insert in the case of a duplicate node.
207 * the function will be called with the original node's data as its first
208 * argument and with the incoming duplicate node's data as its second
209 * argument. this could be used, for example, to keep a count with each
212 * NOTE: this routine may malloc memory
216 avl_insert( Avlnode **root, void* data, AVL_CMP fcmp, AVL_DUP fdup )
220 return( ravl_insert( root, data, &taller, fcmp, fdup, 0 ) );
224 * right_balance() - called from delete when root's right subtree has
225 * been shortened because of a deletion.
229 right_balance( Avlnode **root )
234 switch( (*root)->avl_bf ) {
235 case RH: /* was right high - equal now */
236 (*root)->avl_bf = EH;
239 case EH: /* was equal - left high now */
240 (*root)->avl_bf = LH;
243 case LH: /* was right high - balance */
244 l = (*root)->avl_left;
245 switch ( l->avl_bf ) {
246 case RH : /* double rotation left */
248 switch ( r->avl_bf ) {
250 (*root)->avl_bf = EH;
254 (*root)->avl_bf = EH;
258 (*root)->avl_bf = RH;
264 (*root)->avl_left = l;
268 case EH : /* right rotation */
269 (*root)->avl_bf = LH;
274 case LH : /* single rotation right */
275 (*root)->avl_bf = EH;
288 * left_balance() - called from delete when root's left subtree has
289 * been shortened because of a deletion.
293 left_balance( Avlnode **root )
298 switch( (*root)->avl_bf ) {
299 case LH: /* was left high - equal now */
300 (*root)->avl_bf = EH;
303 case EH: /* was equal - right high now */
304 (*root)->avl_bf = RH;
307 case RH: /* was right high - balance */
308 r = (*root)->avl_right;
309 switch ( r->avl_bf ) {
310 case LH : /* double rotation left */
312 switch ( l->avl_bf ) {
314 (*root)->avl_bf = EH;
318 (*root)->avl_bf = EH;
322 (*root)->avl_bf = LH;
328 (*root)->avl_right = r;
332 case EH : /* single rotation left */
333 (*root)->avl_bf = RH;
338 case RH : /* single rotation left */
339 (*root)->avl_bf = EH;
352 * ravl_delete() - called from avl_delete to do recursive deletion of a
353 * node from an avl tree. It finds the node recursively, deletes it,
354 * and returns shorter if the tree is shorter after the deletion and
359 ravl_delete( Avlnode **root, void* data, AVL_CMP fcmp, int *shorter )
361 int shortersubtree = 0;
364 Avlnode *minnode, *savenode;
366 if ( *root == NULLAVL )
369 cmp = (*fcmp)( data, (*root)->avl_data );
374 savedata = savenode->avl_data;
376 /* simple cases: no left child */
377 if ( (*root)->avl_left == 0 ) {
378 *root = (*root)->avl_right;
380 free( (char *) savenode );
383 } else if ( (*root)->avl_right == 0 ) {
384 *root = (*root)->avl_left;
386 free( (char *) savenode );
391 * avl_getmin will return to us the smallest node greater
392 * than the one we are trying to delete. deleting this node
393 * from the right subtree is guaranteed to end in one of the
394 * simple cases above.
397 minnode = (*root)->avl_right;
398 while ( minnode->avl_left != NULLAVL )
399 minnode = minnode->avl_left;
402 (*root)->avl_data = minnode->avl_data;
403 minnode->avl_data = savedata;
405 savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
408 if ( shortersubtree )
409 *shorter = right_balance( root );
413 } else if ( cmp < 0 ) {
414 if ( (savedata = ravl_delete( &(*root)->avl_left, data, fcmp,
415 &shortersubtree )) == 0 ) {
420 /* left subtree shorter? */
421 if ( shortersubtree )
422 *shorter = left_balance( root );
427 if ( (savedata = ravl_delete( &(*root)->avl_right, data, fcmp,
428 &shortersubtree )) == 0 ) {
433 if ( shortersubtree )
434 *shorter = right_balance( root );
443 * avl_delete() - deletes the node containing data (according to fcmp) from
444 * the avl tree rooted at root.
448 avl_delete( Avlnode **root, void* data, AVL_CMP fcmp )
452 return( ravl_delete( root, data, fcmp, &shorter ) );
456 avl_inapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
459 return( AVL_NOMORE );
461 if ( root->avl_left != 0 )
462 if ( avl_inapply( root->avl_left, fn, arg, stopflag )
466 if ( (*fn)( root->avl_data, arg ) == stopflag )
469 if ( root->avl_right == 0 )
470 return( AVL_NOMORE );
472 return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
476 avl_postapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
479 return( AVL_NOMORE );
481 if ( root->avl_left != 0 )
482 if ( avl_postapply( root->avl_left, fn, arg, stopflag )
486 if ( root->avl_right != 0 )
487 if ( avl_postapply( root->avl_right, fn, arg, stopflag )
491 return( (*fn)( root->avl_data, arg ) );
495 avl_preapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
498 return( AVL_NOMORE );
500 if ( (*fn)( root->avl_data, arg ) == stopflag )
503 if ( root->avl_left != 0 )
504 if ( avl_preapply( root->avl_left, fn, arg, stopflag )
508 if ( root->avl_right == 0 )
509 return( AVL_NOMORE );
511 return( avl_preapply( root->avl_right, fn, arg, stopflag ) );
515 * avl_apply -- avl tree root is traversed, function fn is called with
516 * arguments arg and the data portion of each node. if fn returns stopflag,
517 * the traversal is cut short, otherwise it continues. Do not use -6 as
518 * a stopflag, as this is what is used to indicate the traversal ran out
523 avl_apply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag, int type )
527 return( avl_inapply( root, fn, arg, stopflag ) );
529 return( avl_preapply( root, fn, arg, stopflag ) );
531 return( avl_postapply( root, fn, arg, stopflag ) );
533 fprintf( stderr, "Invalid traversal type %d\n", type );
541 * avl_prefixapply - traverse avl tree root, applying function fprefix
542 * to any nodes that match. fcmp is called with data as its first arg
543 * and the current node's data as its second arg. it should return
544 * 0 if they match, < 0 if data is less, and > 0 if data is greater.
545 * the idea is to efficiently find all nodes that are prefixes of
546 * some key... Like avl_apply, this routine also takes a stopflag
547 * and will return prematurely if fmatch returns this value. Otherwise,
548 * AVL_NOMORE is returned.
565 return( AVL_NOMORE );
567 cmp = (*fcmp)( data, root->avl_data /* , carg */);
569 if ( (*fmatch)( root->avl_data, marg ) == stopflag )
572 if ( root->avl_left != 0 )
573 if ( avl_prefixapply( root->avl_left, data, fmatch,
574 marg, fcmp, carg, stopflag ) == stopflag )
577 if ( root->avl_right != 0 )
578 return( avl_prefixapply( root->avl_right, data, fmatch,
579 marg, fcmp, carg, stopflag ) );
581 return( AVL_NOMORE );
583 } else if ( cmp < 0 ) {
584 if ( root->avl_left != 0 )
585 return( avl_prefixapply( root->avl_left, data, fmatch,
586 marg, fcmp, carg, stopflag ) );
588 if ( root->avl_right != 0 )
589 return( avl_prefixapply( root->avl_right, data, fmatch,
590 marg, fcmp, carg, stopflag ) );
593 return( AVL_NOMORE );
597 * avl_free -- traverse avltree root, freeing the memory it is using.
598 * the dfree() is called to free the data portion of each node. The
599 * number of items actually freed is returned.
603 avl_free( Avlnode *root, AVL_FREE dfree )
611 if ( root->avl_left != 0 )
612 nleft = avl_free( root->avl_left, dfree );
614 if ( root->avl_right != 0 )
615 nright = avl_free( root->avl_right, dfree );
618 (*dfree)( root->avl_data );
621 return( nleft + nright + 1 );
625 * avl_find -- search avltree root for a node with data data. the function
626 * cmp is used to compare things. it is called with data as its first arg
627 * and the current node data as its second. it should return 0 if they match,
628 * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
632 avl_find( Avlnode *root, const void* data, AVL_CMP fcmp )
636 while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
638 root = root->avl_left;
640 root = root->avl_right;
643 return( root ? root->avl_data : 0 );
647 * avl_find_lin -- search avltree root linearly for a node with data data.
648 * the function cmp is used to compare things. it is called with data as its
649 * first arg and the current node data as its second. it should return 0 if
650 * they match, non-zero otherwise.
654 avl_find_lin( Avlnode *root, const void* data, AVL_CMP fcmp )
661 if ( (*fcmp)( data, root->avl_data ) == 0 )
662 return( root->avl_data );
664 if ( root->avl_left != 0 )
665 if ( (res = avl_find_lin( root->avl_left, data, fcmp ))
669 if ( root->avl_right == 0 )
672 return( avl_find_lin( root->avl_right, data, fcmp ) );
675 /* NON-REENTRANT INTERFACE */
677 static void* *avl_list;
678 static int avl_maxlist;
679 static int avl_nextlist;
681 #define AVL_GRABSIZE 100
685 avl_buildlist( void* data, void* arg )
689 if ( avl_list == (void* *) 0 ) {
690 avl_list = (void* *) ber_memalloc(AVL_GRABSIZE * sizeof(void*));
691 slots = AVL_GRABSIZE;
693 } else if ( avl_maxlist == slots ) {
694 slots += AVL_GRABSIZE;
695 avl_list = (void* *) ber_memrealloc( (char *) avl_list,
696 (unsigned) slots * sizeof(void*));
699 avl_list[ avl_maxlist++ ] = data;
705 * avl_getfirst() and avl_getnext() are provided as alternate tree
706 * traversal methods, to be used when a single function cannot be
707 * provided to be called with every node in the tree. avl_getfirst()
708 * traverses the tree and builds a linear list of all the nodes,
709 * returning the first node. avl_getnext() returns the next thing
710 * on the list built by avl_getfirst(). This means that avl_getfirst()
711 * can take a while, and that the tree should not be messed with while
712 * being traversed in this way, and that multiple traversals (even of
713 * different trees) cannot be active at once.
717 avl_getfirst( Avlnode *root )
720 free( (char *) avl_list);
721 avl_list = (void* *) 0;
729 (void) avl_apply( root, avl_buildlist, (void*) 0, -1, AVL_INORDER );
731 return( avl_list[ avl_nextlist++ ] );
740 if ( avl_nextlist == avl_maxlist ) {
741 free( (void*) avl_list);
742 avl_list = (void* *) 0;
746 return( avl_list[ avl_nextlist++ ] );
749 /* end non-reentrant code */
753 avl_dup_error( void* left, void* right )
759 avl_dup_ok( void* left, void* right )