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/09 04:09:51 crh
36 * This is my library of lists and trees. My hope is to replace all of the
37 * hard coded linked lists that are currently used in Samba with calls to
38 * these modules. This should make the code simpler, smaller, and (I hope)
39 * faster. The tree code, in particular, should speed up processing where
40 * large lists are involved.
44 * Revision 2.4 1997/07/26 04:36:20 crh
45 * Andrew Leppard, aka "Grazgur", discovered that I still had my brains tied
46 * on backwards with respect to node deletion. I did some more digging and
47 * discovered that I was not changing the balance values correctly in the
48 * single rotation functions. Double rotation was working correctly because
49 * the formula for changing the balance values is the same for insertion or
50 * deletion. Not so for single rotation.
52 * I have tested the fix by loading the tree with over 44 thousand names,
53 * deleting 2,629 of them (all those in which the second character is 'u')
54 * and then walking the tree recursively to verify that the balance factor of
55 * each node is correct. Passed.
60 * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
61 * + Rewrote the ubi_tr<func> macros because they weren't doing what I'd
62 * hoped they would do (see the bottom of the header file). They work now.
64 * Revision 2.3 1997/06/03 04:41:35 crh
65 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
68 * Revision 2.2 1995/10/03 22:16:01 CRH
71 * Revision 2.1 95/03/09 23:45:59 CRH
72 * Added the ModuleID static string and function. These modules are now
75 * Revision 2.0 95/03/05 14:10:51 CRH
76 * This revision of ubi_AVLtree coincides with revision 2.0 of ubi_BinTree,
77 * and so includes all of the changes to that module. In addition, a bug in
78 * the node deletion process has been fixed.
80 * After rewriting the Locate() function in ubi_BinTree, I decided that it was
81 * time to overhaul this module. In the process, I discovered a bug related
82 * to node deletion. To fix the bug, I wrote function Debalance(). A quick
83 * glance will show that it is very similar to the Rebalance() function. In
84 * previous versions of this module, I tried to include the functionality of
85 * Debalance() within Rebalance(), with poor results.
87 * Revision 1.0 93/10/15 22:58:56 CRH
88 * With this revision, I have added a set of #define's that provide a single,
89 * standard API to all existing tree modules. Until now, each of the three
90 * existing modules had a different function and typedef prefix, as follows:
95 * ubi_SplayTree ubi_spt
97 * To further complicate matters, only those portions of the base module
98 * (ubi_BinTree) that were superceeded in the new module had the new names.
99 * For example, if you were using ubi_AVLtree, the AVL node structure was
100 * named "ubi_avlNode", but the root structure was still "ubi_btRoot". Using
101 * SplayTree, the locate function was called "ubi_sptLocate", but the next
102 * and previous functions remained "ubi_btNext" and "ubi_btPrev".
104 * This was not too terrible if you were familiar with the modules and knew
105 * exactly which tree model you wanted to use. If you wanted to be able to
106 * change modules (for speed comparisons, etc), things could get messy very
109 * So, I have added a set of defined names that get redefined in any of the
110 * descendant modules. To use this standardized interface in your code,
111 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
112 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
113 * datatype names for the module that you are using. Just remember to
114 * include the header for that module in your program file. Because these
115 * names are handled by the preprocessor, there is no added run-time
118 * Note that the original names do still exist, and can be used if you wish
119 * to write code directly to a specific module. This should probably only be
120 * done if you are planning to implement a new descendant type, such as
121 * red/black trees. CRH
123 * V0.0 - May, 1990 - Written by Christopher R. Hertel (CRH).
125 * ========================================================================= **
128 #include "ubi_AVLtree.h" /* Header for THIS module. */
129 #include <stdlib.h> /* Standard C definitions, etc. */
131 /* ========================================================================== **
135 static char ModuleID[] = "ubi_AVLtree\n\
136 \t$Revision: 1.1 $\n\
137 \t$Date: 1997/10/09 04:09:51 $\n\
140 /* ========================================================================== **
141 * The next set of functions are the AVL balancing routines. There are left
142 * and right, single and double rotations. The rotation routines handle the
143 * rotations and reconnect all tree pointers that might get confused by the
144 * rotations. A pointer to the new subtree root node is returned.
146 * Note that L1 and R1 are identical, except that all the RIGHTs and LEFTs
147 * are reversed. The same is true for L2 and R2. I'm sure that there is
148 * a clever way to reduce the amount of code by combining these functions,
149 * but it might involve additional overhead, and it would probably be a pain
150 * to read, debug, etc.
151 * -------------------------------------------------------------------------- **
154 static ubi_avlNodePtr L1( ubi_avlNodePtr p )
155 /* ------------------------------------------------------------------------ **
156 * Single rotate left.
158 * Input: p - Pointer to the root of a tree (possibly a subtree).
159 * Output: A pointer to the new root of the same subtree (now that node
161 * ------------------------------------------------------------------------ **
166 tmp = p->Link[RIGHT];
167 p->Link[RIGHT] = tmp->Link[LEFT];
170 tmp->Link[PARENT] = p->Link[PARENT];
171 tmp->gender = p->gender;
172 if(tmp->Link[PARENT])
173 (tmp->Link[PARENT])->Link[(tmp->gender)] = tmp;
174 p->Link[PARENT] = tmp;
178 p->Link[RIGHT]->Link[PARENT] = p;
179 (p->Link[RIGHT])->gender = RIGHT;
181 p->balance -= Normalize( tmp->balance );
186 static ubi_avlNodePtr R1( ubi_avlNodePtr p )
187 /* ------------------------------------------------------------------------ **
188 * Single rotate right.
190 * Input: p - Pointer to the root of a tree (possibly a subtree).
191 * Output: A pointer to the new root of the same subtree (now that node
193 * ------------------------------------------------------------------------ **
199 p->Link[LEFT] = tmp->Link[RIGHT];
200 tmp->Link[RIGHT] = p;
202 tmp->Link[PARENT] = p->Link[PARENT];
203 tmp->gender = p->gender;
204 if(tmp->Link[PARENT])
205 (tmp->Link[PARENT])->Link[(tmp->gender)] = tmp;
206 p->Link[PARENT] = tmp;
210 p->Link[LEFT]->Link[PARENT] = p;
211 p->Link[LEFT]->gender = LEFT;
213 p->balance -= Normalize( tmp->balance );
218 static ubi_avlNodePtr L2( ubi_avlNodePtr tree )
219 /* ------------------------------------------------------------------------ **
220 * Double rotate left.
222 * Input: p - Pointer to the root of a tree (possibly a subtree).
223 * Output: A pointer to the new root of the same subtree (now that node
225 * ------------------------------------------------------------------------ **
228 ubi_avlNodePtr tmp, newroot;
230 tmp = tree->Link[RIGHT];
231 newroot = tmp->Link[LEFT];
232 tmp->Link[LEFT] = newroot->Link[RIGHT];
233 newroot->Link[RIGHT] = tmp;
234 tree->Link[RIGHT] = newroot->Link[LEFT];
235 newroot->Link[LEFT] = tree;
237 newroot->Link[PARENT] = tree->Link[PARENT];
238 newroot->gender = tree->gender;
239 tree->Link[PARENT] = newroot;
241 tmp->Link[PARENT] = newroot;
244 if( tree->Link[RIGHT] )
246 tree->Link[RIGHT]->Link[PARENT] = tree;
247 tree->Link[RIGHT]->gender = RIGHT;
249 if( tmp->Link[LEFT] )
251 tmp->Link[LEFT]->Link[PARENT] = tmp;
252 tmp->Link[LEFT]->gender = LEFT;
254 if(newroot->Link[PARENT])
255 newroot->Link[PARENT]->Link[newroot->gender] = newroot;
257 switch( newroot->balance )
260 tree->balance = EQUAL; tmp->balance = RIGHT; break;
262 tree->balance = EQUAL; tmp->balance = EQUAL; break;
264 tree->balance = LEFT; tmp->balance = EQUAL; break;
266 newroot->balance = EQUAL;
270 static ubi_avlNodePtr R2( ubi_avlNodePtr tree )
271 /* ------------------------------------------------------------------------ **
272 * Double rotate right.
274 * Input: p - Pointer to the root of a tree (possibly a subtree).
275 * Output: A pointer to the new root of the same subtree (now that node
277 * ------------------------------------------------------------------------ **
280 ubi_avlNodePtr tmp, newroot;
282 tmp = tree->Link[LEFT];
283 newroot = tmp->Link[RIGHT];
284 tmp->Link[RIGHT] = newroot->Link[LEFT];
285 newroot->Link[LEFT] = tmp;
286 tree->Link[LEFT] = newroot->Link[RIGHT];
287 newroot->Link[RIGHT] = tree;
289 newroot->Link[PARENT] = tree->Link[PARENT];
290 newroot->gender = tree->gender;
291 tree->Link[PARENT] = newroot;
292 tree->gender = RIGHT;
293 tmp->Link[PARENT] = newroot;
296 if( tree->Link[LEFT] )
298 tree->Link[LEFT]->Link[PARENT] = tree;
299 tree->Link[LEFT]->gender = LEFT;
301 if( tmp->Link[RIGHT] )
303 tmp->Link[RIGHT]->Link[PARENT] = tmp;
304 tmp->Link[RIGHT]->gender = RIGHT;
306 if(newroot->Link[PARENT])
307 newroot->Link[PARENT]->Link[newroot->gender] = newroot;
309 switch( newroot->balance )
312 tree->balance = RIGHT; tmp->balance = EQUAL; break;
314 tree->balance = EQUAL; tmp->balance = EQUAL; break;
316 tree->balance = EQUAL; tmp->balance = LEFT; break;
318 newroot->balance = EQUAL;
323 static ubi_avlNodePtr Adjust( ubi_avlNodePtr p, char LorR )
324 /* ------------------------------------------------------------------------ **
325 * Adjust the balance value at node *p. If necessary, rotate the subtree
328 * Input: p - A pointer to the node to be adjusted. One of the
329 * subtrees of this node has changed height, so the
330 * balance value at this node must be adjusted, possibly
331 * by rotating the tree at this node.
332 * LorR - Indicates the TALLER subtree.
334 * Output: A pointer to the (possibly new) root node of the subtree.
336 * Notes: This function may be called after a node has been added *or*
337 * deleted, so LorR indicates the TALLER subtree.
338 * ------------------------------------------------------------------------ **
341 if( p->balance != LorR )
342 p->balance += Normalize(LorR);
345 char tallerbal; /* Balance value of the root of the taller subtree of p. */
347 tallerbal = p->Link[LorR]->balance;
348 if( ( EQUAL == tallerbal ) || ( p->balance == tallerbal ) )
349 p = ( (LEFT==LorR) ? R1(p) : L1(p) ); /* single rotation */
351 p = ( (LEFT==LorR) ? R2(p) : L2(p) ); /* double rotation */
356 static ubi_avlNodePtr Rebalance( ubi_avlNodePtr Root,
357 ubi_avlNodePtr subtree,
359 /* ------------------------------------------------------------------------ **
360 * Rebalance the tree following an insertion.
362 * Input: Root - A pointer to the root node of the whole tree.
363 * subtree - A pointer to the node that has just gained a new
365 * LorR - Gender of the child that has just been gained.
367 * Output: A pointer to the (possibly new) root of the AVL tree.
368 * Rebalancing the tree moves nodes around a bit, so the node
369 * that *was* the root, may not be the root when we're finished.
371 * Notes: Rebalance() must walk up the tree from where we are (which is
372 * where the latest change occurred), rebalancing the subtrees
373 * along the way. The rebalancing operation can stop if the
374 * change at the current subtree root won't affect the rest of
375 * the tree. In the case of an addition, if a subtree root's
376 * balance becomes EQUAL, then we know that the height of that
377 * subtree has not changed, so we can exit.
378 * ------------------------------------------------------------------------ **
383 subtree = Adjust( subtree, LorR );
384 if( PARENT == subtree->gender )
386 if( EQUAL == subtree->balance )
388 LorR = subtree->gender;
389 subtree = subtree->Link[PARENT];
394 static ubi_avlNodePtr Debalance( ubi_avlNodePtr Root,
395 ubi_avlNodePtr subtree,
397 /* ------------------------------------------------------------------------ **
398 * Rebalance the tree following a deletion.
400 * Input: Root - A pointer to the root node of the whole tree.
401 * subtree - A pointer to the node who's child has just "left the
403 * LorR - Gender of the child that left.
405 * Output: A pointer to the (possibly new) root of the AVL tree.
406 * Rebalancing the tree moves nodes around a bit, so the node
407 * that *was* the root, may not be the root when we're finished.
409 * Notes: Debalance() is subtly different from Rebalance() (above) in
411 * * When it calls Adjust(), it passes the *opposite* of LorR.
412 * This is because LorR, as passed into Debalance() indicates
413 * the shorter subtree. As we move up the tree, LorR is
414 * assigned the gender of the node that we are leaving (i.e.,
415 * the subtree that we just rebalanced).
416 * * We know that a subtree has not changed height if the
417 * balance becomes LEFT or RIGHT. This is the *opposite* of
418 * what happens in Rebalance().
419 * ------------------------------------------------------------------------ **
424 subtree = Adjust( subtree, RevWay(LorR) );
425 if( PARENT == subtree->gender )
427 if( EQUAL != subtree->balance )
429 LorR = subtree->gender;
430 subtree = subtree->Link[PARENT];
436 /* -------------------------------------------------------------------------- **
437 * The next two functions are used for general tree manipulation. They are
438 * each slightly different from their ubi_BinTree counterparts.
439 * -------------------------------------------------------------------------- **
442 static void ReplaceNode( ubi_avlNodePtr *parent,
443 ubi_avlNodePtr oldnode,
444 ubi_avlNodePtr newnode )
445 /* ------------------------------------------------------------------------ **
446 * Remove node oldnode from the tree, replacing it with node newnode.
449 * parent - A pointer to he parent pointer of the node to be
450 * replaced. <parent> may point to the Link[] field of
451 * a parent node, or it may indicate the root pointer at
452 * the top of the tree.
453 * oldnode - A pointer to the node that is to be replaced.
454 * newnode - A pointer to the node that is to be installed in the
455 * place of <*oldnode>.
457 * Notes: Don't forget to free oldnode.
458 * The only difference between this function and the ubi_bt
459 * version is that the node size is sizeof( ubi_avlNode ), not
460 * sizeof( ubi_btNode ).
461 * ------------------------------------------------------------------------ **
465 register int avlNodeSize = sizeof( ubi_avlNode );
467 for( i = 0; i < avlNodeSize; i++ )
468 ((unsigned char *)newnode)[i] = ((unsigned char *)oldnode)[i];
471 if(oldnode->Link[LEFT ] )
472 (oldnode->Link[LEFT ])->Link[PARENT] = newnode;
473 if(oldnode->Link[RIGHT] )
474 (oldnode->Link[RIGHT])->Link[PARENT] = newnode;
477 static void SwapNodes( ubi_btRootPtr RootPtr,
478 ubi_avlNodePtr Node1,
479 ubi_avlNodePtr Node2 )
480 /* ------------------------------------------------------------------------ **
481 * This function swaps two nodes in the tree. Node1 will take the place of
482 * Node2, and Node2 will fill in the space left vacant by Node 1.
485 * RootPtr - pointer to the tree header structure for this tree.
487 * > These are the two nodes which are to be swapped.
491 * This function does a three step swap, using a dummy node as a place
492 * holder. This function is used by ubi_avlRemove().
493 * The only difference between this function and its ubi_bt counterpart
494 * is that the nodes are ubi_avlNodes, not ubi_btNodes.
495 * ------------------------------------------------------------------------ **
498 ubi_avlNodePtr *Parent;
500 ubi_avlNodePtr dummy_p = &dummy;
502 if( Node1->Link[PARENT] )
503 Parent = &((Node1->Link[PARENT])->Link[Node1->gender]);
505 Parent = (ubi_avlNodePtr *)&(RootPtr->root);
506 ReplaceNode( Parent, Node1, dummy_p );
508 if( Node2->Link[PARENT] )
509 Parent = &((Node2->Link[PARENT])->Link[Node2->gender]);
511 Parent = (ubi_avlNodePtr *)&(RootPtr->root);
512 ReplaceNode( Parent, Node2, Node1 );
514 if( dummy_p->Link[PARENT] )
515 Parent = &((dummy_p->Link[PARENT])->Link[dummy_p->gender]);
517 Parent = (ubi_avlNodePtr *)&(RootPtr->root);
518 ReplaceNode( Parent, dummy_p, Node2 );
522 /* ========================================================================== **
523 * Public, exported (ie. not static-ly declared) functions...
524 * -------------------------------------------------------------------------- **
527 ubi_avlNodePtr ubi_avlInitNode( ubi_avlNodePtr NodePtr )
528 /* ------------------------------------------------------------------------ **
529 * Initialize a tree node.
531 * Input: NodePtr - pointer to a ubi_btNode structure to be
533 * Output: a pointer to the initialized ubi_avlNode structure (ie. the
534 * same as the input pointer).
535 * ------------------------------------------------------------------------ **
538 (void)ubi_btInitNode( (ubi_btNodePtr)NodePtr );
539 NodePtr->balance = EQUAL;
541 } /* ubi_avlInitNode */
543 ubi_trBool ubi_avlInsert( ubi_btRootPtr RootPtr,
544 ubi_avlNodePtr NewNode,
545 ubi_btItemPtr ItemPtr,
546 ubi_avlNodePtr *OldNode )
547 /* ------------------------------------------------------------------------ **
548 * This function uses a non-recursive algorithm to add a new element to
551 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
552 * the root of the tree to which NewNode is to be added.
553 * NewNode - a pointer to an ubi_avlNode structure that is NOT
555 * ItemPtr - A pointer to the sort key that is stored within
556 * *NewNode. ItemPtr MUST point to information stored
557 * in *NewNode or an EXACT DUPLICATE. The key data
558 * indicated by ItemPtr is used to place the new node
560 * OldNode - a pointer to an ubi_btNodePtr. When searching
561 * the tree, a duplicate node may be found. If
562 * duplicates are allowed, then the new node will
563 * be simply placed into the tree. If duplicates
564 * are not allowed, however, then one of two things
566 * 1) if overwritting *is not* allowed, this
567 * function will return FALSE (indicating that
568 * the new node could not be inserted), and
569 * *OldNode will point to the duplicate that is
571 * 2) if overwritting *is* allowed, then this
572 * function will swap **OldNode for *NewNode.
573 * In this case, *OldNode will point to the node
574 * that was removed (thus allowing you to free
576 * ** If you are using overwrite mode, ALWAYS **
577 * ** check the return value of this parameter! **
578 * Note: You may pass NULL in this parameter, the
579 * function knows how to cope. If you do this,
580 * however, there will be no way to return a
581 * pointer to an old (ie. replaced) node (which is
582 * a problem if you are using overwrite mode).
584 * Output: a boolean value indicating success or failure. The function
585 * will return FALSE if the node could not be added to the tree.
586 * Such failure will only occur if duplicates are not allowed,
587 * nodes cannot be overwritten, AND a duplicate key was found
589 * ------------------------------------------------------------------------ **
592 ubi_avlNodePtr OtherP;
594 if( !(OldNode) ) OldNode = &OtherP;
595 if( ubi_btInsert( RootPtr,
596 (ubi_btNodePtr)NewNode,
598 (ubi_btNodePtr *)OldNode ) )
601 NewNode->balance = (*OldNode)->balance;
604 NewNode->balance = EQUAL;
605 RootPtr->root = (ubi_btNodePtr)Rebalance( (ubi_avlNodePtr)RootPtr->root,
606 NewNode->Link[PARENT],
609 return( ubi_trTRUE );
611 return( ubi_trFALSE ); /* Failure: could not replace an existing node. */
612 } /* ubi_avlInsert */
614 ubi_avlNodePtr ubi_avlRemove( ubi_btRootPtr RootPtr,
615 ubi_avlNodePtr DeadNode )
616 /* ------------------------------------------------------------------------ **
617 * This function removes the indicated node from the tree, after which the
618 * tree is rebalanced.
620 * Input: RootPtr - A pointer to the header of the tree that contains
621 * the node to be removed.
622 * DeadNode - A pointer to the node that will be removed.
624 * Output: This function returns a pointer to the node that was removed
625 * from the tree (ie. the same as DeadNode).
627 * Note: The node MUST be in the tree indicated by RootPtr. If not,
628 * strange and evil things will happen to your trees.
629 * ------------------------------------------------------------------------ **
635 /* if the node has both left and right subtrees, then we have to swap
636 * it with another node.
638 if( (DeadNode->Link[LEFT]) && (DeadNode->Link[RIGHT]) )
639 SwapNodes( RootPtr, DeadNode, ubi_trPrev( DeadNode ) );
641 /* The parent of the node to be deleted may be another node, or it may be
642 * the root of the tree. Since we're not sure, it's best just to have
643 * a pointer to the parent pointer, whatever it is.
645 if( DeadNode->Link[PARENT] )
646 parentp = (ubi_btNodePtr *)
647 &((DeadNode->Link[PARENT])->Link[(DeadNode->gender)]);
649 parentp = &( RootPtr->root );
651 /* Now link the parent to the only grand-child. Patch up the gender and
652 * such, and rebalance.
654 if( EQUAL == DeadNode->balance )
658 p = (ubi_btNodePtr)(DeadNode->Link[(DeadNode->balance)]);
659 p->Link[PARENT] = (ubi_btNodePtr)DeadNode->Link[PARENT];
660 p->gender = DeadNode->gender;
663 RootPtr->root = (ubi_btNodePtr)Debalance( (ubi_avlNodePtr)RootPtr->root,
664 DeadNode->Link[PARENT],
669 } /* ubi_avlRemove */
671 int ubi_avlModuleID( int size, char *list[] )
672 /* ------------------------------------------------------------------------ **
673 * Returns a set of strings that identify the module.
675 * Input: size - The number of elements in the array <list>.
676 * list - An array of pointers of type (char *). This array
677 * should, initially, be empty. This function will fill
678 * in the array with pointers to strings.
679 * Output: The number of elements of <list> that were used. If this value
680 * is less than <size>, the values of the remaining elements are
683 * Notes: Please keep in mind that the pointers returned indicate strings
684 * stored in static memory. Don't free() them, don't write over
685 * them, etc. Just read them.
686 * ------------------------------------------------------------------------ **
693 return( 1 + ubi_btModuleID( --size, &(list[1]) ) );
697 } /* ubi_avlModuleID */
699 /* ============================== The End ============================== */