1 /* avl.c - routines to implement an avl tree */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1993 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP). Additional significant contributors
31 * Hallvard B. Furuseth
38 #include <ac/stdlib.h>
41 #define ber_memalloc malloc
42 #define ber_memrealloc realloc
43 #define ber_memfree free
51 static const int avl_bfs[] = {LH, RH};
54 * avl_insert -- insert a node containing data data into the avl tree
55 * with root root. fcmp is a function to call to compare the data portion
56 * of two nodes. it should take two arguments and return <, >, or == 0,
57 * depending on whether its first argument is <, >, or == its second
58 * argument (like strcmp, e.g.). fdup is a function to call when a duplicate
59 * node is inserted. it should return 0, or -1 and its return value
60 * will be the return value from avl_insert in the case of a duplicate node.
61 * the function will be called with the original node's data as its first
62 * argument and with the incoming duplicate node's data as its second
63 * argument. this could be used, for example, to keep a count with each
66 * NOTE: this routine may malloc memory
69 avl_insert( Avlnode ** root, void *data, AVL_CMP fcmp, AVL_DUP fdup )
71 Avlnode *t, *p, *s, *q, *r;
74 if ( *root == NULL ) {
75 if (( r = (Avlnode *) ber_memalloc( sizeof( Avlnode ))) == NULL ) {
78 r->avl_link[0] = r->avl_link[1] = NULL;
89 /* find insertion point */
91 cmp = fcmp( data, p->avl_data );
93 return (*fdup)( p->avl_data, data );
99 if (( q = (Avlnode *) ber_memalloc( sizeof( Avlnode ))) == NULL ) {
102 q->avl_link[0] = q->avl_link[1] = NULL;
106 p->avl_link[cmp] = q;
108 } else if ( q->avl_bf ) {
115 /* adjust balance factors */
116 cmp = fcmp( data, s->avl_data ) > 0;
117 r = p = s->avl_link[cmp];
121 cmp = fcmp( data, p->avl_data ) > 0;
122 p->avl_bf = avl_bfs[cmp];
123 p = p->avl_link[cmp];
126 /* checks and balances */
128 if ( s->avl_bf == EH ) {
131 } else if ( s->avl_bf == -a ) {
134 } else if ( s->avl_bf == a ) {
137 if ( r->avl_bf == a ) {
138 /* single rotation */
140 s->avl_link[cmp] = r->avl_link[ncmp];
141 r->avl_link[ncmp] = s;
144 } else if ( r->avl_bf == -a ) {
145 /* double rotation */
146 p = r->avl_link[ncmp];
147 r->avl_link[ncmp] = p->avl_link[cmp];
148 p->avl_link[cmp] = r;
149 s->avl_link[cmp] = p->avl_link[ncmp];
150 p->avl_link[ncmp] = s;
152 if ( p->avl_bf == a ) {
155 } else if ( p->avl_bf == -a ) {
167 else if ( s == t->avl_right )
177 avl_delete( Avlnode **root, void* data, AVL_CMP fcmp )
179 Avlnode *p, *q, *r, *top;
180 int side, side_bf, shorter, nside;
183 Avlnode *pptr[sizeof(void *)*8];
184 unsigned char pdir[sizeof(void *)*8];
193 side = fcmp( data, p->avl_data );
200 p = p->avl_link[side];
206 /* If this node has two children, swap so we are deleting a node with
209 if ( p->avl_link[0] && p->avl_link[1] ) {
211 /* find the immediate predecessor <q> */
215 while (q->avl_link[1]) {
222 p->avl_link[0] = q->avl_link[0];
225 q->avl_link[1] = p->avl_link[1];
226 p->avl_link[1] = NULL;
228 q->avl_bf = p->avl_bf;
230 /* fix stack positions: old parent of p points to q */
235 r->avl_link[pdir[side]] = q;
239 /* new parent of p points to p */
248 /* now <p> has at most one child, get it */
258 /* set the child into p's parent */
262 p->avl_link[side] = q;
271 side_bf = avl_bfs[side];
273 /* case 1: height unchanged */
274 if ( p->avl_bf == EH ) {
275 /* Tree is now heavier on opposite side */
276 p->avl_bf = avl_bfs[nside];
279 } else if ( p->avl_bf == side_bf ) {
280 /* case 2: taller subtree shortened, height reduced */
283 /* case 3: shorter subtree shortened */
285 top = pptr[depth-1]; /* p->parent; */
288 /* set <q> to the taller of the two subtrees of <p> */
289 q = p->avl_link[nside];
290 if ( q->avl_bf == EH ) {
291 /* case 3a: height unchanged, single rotate */
292 p->avl_link[nside] = q->avl_link[side];
293 q->avl_link[side] = p;
296 p->avl_bf = (- side_bf);
298 } else if ( q->avl_bf == p->avl_bf ) {
299 /* case 3b: height reduced, single rotate */
300 p->avl_link[nside] = q->avl_link[side];
301 q->avl_link[side] = p;
307 /* case 3c: height reduced, balance factors opposite */
308 r = q->avl_link[side];
309 q->avl_link[side] = r->avl_link[nside];
310 r->avl_link[nside] = q;
312 p->avl_link[nside] = r->avl_link[side];
313 r->avl_link[side] = p;
315 if ( r->avl_bf == side_bf ) {
316 q->avl_bf = (- side_bf);
318 } else if ( r->avl_bf == (- side_bf)) {
328 /* a rotation has caused <q> (or <r> in case 3c) to become
329 * the root. let <p>'s former parent know this.
333 } else if (top->avl_link[0] == p) {
334 top->avl_link[0] = q;
336 top->avl_link[1] = q;
344 } /* end while(shorter) */
350 avl_inapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
353 return( AVL_NOMORE );
355 if ( root->avl_left != 0 )
356 if ( avl_inapply( root->avl_left, fn, arg, stopflag )
360 if ( (*fn)( root->avl_data, arg ) == stopflag )
363 if ( root->avl_right == 0 )
364 return( AVL_NOMORE );
366 return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
370 avl_postapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
373 return( AVL_NOMORE );
375 if ( root->avl_left != 0 )
376 if ( avl_postapply( root->avl_left, fn, arg, stopflag )
380 if ( root->avl_right != 0 )
381 if ( avl_postapply( root->avl_right, fn, arg, stopflag )
385 return( (*fn)( root->avl_data, arg ) );
389 avl_preapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
392 return( AVL_NOMORE );
394 if ( (*fn)( root->avl_data, arg ) == stopflag )
397 if ( root->avl_left != 0 )
398 if ( avl_preapply( root->avl_left, fn, arg, stopflag )
402 if ( root->avl_right == 0 )
403 return( AVL_NOMORE );
405 return( avl_preapply( root->avl_right, fn, arg, stopflag ) );
409 * avl_apply -- avl tree root is traversed, function fn is called with
410 * arguments arg and the data portion of each node. if fn returns stopflag,
411 * the traversal is cut short, otherwise it continues. Do not use -6 as
412 * a stopflag, as this is what is used to indicate the traversal ran out
417 avl_apply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag, int type )
421 return( avl_inapply( root, fn, arg, stopflag ) );
423 return( avl_preapply( root, fn, arg, stopflag ) );
425 return( avl_postapply( root, fn, arg, stopflag ) );
427 fprintf( stderr, "Invalid traversal type %d\n", type );
435 * avl_prefixapply - traverse avl tree root, applying function fprefix
436 * to any nodes that match. fcmp is called with data as its first arg
437 * and the current node's data as its second arg. it should return
438 * 0 if they match, < 0 if data is less, and > 0 if data is greater.
439 * the idea is to efficiently find all nodes that are prefixes of
440 * some key... Like avl_apply, this routine also takes a stopflag
441 * and will return prematurely if fmatch returns this value. Otherwise,
442 * AVL_NOMORE is returned.
459 return( AVL_NOMORE );
461 cmp = (*fcmp)( data, root->avl_data /* , carg */);
463 if ( (*fmatch)( root->avl_data, marg ) == stopflag )
466 if ( root->avl_left != 0 )
467 if ( avl_prefixapply( root->avl_left, data, fmatch,
468 marg, fcmp, carg, stopflag ) == stopflag )
471 if ( root->avl_right != 0 )
472 return( avl_prefixapply( root->avl_right, data, fmatch,
473 marg, fcmp, carg, stopflag ) );
475 return( AVL_NOMORE );
477 } else if ( cmp < 0 ) {
478 if ( root->avl_left != 0 )
479 return( avl_prefixapply( root->avl_left, data, fmatch,
480 marg, fcmp, carg, stopflag ) );
482 if ( root->avl_right != 0 )
483 return( avl_prefixapply( root->avl_right, data, fmatch,
484 marg, fcmp, carg, stopflag ) );
487 return( AVL_NOMORE );
491 * avl_free -- traverse avltree root, freeing the memory it is using.
492 * the dfree() is called to free the data portion of each node. The
493 * number of items actually freed is returned.
497 avl_free( Avlnode *root, AVL_FREE dfree )
505 if ( root->avl_left != 0 )
506 nleft = avl_free( root->avl_left, dfree );
508 if ( root->avl_right != 0 )
509 nright = avl_free( root->avl_right, dfree );
512 (*dfree)( root->avl_data );
515 return( nleft + nright + 1 );
519 * avl_find -- search avltree root for a node with data data. the function
520 * cmp is used to compare things. it is called with data as its first arg
521 * and the current node data as its second. it should return 0 if they match,
522 * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
526 avl_find2( Avlnode *root, const void *data, AVL_CMP fcmp )
530 while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
532 root = root->avl_left;
534 root = root->avl_right;
540 avl_find( Avlnode *root, const void* data, AVL_CMP fcmp )
544 while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
546 root = root->avl_left;
548 root = root->avl_right;
551 return( root ? root->avl_data : 0 );
555 * avl_find_lin -- search avltree root linearly for a node with data data.
556 * the function cmp is used to compare things. it is called with data as its
557 * first arg and the current node data as its second. it should return 0 if
558 * they match, non-zero otherwise.
562 avl_find_lin( Avlnode *root, const void* data, AVL_CMP fcmp )
569 if ( (*fcmp)( data, root->avl_data ) == 0 )
570 return( root->avl_data );
572 if ( root->avl_left != 0 )
573 if ( (res = avl_find_lin( root->avl_left, data, fcmp ))
577 if ( root->avl_right == 0 )
580 return( avl_find_lin( root->avl_right, data, fcmp ) );
583 /* NON-REENTRANT INTERFACE */
585 static void* *avl_list;
586 static int avl_maxlist;
587 static int avl_nextlist;
589 #define AVL_GRABSIZE 100
593 avl_buildlist( void* data, void* arg )
597 if ( avl_list == (void* *) 0 ) {
598 avl_list = (void* *) ber_memalloc(AVL_GRABSIZE * sizeof(void*));
599 slots = AVL_GRABSIZE;
601 } else if ( avl_maxlist == slots ) {
602 slots += AVL_GRABSIZE;
603 avl_list = (void* *) ber_memrealloc( (char *) avl_list,
604 (unsigned) slots * sizeof(void*));
607 avl_list[ avl_maxlist++ ] = data;
613 * avl_getfirst() and avl_getnext() are provided as alternate tree
614 * traversal methods, to be used when a single function cannot be
615 * provided to be called with every node in the tree. avl_getfirst()
616 * traverses the tree and builds a linear list of all the nodes,
617 * returning the first node. avl_getnext() returns the next thing
618 * on the list built by avl_getfirst(). This means that avl_getfirst()
619 * can take a while, and that the tree should not be messed with while
620 * being traversed in this way, and that multiple traversals (even of
621 * different trees) cannot be active at once.
625 avl_getfirst( Avlnode *root )
628 ber_memfree( (char *) avl_list);
629 avl_list = (void* *) 0;
637 (void) avl_apply( root, avl_buildlist, (void*) 0, -1, AVL_INORDER );
639 return( avl_list[ avl_nextlist++ ] );
648 if ( avl_nextlist == avl_maxlist ) {
649 ber_memfree( (void*) avl_list);
650 avl_list = (void* *) 0;
654 return( avl_list[ avl_nextlist++ ] );
657 /* end non-reentrant code */
661 avl_dup_error( void* left, void* right )
667 avl_dup_ok( void* left, void* right )