1 /* ========================================================================== **
4 * Copyright (C) 1991-1997 by Christopher R. Hertel
6 * Email: crh@ubiqx.mn.org
7 * -------------------------------------------------------------------------- **
9 * This module provides an implementation of AVL height balanced binary
10 * trees. (Adelson-Velskii, Landis 1962)
12 * This file implements the core of the height-balanced (AVL) tree management
13 * routines. The header file, ubi_AVLtree.h, contains function prototypes
14 * for all "exported" functions.
16 * -------------------------------------------------------------------------- **
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Library General Public
20 * License as published by the Free Software Foundation; either
21 * version 2 of the License, or (at your option) any later version.
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Library General Public License for more details.
28 * You should have received a copy of the GNU Library General Public
29 * License along with this library; if not, write to the Free
30 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 * -------------------------------------------------------------------------- **
34 * $Log: ubi_AVLtree.c,v $
35 * Revision 1.1 1997/10/10 14:46:36 crh
36 * This is the ubiqx binary tree and linked list library.
37 * This library is being included as part of the Samba distribution.
40 * Revision 2.4 1997/07/26 04:36:20 crh
41 * Andrew Leppard, aka "Grazgur", discovered that I still had my brains tied
42 * on backwards with respect to node deletion. I did some more digging and
43 * discovered that I was not changing the balance values correctly in the
44 * single rotation functions. Double rotation was working correctly because
45 * the formula for changing the balance values is the same for insertion or
46 * deletion. Not so for single rotation.
48 * I have tested the fix by loading the tree with over 44 thousand names,
49 * deleting 2,629 of them (all those in which the second character is 'u')
50 * and then walking the tree recursively to verify that the balance factor of
51 * each node is correct. Passed.
56 * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
57 * + Rewrote the ubi_tr<func> macros because they weren't doing what I'd
58 * hoped they would do (see the bottom of the header file). They work now.
60 * Revision 2.3 1997/06/03 04:41:35 crh
61 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
64 * Revision 2.2 1995/10/03 22:16:01 CRH
67 * Revision 2.1 95/03/09 23:45:59 CRH
68 * Added the ModuleID static string and function. These modules are now
71 * Revision 2.0 95/03/05 14:10:51 CRH
72 * This revision of ubi_AVLtree coincides with revision 2.0 of ubi_BinTree,
73 * and so includes all of the changes to that module. In addition, a bug in
74 * the node deletion process has been fixed.
76 * After rewriting the Locate() function in ubi_BinTree, I decided that it was
77 * time to overhaul this module. In the process, I discovered a bug related
78 * to node deletion. To fix the bug, I wrote function Debalance(). A quick
79 * glance will show that it is very similar to the Rebalance() function. In
80 * previous versions of this module, I tried to include the functionality of
81 * Debalance() within Rebalance(), with poor results.
83 * Revision 1.0 93/10/15 22:58:56 CRH
84 * With this revision, I have added a set of #define's that provide a single,
85 * standard API to all existing tree modules. Until now, each of the three
86 * existing modules had a different function and typedef prefix, as follows:
91 * ubi_SplayTree ubi_spt
93 * To further complicate matters, only those portions of the base module
94 * (ubi_BinTree) that were superceeded in the new module had the new names.
95 * For example, if you were using ubi_AVLtree, the AVL node structure was
96 * named "ubi_avlNode", but the root structure was still "ubi_btRoot". Using
97 * SplayTree, the locate function was called "ubi_sptLocate", but the next
98 * and previous functions remained "ubi_btNext" and "ubi_btPrev".
100 * This was not too terrible if you were familiar with the modules and knew
101 * exactly which tree model you wanted to use. If you wanted to be able to
102 * change modules (for speed comparisons, etc), things could get messy very
105 * So, I have added a set of defined names that get redefined in any of the
106 * descendant modules. To use this standardized interface in your code,
107 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
108 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
109 * datatype names for the module that you are using. Just remember to
110 * include the header for that module in your program file. Because these
111 * names are handled by the preprocessor, there is no added run-time
114 * Note that the original names do still exist, and can be used if you wish
115 * to write code directly to a specific module. This should probably only be
116 * done if you are planning to implement a new descendant type, such as
117 * red/black trees. CRH
119 * V0.0 - May, 1990 - Written by Christopher R. Hertel (CRH).
121 * ========================================================================= **
124 #include "ubi_AVLtree.h" /* Header for THIS module. */
125 #include <stdlib.h> /* Standard C definitions, etc. */
127 /* ========================================================================== **
131 static char ModuleID[] = "ubi_AVLtree\n\
132 \t$Revision: 1.1 $\n\
133 \t$Date: 1997/10/10 14:46:36 $\n\
136 /* ========================================================================== **
137 * The next set of functions are the AVL balancing routines. There are left
138 * and right, single and double rotations. The rotation routines handle the
139 * rotations and reconnect all tree pointers that might get confused by the
140 * rotations. A pointer to the new subtree root node is returned.
142 * Note that L1 and R1 are identical, except that all the RIGHTs and LEFTs
143 * are reversed. The same is true for L2 and R2. I'm sure that there is
144 * a clever way to reduce the amount of code by combining these functions,
145 * but it might involve additional overhead, and it would probably be a pain
146 * to read, debug, etc.
147 * -------------------------------------------------------------------------- **
150 static ubi_avlNodePtr L1( ubi_avlNodePtr p )
151 /* ------------------------------------------------------------------------ **
152 * Single rotate left.
154 * Input: p - Pointer to the root of a tree (possibly a subtree).
155 * Output: A pointer to the new root of the same subtree (now that node
157 * ------------------------------------------------------------------------ **
162 tmp = p->Link[RIGHT];
163 p->Link[RIGHT] = tmp->Link[LEFT];
166 tmp->Link[PARENT] = p->Link[PARENT];
167 tmp->gender = p->gender;
168 if(tmp->Link[PARENT])
169 (tmp->Link[PARENT])->Link[(tmp->gender)] = tmp;
170 p->Link[PARENT] = tmp;
174 p->Link[RIGHT]->Link[PARENT] = p;
175 (p->Link[RIGHT])->gender = RIGHT;
177 p->balance -= Normalize( tmp->balance );
182 static ubi_avlNodePtr R1( ubi_avlNodePtr p )
183 /* ------------------------------------------------------------------------ **
184 * Single rotate right.
186 * Input: p - Pointer to the root of a tree (possibly a subtree).
187 * Output: A pointer to the new root of the same subtree (now that node
189 * ------------------------------------------------------------------------ **
195 p->Link[LEFT] = tmp->Link[RIGHT];
196 tmp->Link[RIGHT] = p;
198 tmp->Link[PARENT] = p->Link[PARENT];
199 tmp->gender = p->gender;
200 if(tmp->Link[PARENT])
201 (tmp->Link[PARENT])->Link[(tmp->gender)] = tmp;
202 p->Link[PARENT] = tmp;
206 p->Link[LEFT]->Link[PARENT] = p;
207 p->Link[LEFT]->gender = LEFT;
209 p->balance -= Normalize( tmp->balance );
214 static ubi_avlNodePtr L2( ubi_avlNodePtr tree )
215 /* ------------------------------------------------------------------------ **
216 * Double rotate left.
218 * Input: p - Pointer to the root of a tree (possibly a subtree).
219 * Output: A pointer to the new root of the same subtree (now that node
221 * ------------------------------------------------------------------------ **
224 ubi_avlNodePtr tmp, newroot;
226 tmp = tree->Link[RIGHT];
227 newroot = tmp->Link[LEFT];
228 tmp->Link[LEFT] = newroot->Link[RIGHT];
229 newroot->Link[RIGHT] = tmp;
230 tree->Link[RIGHT] = newroot->Link[LEFT];
231 newroot->Link[LEFT] = tree;
233 newroot->Link[PARENT] = tree->Link[PARENT];
234 newroot->gender = tree->gender;
235 tree->Link[PARENT] = newroot;
237 tmp->Link[PARENT] = newroot;
240 if( tree->Link[RIGHT] )
242 tree->Link[RIGHT]->Link[PARENT] = tree;
243 tree->Link[RIGHT]->gender = RIGHT;
245 if( tmp->Link[LEFT] )
247 tmp->Link[LEFT]->Link[PARENT] = tmp;
248 tmp->Link[LEFT]->gender = LEFT;
250 if(newroot->Link[PARENT])
251 newroot->Link[PARENT]->Link[newroot->gender] = newroot;
253 switch( newroot->balance )
256 tree->balance = EQUAL; tmp->balance = RIGHT; break;
258 tree->balance = EQUAL; tmp->balance = EQUAL; break;
260 tree->balance = LEFT; tmp->balance = EQUAL; break;
262 newroot->balance = EQUAL;
266 static ubi_avlNodePtr R2( ubi_avlNodePtr tree )
267 /* ------------------------------------------------------------------------ **
268 * Double rotate right.
270 * Input: p - Pointer to the root of a tree (possibly a subtree).
271 * Output: A pointer to the new root of the same subtree (now that node
273 * ------------------------------------------------------------------------ **
276 ubi_avlNodePtr tmp, newroot;
278 tmp = tree->Link[LEFT];
279 newroot = tmp->Link[RIGHT];
280 tmp->Link[RIGHT] = newroot->Link[LEFT];
281 newroot->Link[LEFT] = tmp;
282 tree->Link[LEFT] = newroot->Link[RIGHT];
283 newroot->Link[RIGHT] = tree;
285 newroot->Link[PARENT] = tree->Link[PARENT];
286 newroot->gender = tree->gender;
287 tree->Link[PARENT] = newroot;
288 tree->gender = RIGHT;
289 tmp->Link[PARENT] = newroot;
292 if( tree->Link[LEFT] )
294 tree->Link[LEFT]->Link[PARENT] = tree;
295 tree->Link[LEFT]->gender = LEFT;
297 if( tmp->Link[RIGHT] )
299 tmp->Link[RIGHT]->Link[PARENT] = tmp;
300 tmp->Link[RIGHT]->gender = RIGHT;
302 if(newroot->Link[PARENT])
303 newroot->Link[PARENT]->Link[newroot->gender] = newroot;
305 switch( newroot->balance )
308 tree->balance = RIGHT; tmp->balance = EQUAL; break;
310 tree->balance = EQUAL; tmp->balance = EQUAL; break;
312 tree->balance = EQUAL; tmp->balance = LEFT; break;
314 newroot->balance = EQUAL;
319 static ubi_avlNodePtr Adjust( ubi_avlNodePtr p, char LorR )
320 /* ------------------------------------------------------------------------ **
321 * Adjust the balance value at node *p. If necessary, rotate the subtree
324 * Input: p - A pointer to the node to be adjusted. One of the
325 * subtrees of this node has changed height, so the
326 * balance value at this node must be adjusted, possibly
327 * by rotating the tree at this node.
328 * LorR - Indicates the TALLER subtree.
330 * Output: A pointer to the (possibly new) root node of the subtree.
332 * Notes: This function may be called after a node has been added *or*
333 * deleted, so LorR indicates the TALLER subtree.
334 * ------------------------------------------------------------------------ **
337 if( p->balance != LorR )
338 p->balance += Normalize(LorR);
341 char tallerbal; /* Balance value of the root of the taller subtree of p. */
343 tallerbal = p->Link[LorR]->balance;
344 if( ( EQUAL == tallerbal ) || ( p->balance == tallerbal ) )
345 p = ( (LEFT==LorR) ? R1(p) : L1(p) ); /* single rotation */
347 p = ( (LEFT==LorR) ? R2(p) : L2(p) ); /* double rotation */
352 static ubi_avlNodePtr Rebalance( ubi_avlNodePtr Root,
353 ubi_avlNodePtr subtree,
355 /* ------------------------------------------------------------------------ **
356 * Rebalance the tree following an insertion.
358 * Input: Root - A pointer to the root node of the whole tree.
359 * subtree - A pointer to the node that has just gained a new
361 * LorR - Gender of the child that has just been gained.
363 * Output: A pointer to the (possibly new) root of the AVL tree.
364 * Rebalancing the tree moves nodes around a bit, so the node
365 * that *was* the root, may not be the root when we're finished.
367 * Notes: Rebalance() must walk up the tree from where we are (which is
368 * where the latest change occurred), rebalancing the subtrees
369 * along the way. The rebalancing operation can stop if the
370 * change at the current subtree root won't affect the rest of
371 * the tree. In the case of an addition, if a subtree root's
372 * balance becomes EQUAL, then we know that the height of that
373 * subtree has not changed, so we can exit.
374 * ------------------------------------------------------------------------ **
379 subtree = Adjust( subtree, LorR );
380 if( PARENT == subtree->gender )
382 if( EQUAL == subtree->balance )
384 LorR = subtree->gender;
385 subtree = subtree->Link[PARENT];
390 static ubi_avlNodePtr Debalance( ubi_avlNodePtr Root,
391 ubi_avlNodePtr subtree,
393 /* ------------------------------------------------------------------------ **
394 * Rebalance the tree following a deletion.
396 * Input: Root - A pointer to the root node of the whole tree.
397 * subtree - A pointer to the node who's child has just "left the
399 * LorR - Gender of the child that left.
401 * Output: A pointer to the (possibly new) root of the AVL tree.
402 * Rebalancing the tree moves nodes around a bit, so the node
403 * that *was* the root, may not be the root when we're finished.
405 * Notes: Debalance() is subtly different from Rebalance() (above) in
407 * * When it calls Adjust(), it passes the *opposite* of LorR.
408 * This is because LorR, as passed into Debalance() indicates
409 * the shorter subtree. As we move up the tree, LorR is
410 * assigned the gender of the node that we are leaving (i.e.,
411 * the subtree that we just rebalanced).
412 * * We know that a subtree has not changed height if the
413 * balance becomes LEFT or RIGHT. This is the *opposite* of
414 * what happens in Rebalance().
415 * ------------------------------------------------------------------------ **
420 subtree = Adjust( subtree, RevWay(LorR) );
421 if( PARENT == subtree->gender )
423 if( EQUAL != subtree->balance )
425 LorR = subtree->gender;
426 subtree = subtree->Link[PARENT];
432 /* -------------------------------------------------------------------------- **
433 * The next two functions are used for general tree manipulation. They are
434 * each slightly different from their ubi_BinTree counterparts.
435 * -------------------------------------------------------------------------- **
438 static void ReplaceNode( ubi_avlNodePtr *parent,
439 ubi_avlNodePtr oldnode,
440 ubi_avlNodePtr newnode )
441 /* ------------------------------------------------------------------------ **
442 * Remove node oldnode from the tree, replacing it with node newnode.
445 * parent - A pointer to he parent pointer of the node to be
446 * replaced. <parent> may point to the Link[] field of
447 * a parent node, or it may indicate the root pointer at
448 * the top of the tree.
449 * oldnode - A pointer to the node that is to be replaced.
450 * newnode - A pointer to the node that is to be installed in the
451 * place of <*oldnode>.
453 * Notes: Don't forget to free oldnode.
454 * The only difference between this function and the ubi_bt
455 * version is that the node size is sizeof( ubi_avlNode ), not
456 * sizeof( ubi_btNode ).
457 * ------------------------------------------------------------------------ **
461 register int avlNodeSize = sizeof( ubi_avlNode );
463 for( i = 0; i < avlNodeSize; i++ )
464 ((unsigned char *)newnode)[i] = ((unsigned char *)oldnode)[i];
467 if(oldnode->Link[LEFT ] )
468 (oldnode->Link[LEFT ])->Link[PARENT] = newnode;
469 if(oldnode->Link[RIGHT] )
470 (oldnode->Link[RIGHT])->Link[PARENT] = newnode;
473 static void SwapNodes( ubi_btRootPtr RootPtr,
474 ubi_avlNodePtr Node1,
475 ubi_avlNodePtr Node2 )
476 /* ------------------------------------------------------------------------ **
477 * This function swaps two nodes in the tree. Node1 will take the place of
478 * Node2, and Node2 will fill in the space left vacant by Node 1.
481 * RootPtr - pointer to the tree header structure for this tree.
483 * > These are the two nodes which are to be swapped.
487 * This function does a three step swap, using a dummy node as a place
488 * holder. This function is used by ubi_avlRemove().
489 * The only difference between this function and its ubi_bt counterpart
490 * is that the nodes are ubi_avlNodes, not ubi_btNodes.
491 * ------------------------------------------------------------------------ **
494 ubi_avlNodePtr *Parent;
496 ubi_avlNodePtr dummy_p = &dummy;
498 if( Node1->Link[PARENT] )
499 Parent = &((Node1->Link[PARENT])->Link[Node1->gender]);
501 Parent = (ubi_avlNodePtr *)&(RootPtr->root);
502 ReplaceNode( Parent, Node1, dummy_p );
504 if( Node2->Link[PARENT] )
505 Parent = &((Node2->Link[PARENT])->Link[Node2->gender]);
507 Parent = (ubi_avlNodePtr *)&(RootPtr->root);
508 ReplaceNode( Parent, Node2, Node1 );
510 if( dummy_p->Link[PARENT] )
511 Parent = &((dummy_p->Link[PARENT])->Link[dummy_p->gender]);
513 Parent = (ubi_avlNodePtr *)&(RootPtr->root);
514 ReplaceNode( Parent, dummy_p, Node2 );
518 /* ========================================================================== **
519 * Public, exported (ie. not static-ly declared) functions...
520 * -------------------------------------------------------------------------- **
523 ubi_avlNodePtr ubi_avlInitNode( ubi_avlNodePtr NodePtr )
524 /* ------------------------------------------------------------------------ **
525 * Initialize a tree node.
527 * Input: NodePtr - pointer to a ubi_btNode structure to be
529 * Output: a pointer to the initialized ubi_avlNode structure (ie. the
530 * same as the input pointer).
531 * ------------------------------------------------------------------------ **
534 (void)ubi_btInitNode( (ubi_btNodePtr)NodePtr );
535 NodePtr->balance = EQUAL;
537 } /* ubi_avlInitNode */
539 ubi_trBool ubi_avlInsert( ubi_btRootPtr RootPtr,
540 ubi_avlNodePtr NewNode,
541 ubi_btItemPtr ItemPtr,
542 ubi_avlNodePtr *OldNode )
543 /* ------------------------------------------------------------------------ **
544 * This function uses a non-recursive algorithm to add a new element to
547 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
548 * the root of the tree to which NewNode is to be added.
549 * NewNode - a pointer to an ubi_avlNode structure that is NOT
551 * ItemPtr - A pointer to the sort key that is stored within
552 * *NewNode. ItemPtr MUST point to information stored
553 * in *NewNode or an EXACT DUPLICATE. The key data
554 * indicated by ItemPtr is used to place the new node
556 * OldNode - a pointer to an ubi_btNodePtr. When searching
557 * the tree, a duplicate node may be found. If
558 * duplicates are allowed, then the new node will
559 * be simply placed into the tree. If duplicates
560 * are not allowed, however, then one of two things
562 * 1) if overwritting *is not* allowed, this
563 * function will return FALSE (indicating that
564 * the new node could not be inserted), and
565 * *OldNode will point to the duplicate that is
567 * 2) if overwritting *is* allowed, then this
568 * function will swap **OldNode for *NewNode.
569 * In this case, *OldNode will point to the node
570 * that was removed (thus allowing you to free
572 * ** If you are using overwrite mode, ALWAYS **
573 * ** check the return value of this parameter! **
574 * Note: You may pass NULL in this parameter, the
575 * function knows how to cope. If you do this,
576 * however, there will be no way to return a
577 * pointer to an old (ie. replaced) node (which is
578 * a problem if you are using overwrite mode).
580 * Output: a boolean value indicating success or failure. The function
581 * will return FALSE if the node could not be added to the tree.
582 * Such failure will only occur if duplicates are not allowed,
583 * nodes cannot be overwritten, AND a duplicate key was found
585 * ------------------------------------------------------------------------ **
588 ubi_avlNodePtr OtherP;
590 if( !(OldNode) ) OldNode = &OtherP;
591 if( ubi_btInsert( RootPtr,
592 (ubi_btNodePtr)NewNode,
594 (ubi_btNodePtr *)OldNode ) )
597 NewNode->balance = (*OldNode)->balance;
600 NewNode->balance = EQUAL;
601 RootPtr->root = (ubi_btNodePtr)Rebalance( (ubi_avlNodePtr)RootPtr->root,
602 NewNode->Link[PARENT],
605 return( ubi_trTRUE );
607 return( ubi_trFALSE ); /* Failure: could not replace an existing node. */
608 } /* ubi_avlInsert */
610 ubi_avlNodePtr ubi_avlRemove( ubi_btRootPtr RootPtr,
611 ubi_avlNodePtr DeadNode )
612 /* ------------------------------------------------------------------------ **
613 * This function removes the indicated node from the tree, after which the
614 * tree is rebalanced.
616 * Input: RootPtr - A pointer to the header of the tree that contains
617 * the node to be removed.
618 * DeadNode - A pointer to the node that will be removed.
620 * Output: This function returns a pointer to the node that was removed
621 * from the tree (ie. the same as DeadNode).
623 * Note: The node MUST be in the tree indicated by RootPtr. If not,
624 * strange and evil things will happen to your trees.
625 * ------------------------------------------------------------------------ **
631 /* if the node has both left and right subtrees, then we have to swap
632 * it with another node.
634 if( (DeadNode->Link[LEFT]) && (DeadNode->Link[RIGHT]) )
635 SwapNodes( RootPtr, DeadNode, ubi_trPrev( DeadNode ) );
637 /* The parent of the node to be deleted may be another node, or it may be
638 * the root of the tree. Since we're not sure, it's best just to have
639 * a pointer to the parent pointer, whatever it is.
641 if( DeadNode->Link[PARENT] )
642 parentp = (ubi_btNodePtr *)
643 &((DeadNode->Link[PARENT])->Link[(DeadNode->gender)]);
645 parentp = &( RootPtr->root );
647 /* Now link the parent to the only grand-child. Patch up the gender and
648 * such, and rebalance.
650 if( EQUAL == DeadNode->balance )
654 p = (ubi_btNodePtr)(DeadNode->Link[(DeadNode->balance)]);
655 p->Link[PARENT] = (ubi_btNodePtr)DeadNode->Link[PARENT];
656 p->gender = DeadNode->gender;
659 RootPtr->root = (ubi_btNodePtr)Debalance( (ubi_avlNodePtr)RootPtr->root,
660 DeadNode->Link[PARENT],
665 } /* ubi_avlRemove */
667 int ubi_avlModuleID( int size, char *list[] )
668 /* ------------------------------------------------------------------------ **
669 * Returns a set of strings that identify the module.
671 * Input: size - The number of elements in the array <list>.
672 * list - An array of pointers of type (char *). This array
673 * should, initially, be empty. This function will fill
674 * in the array with pointers to strings.
675 * Output: The number of elements of <list> that were used. If this value
676 * is less than <size>, the values of the remaining elements are
679 * Notes: Please keep in mind that the pointers returned indicate strings
680 * stored in static memory. Don't free() them, don't write over
681 * them, etc. Just read them.
682 * ------------------------------------------------------------------------ **
689 return( 1 + ubi_btModuleID( --size, &(list[1]) ) );
693 } /* ubi_avlModuleID */
695 /* ============================== The End ============================== */