1 #ifndef ubi_SplayTree_H
2 #define ubi_SplayTree_H
3 /* ========================================================================== **
6 * Copyright (C) 1993,1995 by Christopher R. Hertel
8 * Email: crh@ubiqx.mn.org
9 * -------------------------------------------------------------------------- **
11 * This module implements "splay" trees. Splay trees are binary trees
12 * that are rearranged (splayed) whenever a node is accessed. The
13 * splaying process *tends* to make the tree bushier (improves balance),
14 * and the nodes that are accessed most frequently *tend* to be closer to
17 * References: "Self-Adjusting Binary Search Trees", by Daniel Sleator and
18 * Robert Tarjan. Journal of the Association for Computing
19 * Machinery Vol 32, No. 3, July 1985 pp. 652-686
21 * -------------------------------------------------------------------------- **
23 * This library is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU Library General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This library is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * Library General Public License for more details.
33 * You should have received a copy of the GNU Library General Public
34 * License along with this library; if not, write to the Free
35 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 * -------------------------------------------------------------------------- **
39 * $Log: ubi_SplayTree.h,v $
40 * Revision 1.1 1997/10/09 04:09:55 crh
41 * This is my library of lists and trees. My hope is to replace all of the
42 * hard coded linked lists that are currently used in Samba with calls to
43 * these modules. This should make the code simpler, smaller, and (I hope)
44 * faster. The tree code, in particular, should speed up processing where
45 * large lists are involved.
49 * Revision 2.5 1997/07/26 04:15:46 crh
50 * + Cleaned up a few minor syntax annoyances that gcc discovered for me.
51 * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
53 * Revision 2.4 1997/06/03 05:22:56 crh
54 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
57 * Revision 2.3 1995/10/03 22:19:37 CRH
59 * Also, added the function ubi_sptSplay().
61 * Revision 2.1 95/03/09 23:55:04 CRH
62 * Added the ModuleID static string and function. These modules are now
65 * Revision 2.0 95/02/27 22:34:55 CRH
66 * This module was updated to match the interface changes made to the
67 * ubi_BinTree module. In particular, the interface to the Locate() function
68 * has changed. See ubi_BinTree for more information on changes and new
71 * The revision number was also upped to match ubi_BinTree.
74 * Revision 1.0 93/10/15 22:59:36 CRH
75 * With this revision, I have added a set of #define's that provide a single,
76 * standard API to all existing tree modules. Until now, each of the three
77 * existing modules had a different function and typedef prefix, as follows:
82 * ubi_SplayTree ubi_spt
84 * To further complicate matters, only those portions of the base module
85 * (ubi_BinTree) that were superceeded in the new module had the new names.
86 * For example, if you were using ubi_AVLtree, the AVL node structure was
87 * named "ubi_avlNode", but the root structure was still "ubi_btRoot". Using
88 * SplayTree, the locate function was called "ubi_sptLocate", but the next
89 * and previous functions remained "ubi_btNext" and "ubi_btPrev".
91 * This was not too terrible if you were familiar with the modules and knew
92 * exactly which tree model you wanted to use. If you wanted to be able to
93 * change modules (for speed comparisons, etc), things could get messy very
96 * So, I have added a set of defined names that get redefined in any of the
97 * descendant modules. To use this standardized interface in your code,
98 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
99 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
100 * datatype names for the module that you are using. Just remember to
101 * include the header for that module in your program file. Because these
102 * names are handled by the preprocessor, there is no added run-time
105 * Note that the original names do still exist, and can be used if you wish
106 * to write code directly to a specific module. This should probably only be
107 * done if you are planning to implement a new descendant type, such as
108 * red/black trees. CRH
110 * Revision 0.0 93/04/21 23:07:13 CRH
111 * Initial version, written by Christopher R. Hertel.
112 * This module implements Splay Trees using the ubi_BinTree module as a basis.
114 * ========================================================================== **
117 #include "ubi_BinTree.h" /* Base binary tree functions, types, etc. */
119 /* ========================================================================== **
120 * Function prototypes...
123 ubi_trBool ubi_sptInsert( ubi_btRootPtr RootPtr,
124 ubi_btNodePtr NewNode,
125 ubi_btItemPtr ItemPtr,
126 ubi_btNodePtr *OldNode );
127 /* ------------------------------------------------------------------------ **
128 * This function uses a non-recursive algorithm to add a new element to the
131 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
132 * the root of the tree to which NewNode is to be added.
133 * NewNode - a pointer to an ubi_btNode structure that is NOT
135 * ItemPtr - A pointer to the sort key that is stored within
136 * *NewNode. ItemPtr MUST point to information stored
137 * in *NewNode or an EXACT DUPLICATE. The key data
138 * indicated by ItemPtr is used to place the new node
140 * OldNode - a pointer to an ubi_btNodePtr. When searching
141 * the tree, a duplicate node may be found. If
142 * duplicates are allowed, then the new node will
143 * be simply placed into the tree. If duplicates
144 * are not allowed, however, then one of two things
146 * 1) if overwritting *is not* allowed, this
147 * function will return FALSE (indicating that
148 * the new node could not be inserted), and
149 * *OldNode will point to the duplicate that is
151 * 2) if overwritting *is* allowed, then this
152 * function will swap **OldNode for *NewNode.
153 * In this case, *OldNode will point to the node
154 * that was removed (thus allowing you to free
156 * ** If you are using overwrite mode, ALWAYS **
157 * ** check the return value of this parameter! **
158 * Note: You may pass NULL in this parameter, the
159 * function knows how to cope. If you do this,
160 * however, there will be no way to return a
161 * pointer to an old (ie. replaced) node (which is
162 * a problem if you are using overwrite mode).
164 * Output: a boolean value indicating success or failure. The function
165 * will return FALSE if the node could not be added to the tree.
166 * Such failure will only occur if duplicates are not allowed,
167 * nodes cannot be overwritten, AND a duplicate key was found
169 * ------------------------------------------------------------------------ **
172 ubi_btNodePtr ubi_sptRemove( ubi_btRootPtr RootPtr, ubi_btNodePtr DeadNode );
173 /* ------------------------------------------------------------------------ **
174 * This function removes the indicated node from the tree.
176 * Input: RootPtr - A pointer to the header of the tree that contains
177 * the node to be removed.
178 * DeadNode - A pointer to the node that will be removed.
180 * Output: This function returns a pointer to the node that was removed
181 * from the tree (ie. the same as DeadNode).
183 * Note: The node MUST be in the tree indicated by RootPtr. If not,
184 * strange and evil things will happen to your trees.
185 * ------------------------------------------------------------------------ **
188 ubi_btNodePtr ubi_sptLocate( ubi_btRootPtr RootPtr,
189 ubi_btItemPtr FindMe,
190 ubi_trCompOps CompOp );
191 /* ------------------------------------------------------------------------ **
192 * The purpose of ubi_btLocate() is to find a node or set of nodes given
193 * a target value and a "comparison operator". The Locate() function is
194 * more flexible and (in the case of trees that may contain dupicate keys)
195 * more precise than the ubi_btFind() function. The latter is faster,
196 * but it only searches for exact matches and, if the tree contains
197 * duplicates, Find() may return a pointer to any one of the duplicate-
201 * RootPtr - A pointer to the header of the tree to be searched.
202 * FindMe - An ubi_btItemPtr that indicates the key for which to
204 * CompOp - One of the following:
205 * CompOp Return a pointer to the node with
206 * ------ ---------------------------------
207 * ubi_trLT - the last key value that is less
209 * ubi_trLE - the first key matching FindMe, or
210 * the last key that is less than
212 * ubi_trEQ - the first key matching FindMe.
213 * ubi_trGE - the first key matching FindMe, or the
214 * first key greater than FindMe.
215 * ubi_trGT - the first key greater than FindMe.
217 * A pointer to the node matching the criteria listed above under
218 * CompOp, or NULL if no node matched the criteria.
221 * In the case of trees with duplicate keys, Locate() will behave as
225 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6
229 * That is, when returning a pointer to a node with a key that is LESS
230 * THAN the target key (FindMe), Locate() will return a pointer to the
231 * LAST matching node.
232 * When returning a pointer to a node with a key that is GREATER
233 * THAN the target key (FindMe), Locate() will return a pointer to the
234 * FIRST matching node.
236 * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
237 * ------------------------------------------------------------------------ **
240 ubi_btNodePtr ubi_sptFind( ubi_btRootPtr RootPtr,
241 ubi_btItemPtr FindMe );
242 /* ------------------------------------------------------------------------ **
243 * This function performs a non-recursive search of a tree for any node
244 * matching a specific key.
247 * RootPtr - a pointer to the header of the tree to be searched.
248 * FindMe - a pointer to the key value for which to search.
251 * A pointer to a node with a key that matches the key indicated by
252 * FindMe, or NULL if no such node was found.
254 * Note: In a tree that allows duplicates, the pointer returned *might
255 * not* point to the (sequentially) first occurance of the
256 * desired key. In such a tree, it may be more useful to use
258 * ------------------------------------------------------------------------ **
261 void ubi_sptSplay( ubi_btRootPtr RootPtr,
262 ubi_btNodePtr SplayMe );
263 /* ------------------------------------------------------------------------ **
264 * This function allows you to splay the tree at a given node, thus moving
265 * the node to the top of the tree.
268 * RootPtr - a pointer to the header of the tree to be splayed.
269 * SplayMe - a pointer to a node within the tree. This will become
273 * Notes: This is an uncharacteristic function for this group of modules
274 * in that it provides access to the internal balancing routines,
275 * which would normally be hidden.
276 * Splaying the tree will not damage it (assuming that I've done
277 * *my* job), but there is overhead involved. I don't recommend
278 * that you use this function unless you understand the underlying
279 * Splay Tree principles involved.
280 * ------------------------------------------------------------------------ **
283 int ubi_sptModuleID( int size, char *list[] );
284 /* ------------------------------------------------------------------------ **
285 * Returns a set of strings that identify the module.
287 * Input: size - The number of elements in the array <list>.
288 * list - An array of pointers of type (char *). This array
289 * should, initially, be empty. This function will fill
290 * in the array with pointers to strings.
291 * Output: The number of elements of <list> that were used. If this value
292 * is less than <size>, the values of the remaining elements are
295 * Notes: Please keep in mind that the pointers returned indicate strings
296 * stored in static memory. Don't free() them, don't write over
297 * them, etc. Just read them.
298 * ------------------------------------------------------------------------ **
301 /* -------------------------------------------------------------------------- **
304 * This set of defines allows you to write programs that will use any of the
305 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
306 * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
307 * including the appropriate module header.
314 #undef ubi_trModuleID
316 #define ubi_trInsert( Rp, Nn, Ip, On ) \
317 ubi_sptInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
318 (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
320 #define ubi_trRemove( Rp, Dn ) \
321 ubi_sptRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
323 #define ubi_trLocate( Rp, Ip, Op ) \
324 ubi_sptLocate( (ubi_btRootPtr)(Rp), \
325 (ubi_btItemPtr)(Ip), \
326 (ubi_trCompOps)(Op) )
328 #define ubi_trFind( Rp, Ip ) \
329 ubi_sptFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
331 #define ubi_trModuleID( s, l ) ubi_sptModuleID( s, l )
333 /* ================================ The End ================================= */
334 #endif /* ubi_SplayTree_H */