3 /* ========================================================================== **
6 * Copyright (C) 1991-1997 by Christopher R. Hertel
8 * Email: crh@ubiqx.mn.org
9 * -------------------------------------------------------------------------- **
11 * ubi_BinTree manages a simple binary tree. Nothing fancy here. No height
12 * balancing, no restructuring. Still, a good tool for creating short, low-
13 * overhead sorted lists of things that need to be found in a hurry.
15 * In addition, this module provides a good basis for creating other types
16 * of binary tree handling modules.
18 * -------------------------------------------------------------------------- **
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Library General Public
22 * License as published by the Free Software Foundation; either
23 * version 2 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Library General Public License for more details.
30 * You should have received a copy of the GNU Library General Public
31 * License along with this library; if not, write to the Free
32 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 * -------------------------------------------------------------------------- **
36 * $Log: ubi_BinTree.h,v $
37 * Revision 1.1 1997/10/09 04:09:52 crh
38 * This is my library of lists and trees. My hope is to replace all of the
39 * hard coded linked lists that are currently used in Samba with calls to
40 * these modules. This should make the code simpler, smaller, and (I hope)
41 * faster. The tree code, in particular, should speed up processing where
42 * large lists are involved.
46 * Revision 2.4 1997/07/26 04:11:14 crh
47 * + Just to be annoying I changed ubi_TRUE and ubi_FALSE to ubi_trTRUE
49 * + There is now a type ubi_trBool to go with ubi_trTRUE and ubi_trFALSE.
50 * + There used to be something called "ubi_TypeDefs.h". I got rid of it.
51 * + Added function ubi_btLeafNode().
53 * Revision 2.3 1997/06/03 05:15:27 crh
54 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid conflicts.
55 * Also changed the interface to function InitTree(). See the comments
56 * for this function for more information.
58 * Revision 2.2 1995/10/03 22:00:40 CRH
61 * Revision 2.1 95/03/09 23:43:46 CRH
62 * Added the ModuleID static string and function. These modules are now
65 * Revision 2.0 95/02/27 22:00:33 CRH
66 * Revision 2.0 of this program includes the following changes:
68 * 1) A fix to a major typo in the RepaceNode() function.
69 * 2) The addition of the static function Border().
70 * 3) The addition of the public functions FirstOf() and LastOf(), which
71 * use Border(). These functions are used with trees that allow
73 * 4) A complete rewrite of the Locate() function. Locate() now accepts
74 * a "comparison" operator.
75 * 5) Overall enhancements to both code and comments.
77 * I decided to give this a new major rev number because the interface has
78 * changed. In particular, there are two new functions, and changes to the
81 * Revision 1.0 93/10/15 22:55:04 CRH
82 * With this revision, I have added a set of #define's that provide a single,
83 * standard API to all existing tree modules. Until now, each of the three
84 * existing modules had a different function and typedef prefix, as follows:
89 * ubi_SplayTree ubi_spt
91 * To further complicate matters, only those portions of the base module
92 * (ubi_BinTree) that were superceeded in the new module had the new names.
93 * For example, if you were using ubi_AVLtree, the AVL node structure was
94 * named "ubi_avlNode", but the root structure was still "ubi_btRoot". Using
95 * SplayTree, the locate function was called "ubi_sptLocate", but the next
96 * and previous functions remained "ubi_btNext" and "ubi_btPrev".
98 * This was not too terrible if you were familiar with the modules and knew
99 * exactly which tree model you wanted to use. If you wanted to be able to
100 * change modules (for speed comparisons, etc), things could get messy very
103 * So, I have added a set of defined names that get redefined in any of the
104 * descendant modules. To use this standardized interface in your code,
105 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
106 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
107 * datatype names for the module that you are using. Just remember to
108 * include the header for that module in your program file. Because these
109 * names are handled by the preprocessor, there is no added run-time
112 * Note that the original names do still exist, and can be used if you wish
113 * to write code directly to a specific module. This should probably only be
114 * done if you are planning to implement a new descendant type, such as
115 * red/black trees. CRH
117 * V0.0 - June, 1991 - Written by Christopher R. Hertel (CRH).
119 * ========================================================================== **
122 /* -------------------------------------------------------------------------- **
123 * Macros and constants.
126 * ubi_trTRUE - Boolean TRUE.
127 * ubi_trFALSE - Boolean FALSE.
129 * Flags used in the tree header:
130 * ubi_trOVERWRITE - This flag indicates that an existing node may be
131 * overwritten by a new node with a matching key.
132 * ubi_trDUPKEY - This flag indicates that the tree allows duplicate
133 * keys. If the tree does allow duplicates, the
134 * overwrite flag is ignored.
136 * Node link array index constants: (Each node has an array of three
137 * pointers. One to the left, one to the right, and one back to the
139 * LEFT - Left child pointer.
140 * PARENT - Parent pointer.
141 * RIGHT - Right child pointer.
142 * EQUAL - Synonym for PARENT.
144 * ubi_trCompOps: These values are used in the ubi_trLocate() function.
145 * ubi_trLT - request the first instance of the greatest key less than
147 * ubi_trLE - request the first instance of the greatest key that is less
148 * than or equal to the search key.
149 * ubi_trEQ - request the first instance of key that is equal to the
151 * ubi_trGE - request the first instance of a key that is greater than
152 * or equal to the search key.
153 * ubi_trGT - request the first instance of the first key that is greater
154 * than the search key.
155 * -------------------------------------------------------------------------- **
158 #define ubi_trTRUE 0xFF
159 #define ubi_trFALSE 0x00
161 #define ubi_trOVERWRITE 0x01 /* Turn on allow overwrite */
162 #define ubi_trDUPKEY 0x02 /* Turn on allow duplicate keys */
164 /* Pointer array index constants... */
178 /* -------------------------------------------------------------------------- **
179 * These three macros allow simple manipulation of pointer index values (LEFT,
180 * RIGHT, and PARENT).
182 * Normalize() - converts {LEFT, PARENT, RIGHT} into {-1, 0 ,1}. C
183 * uses {negative, zero, positive} values to indicate
184 * {less than, equal to, greater than}.
185 * AbNormal() - converts {negative, zero, positive} to {LEFT, PARENT,
186 * RIGHT} (opposite of Normalize()). Note: C comparison
187 * functions, such as strcmp(), return {negative, zero,
188 * positive} values, which are not necessarily {-1, 0,
189 * 1}. This macro uses the the ubi_btSgn() function to
191 * RevWay() - converts LEFT to RIGHT and RIGHT to LEFT. PARENT (EQUAL)
193 * -------------------------------------------------------------------------- **
195 #define Normalize(W) ((char)((W)-EQUAL))
196 #define AbNormal(W) ((char)( EQUAL+((char)ubi_btSgn( (W) )) ))
197 #define RevWay(W) ((char)((W)==LEFT?RIGHT:((W)==RIGHT?LEFT:EQUAL)))
199 /* -------------------------------------------------------------------------- **
200 * These macros allow us to quickly read the values of the OVERWRITE and
201 * DUPlicate KEY bits of the tree root flags field.
202 * -------------------------------------------------------------------------- **
204 #define Dups_OK(A) ((ubi_trDUPKEY & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
205 #define Ovwt_OK(A) ((ubi_trOVERWRITE & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
207 /* -------------------------------------------------------------------------- **
210 * ubi_trBool - Your typcial true or false...
212 * Item Pointer: The ubi_btItemPtr is a generic pointer. It is used to
213 * indicate a key that is being searched for within the tree.
214 * Searching occurs whenever the ubi_trFind(), ubi_trLocate(),
215 * or ubi_trInsert() functions are called.
216 * -------------------------------------------------------------------------- **
219 typedef unsigned char ubi_trBool;
221 typedef void *ubi_btItemPtr; /* A pointer to data within a node. */
223 /* ------------------------------------------------------------------------- **
224 * Binary Tree Node Structure: This structure defines the basic elements of
225 * the tree nodes. In general you *SHOULD NOT PLAY WITH THESE FIELDS*!
226 * But, of course, I have to put the structure into this header so that
227 * you can use it as a building block.
229 * The fields are as follows:
230 * Link - an array of pointers. These pointers are manipulated by
231 * the BT routines. The pointers indicate the left and right
232 * child nodes and the parent node. By keeping track of the
233 * parent pointer, we avoid the need for recursive routines or
234 * hand-tooled stacks to keep track of our path back to the
235 * root. The use of these pointers is subject to change without
237 * gender - a one-byte field indicating whether the node is the RIGHT or
238 * LEFT child of its parent. If the node is the root of the
239 * tree, gender will be PARENT.
240 * ------------------------------------------------------------------------- **
242 typedef struct ubi_btNodeStruct {
243 struct ubi_btNodeStruct *Link[ 3 ];
247 typedef ubi_btNode *ubi_btNodePtr; /* Pointer to an ubi_btNode structure. */
249 /* ------------------------------------------------------------------------- **
250 * The next three typedefs define standard function types used by the binary
251 * tree management routines. In particular:
253 * ubi_btCompFunc is a pointer to a comparison function. Comparison
254 * functions are passed an ubi_btItemPtr and an
255 * ubi_btNodePtr. They return a value that is (<0), 0,
256 * or (>0) to indicate that the Item is (respectively)
257 * "less than", "equal to", or "greater than" the Item
258 * contained within the node. (See ubi_btInitTree()).
259 * ubi_btActionRtn is a pointer to a function that may be called for each
260 * node visited when performing a tree traversal (see
261 * ubi_btTraverse()). The function will be passed two
262 * parameters: the first is a pointer to a node in the
263 * tree, the second is a generic pointer that may point to
264 * anything that you like.
265 * ubi_btKillNodeRtn is a pointer to a function that will deallocate the
266 * memory used by a node (see ubi_btKillTree()). Since
267 * memory management is left up to you, deallocation may
268 * mean anything that you want it to mean. Just remember
269 * that the tree *will* be destroyed and that none of the
270 * node pointers will be valid any more.
271 * ------------------------------------------------------------------------- **
274 typedef int (*ubi_btCompFunc)( ubi_btItemPtr, ubi_btNodePtr );
276 typedef void (*ubi_btActionRtn)( ubi_btNodePtr, void * );
278 typedef void (*ubi_btKillNodeRtn)( ubi_btNodePtr );
280 /* -------------------------------------------------------------------------- **
281 * Tree Root Structure: This structure gives us a convenient handle for
282 * accessing whole AVL trees. The fields are:
283 * root - A pointer to the root node of the AVL tree.
284 * count - A count of the number of nodes stored in the tree.
285 * cmp - A pointer to the comparison routine to be used when building or
286 * searching the tree.
287 * flags - A set of bit flags. Two flags are currently defined:
289 * ubi_trOVERWRITE - If set, this flag indicates that a new node should
290 * (bit 0x01) overwrite an old node if the two have identical
291 * keys (ie., the keys are equal).
292 * ubi_trDUPKEY - If set, this flag indicates that the tree is
293 * (bit 0x02) allowed to contain nodes with duplicate keys.
295 * NOTE: ubi_trInsert() tests ubi_trDUPKEY before ubi_trOVERWRITE.
297 * All of these values are set when you initialize the root structure by
298 * calling ubi_trInitTree().
299 * -------------------------------------------------------------------------- **
303 ubi_btNodePtr root; /* A pointer to the root node of the tree */
304 unsigned long count; /* A count of the number of nodes in the tree */
305 ubi_btCompFunc cmp; /* A pointer to the tree's comparison function */
306 unsigned char flags; /* Overwrite Y|N, Duplicate keys Y|N... */
309 typedef ubi_btRoot *ubi_btRootPtr; /* Pointer to an ubi_btRoot structure. */
312 /* -------------------------------------------------------------------------- **
313 * Function Prototypes.
316 long ubi_btSgn( long x );
317 /* ------------------------------------------------------------------------ **
318 * Return the sign of x; {negative,zero,positive} ==> {-1, 0, 1}.
320 * Input: x - a signed long integer value.
322 * Output: the "sign" of x, represented as follows:
324 * 0 == zero (no sign)
327 * Note: This utility is provided in order to facilitate the conversion
328 * of C comparison function return values into BinTree direction
329 * values: {LEFT, PARENT, EQUAL}. It is INCORPORATED into the
330 * AbNormal() conversion macro!
332 * ------------------------------------------------------------------------ **
335 ubi_btNodePtr ubi_btInitNode( ubi_btNodePtr NodePtr );
336 /* ------------------------------------------------------------------------ **
337 * Initialize a tree node.
339 * Input: a pointer to a ubi_btNode structure to be initialized.
340 * Output: a pointer to the initialized ubi_btNode structure (ie. the
341 * same as the input pointer).
342 * ------------------------------------------------------------------------ **
345 ubi_btRootPtr ubi_btInitTree( ubi_btRootPtr RootPtr,
346 ubi_btCompFunc CompFunc,
347 unsigned char Flags );
348 /* ------------------------------------------------------------------------ **
349 * Initialize the fields of a Tree Root header structure.
351 * Input: RootPtr - a pointer to an ubi_btRoot structure to be
353 * CompFunc - a pointer to a comparison function that will be used
354 * whenever nodes in the tree must be compared against
356 * Flags - One bytes worth of flags. Flags include
357 * ubi_trOVERWRITE and ubi_trDUPKEY. See the header
358 * file for more info.
360 * Output: a pointer to the initialized ubi_btRoot structure (ie. the
361 * same value as RootPtr).
363 * Note: The interface to this function has changed from that of
364 * previous versions. The <Flags> parameter replaces two
365 * boolean parameters that had the same basic effect.
366 * ------------------------------------------------------------------------ **
369 ubi_trBool ubi_btInsert( ubi_btRootPtr RootPtr,
370 ubi_btNodePtr NewNode,
371 ubi_btItemPtr ItemPtr,
372 ubi_btNodePtr *OldNode );
373 /* ------------------------------------------------------------------------ **
374 * This function uses a non-recursive algorithm to add a new element to the
377 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
378 * the root of the tree to which NewNode is to be added.
379 * NewNode - a pointer to an ubi_btNode structure that is NOT
381 * ItemPtr - A pointer to the sort key that is stored within
382 * *NewNode. ItemPtr MUST point to information stored
383 * in *NewNode or an EXACT DUPLICATE. The key data
384 * indicated by ItemPtr is used to place the new node
386 * OldNode - a pointer to an ubi_btNodePtr. When searching
387 * the tree, a duplicate node may be found. If
388 * duplicates are allowed, then the new node will
389 * be simply placed into the tree. If duplicates
390 * are not allowed, however, then one of two things
392 * 1) if overwritting *is not* allowed, this
393 * function will return FALSE (indicating that
394 * the new node could not be inserted), and
395 * *OldNode will point to the duplicate that is
397 * 2) if overwritting *is* allowed, then this
398 * function will swap **OldNode for *NewNode.
399 * In this case, *OldNode will point to the node
400 * that was removed (thus allowing you to free
402 * ** If you are using overwrite mode, ALWAYS **
403 * ** check the return value of this parameter! **
404 * Note: You may pass NULL in this parameter, the
405 * function knows how to cope. If you do this,
406 * however, there will be no way to return a
407 * pointer to an old (ie. replaced) node (which is
408 * a problem if you are using overwrite mode).
410 * Output: a boolean value indicating success or failure. The function
411 * will return FALSE if the node could not be added to the tree.
412 * Such failure will only occur if duplicates are not allowed,
413 * nodes cannot be overwritten, AND a duplicate key was found
415 * ------------------------------------------------------------------------ **
418 ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr,
419 ubi_btNodePtr DeadNode );
420 /* ------------------------------------------------------------------------ **
421 * This function removes the indicated node from the tree.
423 * Input: RootPtr - A pointer to the header of the tree that contains
424 * the node to be removed.
425 * DeadNode - A pointer to the node that will be removed.
427 * Output: This function returns a pointer to the node that was removed
428 * from the tree (ie. the same as DeadNode).
430 * Note: The node MUST be in the tree indicated by RootPtr. If not,
431 * strange and evil things will happen to your trees.
432 * ------------------------------------------------------------------------ **
435 ubi_btNodePtr ubi_btLocate( ubi_btRootPtr RootPtr,
436 ubi_btItemPtr FindMe,
437 ubi_trCompOps CompOp );
438 /* ------------------------------------------------------------------------ **
439 * The purpose of ubi_btLocate() is to find a node or set of nodes given
440 * a target value and a "comparison operator". The Locate() function is
441 * more flexible and (in the case of trees that may contain dupicate keys)
442 * more precise than the ubi_btFind() function. The latter is faster,
443 * but it only searches for exact matches and, if the tree contains
444 * duplicates, Find() may return a pointer to any one of the duplicate-
448 * RootPtr - A pointer to the header of the tree to be searched.
449 * FindMe - An ubi_btItemPtr that indicates the key for which to
451 * CompOp - One of the following:
452 * CompOp Return a pointer to the node with
453 * ------ ---------------------------------
454 * ubi_trLT - the last key value that is less
456 * ubi_trLE - the first key matching FindMe, or
457 * the last key that is less than
459 * ubi_trEQ - the first key matching FindMe.
460 * ubi_trGE - the first key matching FindMe, or the
461 * first key greater than FindMe.
462 * ubi_trGT - the first key greater than FindMe.
464 * A pointer to the node matching the criteria listed above under
465 * CompOp, or NULL if no node matched the criteria.
468 * In the case of trees with duplicate keys, Locate() will behave as
472 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6
476 * That is, when returning a pointer to a node with a key that is LESS
477 * THAN the target key (FindMe), Locate() will return a pointer to the
478 * LAST matching node.
479 * When returning a pointer to a node with a key that is GREATER
480 * THAN the target key (FindMe), Locate() will return a pointer to the
481 * FIRST matching node.
483 * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
484 * ------------------------------------------------------------------------ **
487 ubi_btNodePtr ubi_btFind( ubi_btRootPtr RootPtr,
488 ubi_btItemPtr FindMe );
489 /* ------------------------------------------------------------------------ **
490 * This function performs a non-recursive search of a tree for any node
491 * matching a specific key.
494 * RootPtr - a pointer to the header of the tree to be searched.
495 * FindMe - a pointer to the key value for which to search.
498 * A pointer to a node with a key that matches the key indicated by
499 * FindMe, or NULL if no such node was found.
501 * Note: In a tree that allows duplicates, the pointer returned *might
502 * not* point to the (sequentially) first occurance of the
503 * desired key. In such a tree, it may be more useful to use
505 * ------------------------------------------------------------------------ **
508 ubi_btNodePtr ubi_btNext( ubi_btNodePtr P );
509 /* ------------------------------------------------------------------------ **
510 * Given the node indicated by P, find the (sorted order) Next node in the
512 * Input: P - a pointer to a node that exists in a binary tree.
513 * Output: A pointer to the "next" node in the tree, or NULL if P pointed
514 * to the "last" node in the tree or was NULL.
515 * ------------------------------------------------------------------------ **
518 ubi_btNodePtr ubi_btPrev( ubi_btNodePtr P );
519 /* ------------------------------------------------------------------------ **
520 * Given the node indicated by P, find the (sorted order) Previous node in
522 * Input: P - a pointer to a node that exists in a binary tree.
523 * Output: A pointer to the "previous" node in the tree, or NULL if P
524 * pointed to the "first" node in the tree or was NULL.
525 * ------------------------------------------------------------------------ **
528 ubi_btNodePtr ubi_btFirst( ubi_btNodePtr P );
529 /* ------------------------------------------------------------------------ **
530 * Given the node indicated by P, find the (sorted order) First node in the
531 * subtree of which *P is the root.
532 * Input: P - a pointer to a node that exists in a binary tree.
533 * Output: A pointer to the "first" node in a subtree that has *P as its
534 * root. This function will return NULL only if P is NULL.
535 * Note: In general, you will be passing in the value of the root field
536 * of an ubi_btRoot structure.
537 * ------------------------------------------------------------------------ **
540 ubi_btNodePtr ubi_btLast( ubi_btNodePtr P );
541 /* ------------------------------------------------------------------------ **
542 * Given the node indicated by P, find the (sorted order) Last node in the
543 * subtree of which *P is the root.
544 * Input: P - a pointer to a node that exists in a binary tree.
545 * Output: A pointer to the "last" node in a subtree that has *P as its
546 * root. This function will return NULL only if P is NULL.
547 * Note: In general, you will be passing in the value of the root field
548 * of an ubi_btRoot structure.
549 * ------------------------------------------------------------------------ **
552 ubi_btNodePtr ubi_btFirstOf( ubi_btRootPtr RootPtr,
553 ubi_btItemPtr MatchMe,
555 /* ------------------------------------------------------------------------ **
556 * Given a tree that a allows duplicate keys, and a pointer to a node in
557 * the tree, this function will return a pointer to the first (traversal
558 * order) node with the same key value.
560 * Input: RootPtr - A pointer to the root of the tree.
561 * MatchMe - A pointer to the key value. This should probably
562 * point to the key within node *p.
563 * p - A pointer to a node in the tree.
564 * Output: A pointer to the first node in the set of nodes with keys
566 * Notes: Node *p MUST be in the set of nodes with keys matching
567 * <FindMe>. If not, this function will return NULL.
568 * ------------------------------------------------------------------------ **
571 ubi_btNodePtr ubi_btLastOf( ubi_btRootPtr RootPtr,
572 ubi_btItemPtr MatchMe,
574 /* ------------------------------------------------------------------------ **
575 * Given a tree that a allows duplicate keys, and a pointer to a node in
576 * the tree, this function will return a pointer to the last (traversal
577 * order) node with the same key value.
579 * Input: RootPtr - A pointer to the root of the tree.
580 * MatchMe - A pointer to the key value. This should probably
581 * point to the key within node *p.
582 * p - A pointer to a node in the tree.
583 * Output: A pointer to the last node in the set of nodes with keys
585 * Notes: Node *p MUST be in the set of nodes with keys matching
586 * <FindMe>. If not, this function will return NULL.
587 * ------------------------------------------------------------------------ **
590 ubi_trBool ubi_btTraverse( ubi_btRootPtr RootPtr,
591 ubi_btActionRtn EachNode,
593 /* ------------------------------------------------------------------------ **
594 * Traverse a tree in sorted order (non-recursively). At each node, call
595 * (*EachNode)(), passing a pointer to the current node, and UserData as the
597 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
598 * the tree to be traversed.
599 * EachNode - a pointer to a function to be called at each node
600 * as the node is visited.
601 * UserData - a generic pointer that may point to anything that
603 * Output: A boolean value. FALSE if the tree is empty, otherwise TRUE.
604 * ------------------------------------------------------------------------ **
607 ubi_trBool ubi_btKillTree( ubi_btRootPtr RootPtr,
608 ubi_btKillNodeRtn FreeNode );
609 /* ------------------------------------------------------------------------ **
610 * Delete an entire tree (non-recursively) and reinitialize the ubi_btRoot
611 * structure. Note that this function will return FALSE if either parameter
614 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
615 * the root of the tree to delete.
616 * FreeNode - a function that will be called for each node in the
617 * tree to deallocate the memory used by the node.
619 * Output: A boolean value. FALSE if either input parameter was NULL, else
622 * ------------------------------------------------------------------------ **
625 ubi_btNodePtr ubi_btLeafNode( ubi_btNodePtr leader );
626 /* ------------------------------------------------------------------------ **
627 * Returns a pointer to a leaf node.
629 * Input: leader - Pointer to a node at which to start the descent.
631 * Output: A pointer to a leaf node selected in a somewhat arbitrary
634 * Notes: I wrote this function because I was using splay trees as a
635 * database cache. The cache had a maximum size on it, and I
636 * needed a way of choosing a node to sacrifice if the cache
637 * became full. In a splay tree, less recently accessed nodes
638 * tend toward the bottom of the tree, meaning that leaf nodes
639 * are good candidates for removal. (I really can't think of
640 * any other reason to use this function.)
641 * + In a simple binary tree or an AVL tree, the most recently
642 * added nodes tend to be nearer the bottom, making this a *bad*
643 * way to choose which node to remove from the cache.
644 * + Randomizing the traversal order is probably a good idea. You
645 * can improve the randomization of leaf node selection by passing
646 * in pointers to nodes other than the root node each time. A
647 * pointer to any node in the tree will do. Of course, if you
648 * pass a pointer to a leaf node you'll get the same thing back.
650 * ------------------------------------------------------------------------ **
654 int ubi_btModuleID( int size, char *list[] );
655 /* ------------------------------------------------------------------------ **
656 * Returns a set of strings that identify the module.
658 * Input: size - The number of elements in the array <list>.
659 * list - An array of pointers of type (char *). This array
660 * should, initially, be empty. This function will fill
661 * in the array with pointers to strings.
662 * Output: The number of elements of <list> that were used. If this value
663 * is less than <size>, the values of the remaining elements are
666 * Notes: Please keep in mind that the pointers returned indicate strings
667 * stored in static memory. Don't free() them, don't write over
668 * them, etc. Just read them.
669 * ------------------------------------------------------------------------ **
672 /* -------------------------------------------------------------------------- **
675 * This set of defines allows you to write programs that will use any of the
676 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
677 * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
678 * including the appropriate module header.
681 #define ubi_trItemPtr ubi_btItemPtr
683 #define ubi_trNode ubi_btNode
684 #define ubi_trNodePtr ubi_btNodePtr
686 #define ubi_trRoot ubi_btRoot
687 #define ubi_trRootPtr ubi_btRootPtr
689 #define ubi_trCompFunc ubi_btCompFunc
690 #define ubi_trActionRtn ubi_btActionRtn
691 #define ubi_trKillNodeRtn ubi_btKillNodeRtn
693 #define ubi_trSgn( x ) ubi_btSgn( x )
695 #define ubi_trInitNode( Np ) ubi_btInitNode( (ubi_btNodePtr)(Np) )
697 #define ubi_trInitTree( Rp, Cf, Fl ) \
698 ubi_btInitTree( (ubi_btRootPtr)(Rp), (ubi_btCompFunc)(Cf), (Fl) )
700 #define ubi_trInsert( Rp, Nn, Ip, On ) \
701 ubi_btInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
702 (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
704 #define ubi_trRemove( Rp, Dn ) \
705 ubi_btRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
707 #define ubi_trLocate( Rp, Ip, Op ) \
708 ubi_btLocate( (ubi_btRootPtr)(Rp), \
709 (ubi_btItemPtr)(Ip), \
710 (ubi_trCompOps)(Op) )
712 #define ubi_trFind( Rp, Ip ) \
713 ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
715 #define ubi_trNext( P ) ubi_btNext( (ubi_btNodePtr)(P) )
717 #define ubi_trPrev( P ) ubi_btPrev( (ubi_btNodePtr)(P) )
719 #define ubi_trFirst( P ) ubi_btFirst( (ubi_btNodePtr)(P) )
721 #define ubi_trLast( P ) ubi_btLast( (ubi_btNodePtr)(P) )
723 #define ubi_trFirstOf( Rp, Ip, P ) \
724 ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
725 (ubi_btItemPtr)(Ip), \
728 #define ubi_trLastOf( Rp, Ip, P ) \
729 ubi_btLastOf( (ubi_btRootPtr)(Rp), \
730 (ubi_btItemPtr)(Ip), \
733 #define ubi_trTraverse( Rp, En, Ud ) \
734 ubi_btTraverse((ubi_btRootPtr)(Rp), (ubi_btActionRtn)(En), (void *)(Ud))
736 #define ubi_trKillTree( Rp, Fn ) \
737 ubi_btKillTree( (ubi_btRootPtr)(Rp), (ubi_btKillNodeRtn)(Fn) )
739 #define ubi_trLeafNode( Nd ) \
740 ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
742 #define ubi_trModuleID( s, l ) ubi_btModuleID( s, l )
744 /* ========================================================================== */
745 #endif /* ubi_BinTree_H */