3 /* ========================================================================== **
6 * Copyright (C) 1991-1997 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 1.1 1997/10/09 04:09:51 crh
39 * This is my library of lists and trees. My hope is to replace all of the
40 * hard coded linked lists that are currently used in Samba with calls to
41 * these modules. This should make the code simpler, smaller, and (I hope)
42 * faster. The tree code, in particular, should speed up processing where
43 * large lists are involved.
47 * Revision 2.4 1997/07/26 04:36:23 crh
48 * Andrew Leppard, aka "Grazgur", discovered that I still had my brains tied
49 * on backwards with respect to node deletion. I did some more digging and
50 * discovered that I was not changing the balance values correctly in the
51 * single rotation functions. Double rotation was working correctly because
52 * the formula for changing the balance values is the same for insertion or
53 * deletion. Not so for single rotation.
55 * I have tested the fix by loading the tree with over 44 thousand names,
56 * deleting 2,629 of them (all those in which the second character is 'u')
57 * and then walking the tree recursively to verify that the balance factor of
58 * each node is correct. Passed.
63 * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
64 * + Rewrote the ubi_tr<func> macros because they weren't doing what I'd
65 * hoped they would do (see the bottom of the header file). They work now.
67 * Revision 2.3 1997/06/03 05:22:07 crh
68 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
71 * Revision 2.2 1995/10/03 22:15:47 CRH
74 * Revision 2.1 95/03/09 23:46:44 CRH
75 * Added the ModuleID static string and function. These modules are now
78 * Revision 2.0 95/03/05 14:11:22 CRH
79 * This revision of ubi_AVLtree coincides with revision 2.0 of ubi_BinTree,
80 * and so includes all of the changes to that module. In addition, a bug in
81 * the node deletion process has been fixed.
83 * After rewriting the Locate() function in ubi_BinTree, I decided that it was
84 * time to overhaul this module. In the process, I discovered a bug related
85 * to node deletion. To fix the bug, I wrote function Debalance(). A quick
86 * glance will show that it is very similar to the Rebalance() function. In
87 * previous versions of this module, I tried to include the functionality of
88 * Debalance() within Rebalance(), with poor results.
90 * Revision 1.0 93/10/15 22:58:48 CRH
91 * With this revision, I have added a set of #define's that provide a single,
92 * standard API to all existing tree modules. Until now, each of the three
93 * existing modules had a different function and typedef prefix, as follows:
98 * ubi_SplayTree ubi_spt
100 * To further complicate matters, only those portions of the base module
101 * (ubi_BinTree) that were superceeded in the new module had the new names.
102 * For example, if you were using ubi_AVLtree, the AVL node structure was
103 * named "ubi_avlNode", but the root structure was still "ubi_btRoot". Using
104 * SplayTree, the locate function was called "ubi_sptLocate", but the next
105 * and previous functions remained "ubi_btNext" and "ubi_btPrev".
107 * This was not too terrible if you were familiar with the modules and knew
108 * exactly which tree model you wanted to use. If you wanted to be able to
109 * change modules (for speed comparisons, etc), things could get messy very
112 * So, I have added a set of defined names that get redefined in any of the
113 * descendant modules. To use this standardized interface in your code,
114 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
115 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
116 * datatype names for the module that you are using. Just remember to
117 * include the header for that module in your program file. Because these
118 * names are handled by the preprocessor, there is no added run-time
121 * Note that the original names do still exist, and can be used if you wish
122 * to write code directly to a specific module. This should probably only be
123 * done if you are planning to implement a new descendant type, such as
124 * red/black trees. CRH
126 * V0.0 - May, 1990 - Written by Christopher R. Hertel (CRH).
128 * ========================================================================= **
131 #include "ubi_BinTree.h" /* Base erg binary tree support. */
133 /* ------------------------------------------------------------------------- **
134 * AVL Tree Node Structure: This structure defines the basic elements of
135 * the AVL tree nodes. In general you *SHOULD NOT PLAY WITH THESE
136 * FIELDS*! But, of course, I have to put the structure into this
137 * header so that you can use the structure as a building block.
139 * The fields are as follows:
140 * Link - An array of pointers. These pointers are manipulated by the
141 * BT and AVL routines, and indicate the left and right child
142 * nodes, plus the parent node. By keeping track of the parent
143 * pointer, we avoid the need for recursive routines or hand-
144 * tooled stacks to keep track of our path back to the root.
145 * The use of these pointers is subject to change without
147 * gender - For tree rebalancing purposes, it is necessary that each node
148 * know whether it is the left or right child of its parent, or
149 * if it is the root. This information is stored in this field.
150 * balance - This field is also needed for AVL balancing purposes. It
151 * indicates which subtree of the current node is longer, or if
152 * the subtrees are, in fact, balanced with respect to each
154 * ------------------------------------------------------------------------- **
157 typedef struct ubi_avlNodeStruct {
158 struct ubi_avlNodeStruct
159 *Link[3]; /* Normal Binary Tree Node type. */
160 char gender; /* The node is either the RIGHT or LEFT child of its */
161 /* parent, or is the root node. */
162 char balance; /* In an AVL tree, each node is the root of a subtree */
163 /* that may be balanced, or be one node longer to the */
164 /* right or left. This field keeps track of the */
165 /* balance value of each node. */
166 } ubi_avlNode; /* Typedef'd name for an avl tree node. */
168 typedef ubi_avlNode *ubi_avlNodePtr; /* a Pointer to an AVL node */
170 /* -------------------------------------------------------------------------- **
171 * Function prototypes.
172 * -------------------------------------------------------------------------- **
175 ubi_avlNodePtr ubi_avlInitNode( ubi_avlNodePtr NodePtr );
176 /* ------------------------------------------------------------------------ **
177 * Initialize a tree node.
179 * Input: NodePtr - a pointer to a ubi_btNode structure to be
181 * Output: a pointer to the initialized ubi_avlNode structure (ie. the
182 * same as the input pointer).
183 * ------------------------------------------------------------------------ **
186 ubi_trBool ubi_avlInsert( ubi_btRootPtr RootPtr,
187 ubi_avlNodePtr NewNode,
188 ubi_btItemPtr ItemPtr,
189 ubi_avlNodePtr *OldNode );
190 /* ------------------------------------------------------------------------ **
191 * This function uses a non-recursive algorithm to add a new element to
194 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
195 * the root of the tree to which NewNode is to be added.
196 * NewNode - a pointer to an ubi_avlNode structure that is NOT
198 * ItemPtr - A pointer to the sort key that is stored within
199 * *NewNode. ItemPtr MUST point to information stored
200 * in *NewNode or an EXACT DUPLICATE. The key data
201 * indicated by ItemPtr is used to place the new node
203 * OldNode - a pointer to an ubi_btNodePtr. When searching
204 * the tree, a duplicate node may be found. If
205 * duplicates are allowed, then the new node will
206 * be simply placed into the tree. If duplicates
207 * are not allowed, however, then one of two things
209 * 1) if overwritting *is not* allowed, this
210 * function will return FALSE (indicating that
211 * the new node could not be inserted), and
212 * *OldNode will point to the duplicate that is
214 * 2) if overwritting *is* allowed, then this
215 * function will swap **OldNode for *NewNode.
216 * In this case, *OldNode will point to the node
217 * that was removed (thus allowing you to free
219 * ** If you are using overwrite mode, ALWAYS **
220 * ** check the return value of this parameter! **
221 * Note: You may pass NULL in this parameter, the
222 * function knows how to cope. If you do this,
223 * however, there will be no way to return a
224 * pointer to an old (ie. replaced) node (which is
225 * a problem if you are using overwrite mode).
227 * Output: a boolean value indicating success or failure. The function
228 * will return FALSE if the node could not be added to the tree.
229 * Such failure will only occur if duplicates are not allowed,
230 * nodes cannot be overwritten, AND a duplicate key was found
232 * ------------------------------------------------------------------------ **
235 ubi_avlNodePtr ubi_avlRemove( ubi_btRootPtr RootPtr,
236 ubi_avlNodePtr DeadNode );
237 /* ------------------------------------------------------------------------ **
238 * This function removes the indicated node from the tree, after which the
239 * tree is rebalanced.
241 * Input: RootPtr - A pointer to the header of the tree that contains
242 * the node to be removed.
243 * DeadNode - A pointer to the node that will be removed.
245 * Output: This function returns a pointer to the node that was removed
246 * from the tree (ie. the same as DeadNode).
248 * Note: The node MUST be in the tree indicated by RootPtr. If not,
249 * strange and evil things will happen to your trees.
250 * ------------------------------------------------------------------------ **
253 int ubi_avlModuleID( int size, char *list[] );
254 /* ------------------------------------------------------------------------ **
255 * Returns a set of strings that identify the module.
257 * Input: size - The number of elements in the array <list>.
258 * list - An array of pointers of type (char *). This array
259 * should, initially, be empty. This function will fill
260 * in the array with pointers to strings.
261 * Output: The number of elements of <list> that were used. If this value
262 * is less than <size>, the values of the remaining elements are
265 * Notes: Please keep in mind that the pointers returned indicate strings
266 * stored in static memory. Don't free() them, don't write over
267 * them, etc. Just read them.
268 * ------------------------------------------------------------------------ **
271 /* -------------------------------------------------------------------------- **
274 * This set of defines allows you to write programs that will use any of the
275 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
276 * Instead of using ubi_avl... or ubi_bt, use ubi_tr... and select the tree
277 * type by including the appropriate module header.
282 #define ubi_trNode ubi_avlNode
283 #define ubi_trNodePtr ubi_avlNodePtr
285 #undef ubi_trInitNode
286 #define ubi_trInitNode( Np ) ubi_avlInitNode( (ubi_avlNodePtr)(Np) )
289 #define ubi_trInsert( Rp, Nn, Ip, On ) \
290 ubi_avlInsert( (ubi_btRootPtr)(Rp), (ubi_avlNodePtr)(Nn), \
291 (ubi_btItemPtr)(Ip), (ubi_avlNodePtr *)(On) )
294 #define ubi_trRemove( Rp, Dn ) \
295 ubi_avlRemove( (ubi_btRootPtr)(Rp), (ubi_avlNodePtr)(Dn) )
298 #define ubi_trLocate( Rp, Ip, Op ) \
299 (ubi_avlNodePtr)ubi_btLocate( (ubi_btRootPtr)(Rp), \
300 (ubi_btItemPtr)(Ip), \
301 (ubi_trCompOps)(Op) )
304 #define ubi_trFind( Rp, Ip ) \
305 (ubi_avlNodePtr)ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
308 #define ubi_trNext( P ) (ubi_avlNodePtr)ubi_btNext( (ubi_btNodePtr)(P) )
311 #define ubi_trPrev( P ) (ubi_avlNodePtr)ubi_btPrev( (ubi_btNodePtr)(P) )
314 #define ubi_trFirst( P ) (ubi_avlNodePtr)ubi_btFirst( (ubi_btNodePtr)(P) )
317 #define ubi_trLast( P ) (ubi_avlNodePtr)ubi_btLast( (ubi_btNodePtr)(P) )
320 #define ubi_trFirstOf( Rp, Ip, P ) \
321 (ubi_avlNodePtr)ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
322 (ubi_btItemPtr)(Ip), \
326 #define ubi_trLastOf( Rp, Ip, P ) \
327 (ubi_avlNodePtr)ubi_btLastOf( (ubi_btRootPtr)(Rp), \
328 (ubi_btItemPtr)(Ip), \
331 #undef ubi_trLeafNode
332 #define ubi_trLeafNode( Nd ) \
333 (ubi_avlNodePtr)ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
335 #undef ubi_trModuleID
336 #define ubi_trModuleID( s, l ) ubi_avlModuleID( s, l )
339 /* =========================== End ubi_AVLtree.h =========================== */
340 #endif /* ubi_AVLtree_H */