3 /* ========================================================================== **
6 * Copyright (C) 1991-1998 by Christopher R. Hertel
8 * Email: crh@ubiqx.mn.org
9 * -------------------------------------------------------------------------- **
11 * This module provides an implementation of AVL height balanced binary
12 * trees. (Adelson-Velskii, Landis 1962)
14 * This header file contains the basic AVL structure and pointer typedefs
15 * as well as the prototypes needed to access the functions in the AVL
16 * module ubi_AVLtree. The .c file implements the low-level height balancing
17 * routines that manage the AVL tree, plus all of the basic primops for
18 * adding, searching for, and deleting nodes.
20 * -------------------------------------------------------------------------- **
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Library General Public
24 * License as published by the Free Software Foundation; either
25 * version 2 of the License, or (at your option) any later version.
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Library General Public License for more details.
32 * You should have received a copy of the GNU Library General Public
33 * License along with this library; if not, write to the Free
34 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 * -------------------------------------------------------------------------- **
37 * Log: ubi_AVLtree.h,v
38 * Revision 4.0 1998/03/10 03:34:45 crh
40 * By adding the AVL balance field to the base ubi_btNode structure, I no
41 * longer need AVL-specific ReplaceNode(), SwapNodes(), and InitNode()
42 * functions. The Remove() function is also simplified. It's all much
44 * This is rev. 4.0. The 3.x series was dropped.
46 * Revision 2.5 1997/12/23 04:00:15 crh
47 * In this version, all constants & macros defined in the header file have
48 * the ubi_tr prefix. Also cleaned up anything that gcc complained about
49 * when run with '-pedantic -fsyntax-only -Wall'.
51 * Revision 2.4 1997/07/26 04:36:23 crh
52 * Andrew Leppard, aka "Grazgur", discovered that I still had my brains tied
53 * on backwards with respect to node deletion. I did some more digging and
54 * discovered that I was not changing the balance values correctly in the
55 * single rotation functions. Double rotation was working correctly because
56 * the formula for changing the balance values is the same for insertion or
57 * deletion. Not so for single rotation.
59 * I have tested the fix by loading the tree with over 44 thousand names,
60 * deleting 2,629 of them (all those in which the second character is 'u')
61 * and then walking the tree recursively to verify that the balance factor of
62 * each node is correct. Passed.
67 * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
68 * + Rewrote the ubi_tr<func> macros because they weren't doing what I'd
69 * hoped they would do (see the bottom of the header file). They work now.
71 * Revision 2.3 1997/06/03 05:22:07 crh
72 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
75 * Revision 2.2 1995/10/03 22:15:47 CRH
78 * Revision 2.1 95/03/09 23:46:44 CRH
79 * Added the ModuleID static string and function. These modules are now
82 * Revision 2.0 95/03/05 14:11:22 CRH
83 * This revision of ubi_AVLtree coincides with revision 2.0 of ubi_BinTree,
84 * and so includes all of the changes to that module. In addition, a bug in
85 * the node deletion process has been fixed.
87 * After rewriting the Locate() function in ubi_BinTree, I decided that it was
88 * time to overhaul this module. In the process, I discovered a bug related
89 * to node deletion. To fix the bug, I wrote function Debalance(). A quick
90 * glance will show that it is very similar to the Rebalance() function. In
91 * previous versions of this module, I tried to include the functionality of
92 * Debalance() within Rebalance(), with poor results.
94 * Revision 1.0 93/10/15 22:58:48 CRH
95 * With this revision, I have added a set of #define's that provide a single,
96 * standard API to all existing tree modules. Until now, each of the three
97 * existing modules had a different function and typedef prefix, as follows:
101 * ubi_AVLtree ubi_avl
102 * ubi_SplayTree ubi_spt
104 * To further complicate matters, only those portions of the base module
105 * (ubi_BinTree) that were superceeded in the new module had the new names.
106 * For example, if you were using ubi_SplayTree, the locate function was
107 * called "ubi_sptLocate", but the next and previous functions remained
108 * "ubi_btNext" and "ubi_btPrev".
110 * This was not too terrible if you were familiar with the modules and knew
111 * exactly which tree model you wanted to use. If you wanted to be able to
112 * change modules (for speed comparisons, etc), things could get messy very
115 * So, I have added a set of defined names that get redefined in any of the
116 * descendant modules. To use this standardized interface in your code,
117 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
118 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
119 * datatype names for the module that you are using. Just remember to
120 * include the header for that module in your program file. Because these
121 * names are handled by the preprocessor, there is no added run-time
124 * Note that the original names do still exist, and can be used if you wish
125 * to write code directly to a specific module. This should probably only be
126 * done if you are planning to implement a new descendant type, such as
127 * red/black trees. CRH
129 * V0.0 - May, 1990 - Written by Christopher R. Hertel (CRH).
131 * ========================================================================= **
134 #include "ubi_BinTree.h" /* Base erg binary tree support. */
136 /* -------------------------------------------------------------------------- **
137 * Function prototypes.
138 * -------------------------------------------------------------------------- **
141 ubi_trBool ubi_avlInsert( ubi_btRootPtr RootPtr,
142 ubi_btNodePtr NewNode,
143 ubi_btItemPtr ItemPtr,
144 ubi_btNodePtr *OldNode );
145 /* ------------------------------------------------------------------------ **
146 * This function uses a non-recursive algorithm to add a new element to
149 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
150 * the root of the tree to which NewNode is to be added.
151 * NewNode - a pointer to an ubi_btNode structure that is NOT
153 * ItemPtr - A pointer to the sort key that is stored within
154 * *NewNode. ItemPtr MUST point to information stored
155 * in *NewNode or an EXACT DUPLICATE. The key data
156 * indicated by ItemPtr is used to place the new node
158 * OldNode - a pointer to an ubi_btNodePtr. When searching
159 * the tree, a duplicate node may be found. If
160 * duplicates are allowed, then the new node will
161 * be simply placed into the tree. If duplicates
162 * are not allowed, however, then one of two things
164 * 1) if overwritting *is not* allowed, this
165 * function will return FALSE (indicating that
166 * the new node could not be inserted), and
167 * *OldNode will point to the duplicate that is
169 * 2) if overwritting *is* allowed, then this
170 * function will swap **OldNode for *NewNode.
171 * In this case, *OldNode will point to the node
172 * that was removed (thus allowing you to free
174 * ** If you are using overwrite mode, ALWAYS **
175 * ** check the return value of this parameter! **
176 * Note: You may pass NULL in this parameter, the
177 * function knows how to cope. If you do this,
178 * however, there will be no way to return a
179 * pointer to an old (ie. replaced) node (which is
180 * a problem if you are using overwrite mode).
182 * Output: a boolean value indicating success or failure. The function
183 * will return FALSE if the node could not be added to the tree.
184 * Such failure will only occur if duplicates are not allowed,
185 * nodes cannot be overwritten, AND a duplicate key was found
187 * ------------------------------------------------------------------------ **
190 ubi_btNodePtr ubi_avlRemove( ubi_btRootPtr RootPtr,
191 ubi_btNodePtr DeadNode );
192 /* ------------------------------------------------------------------------ **
193 * This function removes the indicated node from the tree, after which the
194 * tree is rebalanced.
196 * Input: RootPtr - A pointer to the header of the tree that contains
197 * the node to be removed.
198 * DeadNode - A pointer to the node that will be removed.
200 * Output: This function returns a pointer to the node that was removed
201 * from the tree (ie. the same as DeadNode).
203 * Note: The node MUST be in the tree indicated by RootPtr. If not,
204 * strange and evil things will happen to your trees.
205 * ------------------------------------------------------------------------ **
208 int ubi_avlModuleID( int size, char *list[] );
209 /* ------------------------------------------------------------------------ **
210 * Returns a set of strings that identify the module.
212 * Input: size - The number of elements in the array <list>.
213 * list - An array of pointers of type (char *). This array
214 * should, initially, be empty. This function will fill
215 * in the array with pointers to strings.
216 * Output: The number of elements of <list> that were used. If this value
217 * is less than <size>, the values of the remaining elements are
220 * Notes: Please keep in mind that the pointers returned indicate strings
221 * stored in static memory. Don't free() them, don't write over
222 * them, etc. Just read them.
223 * ------------------------------------------------------------------------ **
226 /* -------------------------------------------------------------------------- **
229 * This set of defines allows you to write programs that will use any of the
230 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
231 * Instead of using ubi_avl... or ubi_bt, use ubi_tr... and select the tree
232 * type by including the appropriate module header.
236 #define ubi_trInsert( Rp, Nn, Ip, On ) \
237 ubi_avlInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
238 (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
241 #define ubi_trRemove( Rp, Dn ) \
242 ubi_avlRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
244 #undef ubi_trModuleID
245 #define ubi_trModuleID( s, l ) ubi_avlModuleID( s, l )
248 /* =========================== End ubi_AVLtree.h =========================== */
249 #endif /* ubi_AVLtree_H */