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/10 14:46:39 crh
38 * This is the ubiqx binary tree and linked list library.
39 * This library is being included as part of the Samba distribution.
42 * Revision 2.4 1997/07/26 04:11:14 crh
43 * + Just to be annoying I changed ubi_TRUE and ubi_FALSE to ubi_trTRUE
45 * + There is now a type ubi_trBool to go with ubi_trTRUE and ubi_trFALSE.
46 * + There used to be something called "ubi_TypeDefs.h". I got rid of it.
47 * + Added function ubi_btLeafNode().
49 * Revision 2.3 1997/06/03 05:15:27 crh
50 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid conflicts.
51 * Also changed the interface to function InitTree(). See the comments
52 * for this function for more information.
54 * Revision 2.2 1995/10/03 22:00:40 CRH
57 * Revision 2.1 95/03/09 23:43:46 CRH
58 * Added the ModuleID static string and function. These modules are now
61 * Revision 2.0 95/02/27 22:00:33 CRH
62 * Revision 2.0 of this program includes the following changes:
64 * 1) A fix to a major typo in the RepaceNode() function.
65 * 2) The addition of the static function Border().
66 * 3) The addition of the public functions FirstOf() and LastOf(), which
67 * use Border(). These functions are used with trees that allow
69 * 4) A complete rewrite of the Locate() function. Locate() now accepts
70 * a "comparison" operator.
71 * 5) Overall enhancements to both code and comments.
73 * I decided to give this a new major rev number because the interface has
74 * changed. In particular, there are two new functions, and changes to the
77 * Revision 1.0 93/10/15 22:55:04 CRH
78 * With this revision, I have added a set of #define's that provide a single,
79 * standard API to all existing tree modules. Until now, each of the three
80 * existing modules had a different function and typedef prefix, as follows:
85 * ubi_SplayTree ubi_spt
87 * To further complicate matters, only those portions of the base module
88 * (ubi_BinTree) that were superceeded in the new module had the new names.
89 * For example, if you were using ubi_AVLtree, the AVL node structure was
90 * named "ubi_avlNode", but the root structure was still "ubi_btRoot". Using
91 * SplayTree, the locate function was called "ubi_sptLocate", but the next
92 * and previous functions remained "ubi_btNext" and "ubi_btPrev".
94 * This was not too terrible if you were familiar with the modules and knew
95 * exactly which tree model you wanted to use. If you wanted to be able to
96 * change modules (for speed comparisons, etc), things could get messy very
99 * So, I have added a set of defined names that get redefined in any of the
100 * descendant modules. To use this standardized interface in your code,
101 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
102 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
103 * datatype names for the module that you are using. Just remember to
104 * include the header for that module in your program file. Because these
105 * names are handled by the preprocessor, there is no added run-time
108 * Note that the original names do still exist, and can be used if you wish
109 * to write code directly to a specific module. This should probably only be
110 * done if you are planning to implement a new descendant type, such as
111 * red/black trees. CRH
113 * V0.0 - June, 1991 - Written by Christopher R. Hertel (CRH).
115 * ========================================================================== **
118 /* -------------------------------------------------------------------------- **
119 * Macros and constants.
122 * ubi_trTRUE - Boolean TRUE.
123 * ubi_trFALSE - Boolean FALSE.
125 * Flags used in the tree header:
126 * ubi_trOVERWRITE - This flag indicates that an existing node may be
127 * overwritten by a new node with a matching key.
128 * ubi_trDUPKEY - This flag indicates that the tree allows duplicate
129 * keys. If the tree does allow duplicates, the
130 * overwrite flag is ignored.
132 * Node link array index constants: (Each node has an array of three
133 * pointers. One to the left, one to the right, and one back to the
135 * LEFT - Left child pointer.
136 * PARENT - Parent pointer.
137 * RIGHT - Right child pointer.
138 * EQUAL - Synonym for PARENT.
140 * ubi_trCompOps: These values are used in the ubi_trLocate() function.
141 * ubi_trLT - request the first instance of the greatest key less than
143 * ubi_trLE - request the first instance of the greatest key that is less
144 * than or equal to the search key.
145 * ubi_trEQ - request the first instance of key that is equal to the
147 * ubi_trGE - request the first instance of a key that is greater than
148 * or equal to the search key.
149 * ubi_trGT - request the first instance of the first key that is greater
150 * than the search key.
151 * -------------------------------------------------------------------------- **
154 #define ubi_trTRUE 0xFF
155 #define ubi_trFALSE 0x00
157 #define ubi_trOVERWRITE 0x01 /* Turn on allow overwrite */
158 #define ubi_trDUPKEY 0x02 /* Turn on allow duplicate keys */
160 /* Pointer array index constants... */
174 /* -------------------------------------------------------------------------- **
175 * These three macros allow simple manipulation of pointer index values (LEFT,
176 * RIGHT, and PARENT).
178 * Normalize() - converts {LEFT, PARENT, RIGHT} into {-1, 0 ,1}. C
179 * uses {negative, zero, positive} values to indicate
180 * {less than, equal to, greater than}.
181 * AbNormal() - converts {negative, zero, positive} to {LEFT, PARENT,
182 * RIGHT} (opposite of Normalize()). Note: C comparison
183 * functions, such as strcmp(), return {negative, zero,
184 * positive} values, which are not necessarily {-1, 0,
185 * 1}. This macro uses the the ubi_btSgn() function to
187 * RevWay() - converts LEFT to RIGHT and RIGHT to LEFT. PARENT (EQUAL)
189 * -------------------------------------------------------------------------- **
191 #define Normalize(W) ((char)((W)-EQUAL))
192 #define AbNormal(W) ((char)( EQUAL+((char)ubi_btSgn( (W) )) ))
193 #define RevWay(W) ((char)((W)==LEFT?RIGHT:((W)==RIGHT?LEFT:EQUAL)))
195 /* -------------------------------------------------------------------------- **
196 * These macros allow us to quickly read the values of the OVERWRITE and
197 * DUPlicate KEY bits of the tree root flags field.
198 * -------------------------------------------------------------------------- **
200 #define Dups_OK(A) ((ubi_trDUPKEY & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
201 #define Ovwt_OK(A) ((ubi_trOVERWRITE & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
203 /* -------------------------------------------------------------------------- **
206 * ubi_trBool - Your typcial true or false...
208 * Item Pointer: The ubi_btItemPtr is a generic pointer. It is used to
209 * indicate a key that is being searched for within the tree.
210 * Searching occurs whenever the ubi_trFind(), ubi_trLocate(),
211 * or ubi_trInsert() functions are called.
212 * -------------------------------------------------------------------------- **
215 typedef unsigned char ubi_trBool;
217 typedef void *ubi_btItemPtr; /* A pointer to data within a node. */
219 /* ------------------------------------------------------------------------- **
220 * Binary Tree Node Structure: This structure defines the basic elements of
221 * the tree nodes. In general you *SHOULD NOT PLAY WITH THESE FIELDS*!
222 * But, of course, I have to put the structure into this header so that
223 * you can use it as a building block.
225 * The fields are as follows:
226 * Link - an array of pointers. These pointers are manipulated by
227 * the BT routines. The pointers indicate the left and right
228 * child nodes and the parent node. By keeping track of the
229 * parent pointer, we avoid the need for recursive routines or
230 * hand-tooled stacks to keep track of our path back to the
231 * root. The use of these pointers is subject to change without
233 * gender - a one-byte field indicating whether the node is the RIGHT or
234 * LEFT child of its parent. If the node is the root of the
235 * tree, gender will be PARENT.
236 * ------------------------------------------------------------------------- **
238 typedef struct ubi_btNodeStruct {
239 struct ubi_btNodeStruct *Link[ 3 ];
243 typedef ubi_btNode *ubi_btNodePtr; /* Pointer to an ubi_btNode structure. */
245 /* ------------------------------------------------------------------------- **
246 * The next three typedefs define standard function types used by the binary
247 * tree management routines. In particular:
249 * ubi_btCompFunc is a pointer to a comparison function. Comparison
250 * functions are passed an ubi_btItemPtr and an
251 * ubi_btNodePtr. They return a value that is (<0), 0,
252 * or (>0) to indicate that the Item is (respectively)
253 * "less than", "equal to", or "greater than" the Item
254 * contained within the node. (See ubi_btInitTree()).
255 * ubi_btActionRtn is a pointer to a function that may be called for each
256 * node visited when performing a tree traversal (see
257 * ubi_btTraverse()). The function will be passed two
258 * parameters: the first is a pointer to a node in the
259 * tree, the second is a generic pointer that may point to
260 * anything that you like.
261 * ubi_btKillNodeRtn is a pointer to a function that will deallocate the
262 * memory used by a node (see ubi_btKillTree()). Since
263 * memory management is left up to you, deallocation may
264 * mean anything that you want it to mean. Just remember
265 * that the tree *will* be destroyed and that none of the
266 * node pointers will be valid any more.
267 * ------------------------------------------------------------------------- **
270 typedef int (*ubi_btCompFunc)( ubi_btItemPtr, ubi_btNodePtr );
272 typedef void (*ubi_btActionRtn)( ubi_btNodePtr, void * );
274 typedef void (*ubi_btKillNodeRtn)( ubi_btNodePtr );
276 /* -------------------------------------------------------------------------- **
277 * Tree Root Structure: This structure gives us a convenient handle for
278 * accessing whole AVL trees. The fields are:
279 * root - A pointer to the root node of the AVL tree.
280 * count - A count of the number of nodes stored in the tree.
281 * cmp - A pointer to the comparison routine to be used when building or
282 * searching the tree.
283 * flags - A set of bit flags. Two flags are currently defined:
285 * ubi_trOVERWRITE - If set, this flag indicates that a new node should
286 * (bit 0x01) overwrite an old node if the two have identical
287 * keys (ie., the keys are equal).
288 * ubi_trDUPKEY - If set, this flag indicates that the tree is
289 * (bit 0x02) allowed to contain nodes with duplicate keys.
291 * NOTE: ubi_trInsert() tests ubi_trDUPKEY before ubi_trOVERWRITE.
293 * All of these values are set when you initialize the root structure by
294 * calling ubi_trInitTree().
295 * -------------------------------------------------------------------------- **
299 ubi_btNodePtr root; /* A pointer to the root node of the tree */
300 unsigned long count; /* A count of the number of nodes in the tree */
301 ubi_btCompFunc cmp; /* A pointer to the tree's comparison function */
302 unsigned char flags; /* Overwrite Y|N, Duplicate keys Y|N... */
305 typedef ubi_btRoot *ubi_btRootPtr; /* Pointer to an ubi_btRoot structure. */
308 /* -------------------------------------------------------------------------- **
309 * Function Prototypes.
312 long ubi_btSgn( long x );
313 /* ------------------------------------------------------------------------ **
314 * Return the sign of x; {negative,zero,positive} ==> {-1, 0, 1}.
316 * Input: x - a signed long integer value.
318 * Output: the "sign" of x, represented as follows:
320 * 0 == zero (no sign)
323 * Note: This utility is provided in order to facilitate the conversion
324 * of C comparison function return values into BinTree direction
325 * values: {LEFT, PARENT, EQUAL}. It is INCORPORATED into the
326 * AbNormal() conversion macro!
328 * ------------------------------------------------------------------------ **
331 ubi_btNodePtr ubi_btInitNode( ubi_btNodePtr NodePtr );
332 /* ------------------------------------------------------------------------ **
333 * Initialize a tree node.
335 * Input: a pointer to a ubi_btNode structure to be initialized.
336 * Output: a pointer to the initialized ubi_btNode structure (ie. the
337 * same as the input pointer).
338 * ------------------------------------------------------------------------ **
341 ubi_btRootPtr ubi_btInitTree( ubi_btRootPtr RootPtr,
342 ubi_btCompFunc CompFunc,
343 unsigned char Flags );
344 /* ------------------------------------------------------------------------ **
345 * Initialize the fields of a Tree Root header structure.
347 * Input: RootPtr - a pointer to an ubi_btRoot structure to be
349 * CompFunc - a pointer to a comparison function that will be used
350 * whenever nodes in the tree must be compared against
352 * Flags - One bytes worth of flags. Flags include
353 * ubi_trOVERWRITE and ubi_trDUPKEY. See the header
354 * file for more info.
356 * Output: a pointer to the initialized ubi_btRoot structure (ie. the
357 * same value as RootPtr).
359 * Note: The interface to this function has changed from that of
360 * previous versions. The <Flags> parameter replaces two
361 * boolean parameters that had the same basic effect.
362 * ------------------------------------------------------------------------ **
365 ubi_trBool ubi_btInsert( ubi_btRootPtr RootPtr,
366 ubi_btNodePtr NewNode,
367 ubi_btItemPtr ItemPtr,
368 ubi_btNodePtr *OldNode );
369 /* ------------------------------------------------------------------------ **
370 * This function uses a non-recursive algorithm to add a new element to the
373 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
374 * the root of the tree to which NewNode is to be added.
375 * NewNode - a pointer to an ubi_btNode structure that is NOT
377 * ItemPtr - A pointer to the sort key that is stored within
378 * *NewNode. ItemPtr MUST point to information stored
379 * in *NewNode or an EXACT DUPLICATE. The key data
380 * indicated by ItemPtr is used to place the new node
382 * OldNode - a pointer to an ubi_btNodePtr. When searching
383 * the tree, a duplicate node may be found. If
384 * duplicates are allowed, then the new node will
385 * be simply placed into the tree. If duplicates
386 * are not allowed, however, then one of two things
388 * 1) if overwritting *is not* allowed, this
389 * function will return FALSE (indicating that
390 * the new node could not be inserted), and
391 * *OldNode will point to the duplicate that is
393 * 2) if overwritting *is* allowed, then this
394 * function will swap **OldNode for *NewNode.
395 * In this case, *OldNode will point to the node
396 * that was removed (thus allowing you to free
398 * ** If you are using overwrite mode, ALWAYS **
399 * ** check the return value of this parameter! **
400 * Note: You may pass NULL in this parameter, the
401 * function knows how to cope. If you do this,
402 * however, there will be no way to return a
403 * pointer to an old (ie. replaced) node (which is
404 * a problem if you are using overwrite mode).
406 * Output: a boolean value indicating success or failure. The function
407 * will return FALSE if the node could not be added to the tree.
408 * Such failure will only occur if duplicates are not allowed,
409 * nodes cannot be overwritten, AND a duplicate key was found
411 * ------------------------------------------------------------------------ **
414 ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr,
415 ubi_btNodePtr DeadNode );
416 /* ------------------------------------------------------------------------ **
417 * This function removes the indicated node from the tree.
419 * Input: RootPtr - A pointer to the header of the tree that contains
420 * the node to be removed.
421 * DeadNode - A pointer to the node that will be removed.
423 * Output: This function returns a pointer to the node that was removed
424 * from the tree (ie. the same as DeadNode).
426 * Note: The node MUST be in the tree indicated by RootPtr. If not,
427 * strange and evil things will happen to your trees.
428 * ------------------------------------------------------------------------ **
431 ubi_btNodePtr ubi_btLocate( ubi_btRootPtr RootPtr,
432 ubi_btItemPtr FindMe,
433 ubi_trCompOps CompOp );
434 /* ------------------------------------------------------------------------ **
435 * The purpose of ubi_btLocate() is to find a node or set of nodes given
436 * a target value and a "comparison operator". The Locate() function is
437 * more flexible and (in the case of trees that may contain dupicate keys)
438 * more precise than the ubi_btFind() function. The latter is faster,
439 * but it only searches for exact matches and, if the tree contains
440 * duplicates, Find() may return a pointer to any one of the duplicate-
444 * RootPtr - A pointer to the header of the tree to be searched.
445 * FindMe - An ubi_btItemPtr that indicates the key for which to
447 * CompOp - One of the following:
448 * CompOp Return a pointer to the node with
449 * ------ ---------------------------------
450 * ubi_trLT - the last key value that is less
452 * ubi_trLE - the first key matching FindMe, or
453 * the last key that is less than
455 * ubi_trEQ - the first key matching FindMe.
456 * ubi_trGE - the first key matching FindMe, or the
457 * first key greater than FindMe.
458 * ubi_trGT - the first key greater than FindMe.
460 * A pointer to the node matching the criteria listed above under
461 * CompOp, or NULL if no node matched the criteria.
464 * In the case of trees with duplicate keys, Locate() will behave as
468 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6
472 * That is, when returning a pointer to a node with a key that is LESS
473 * THAN the target key (FindMe), Locate() will return a pointer to the
474 * LAST matching node.
475 * When returning a pointer to a node with a key that is GREATER
476 * THAN the target key (FindMe), Locate() will return a pointer to the
477 * FIRST matching node.
479 * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
480 * ------------------------------------------------------------------------ **
483 ubi_btNodePtr ubi_btFind( ubi_btRootPtr RootPtr,
484 ubi_btItemPtr FindMe );
485 /* ------------------------------------------------------------------------ **
486 * This function performs a non-recursive search of a tree for any node
487 * matching a specific key.
490 * RootPtr - a pointer to the header of the tree to be searched.
491 * FindMe - a pointer to the key value for which to search.
494 * A pointer to a node with a key that matches the key indicated by
495 * FindMe, or NULL if no such node was found.
497 * Note: In a tree that allows duplicates, the pointer returned *might
498 * not* point to the (sequentially) first occurance of the
499 * desired key. In such a tree, it may be more useful to use
501 * ------------------------------------------------------------------------ **
504 ubi_btNodePtr ubi_btNext( ubi_btNodePtr P );
505 /* ------------------------------------------------------------------------ **
506 * Given the node indicated by P, find the (sorted order) Next node in the
508 * Input: P - a pointer to a node that exists in a binary tree.
509 * Output: A pointer to the "next" node in the tree, or NULL if P pointed
510 * to the "last" node in the tree or was NULL.
511 * ------------------------------------------------------------------------ **
514 ubi_btNodePtr ubi_btPrev( ubi_btNodePtr P );
515 /* ------------------------------------------------------------------------ **
516 * Given the node indicated by P, find the (sorted order) Previous node in
518 * Input: P - a pointer to a node that exists in a binary tree.
519 * Output: A pointer to the "previous" node in the tree, or NULL if P
520 * pointed to the "first" node in the tree or was NULL.
521 * ------------------------------------------------------------------------ **
524 ubi_btNodePtr ubi_btFirst( ubi_btNodePtr P );
525 /* ------------------------------------------------------------------------ **
526 * Given the node indicated by P, find the (sorted order) First node in the
527 * subtree of which *P is the root.
528 * Input: P - a pointer to a node that exists in a binary tree.
529 * Output: A pointer to the "first" node in a subtree that has *P as its
530 * root. This function will return NULL only if P is NULL.
531 * Note: In general, you will be passing in the value of the root field
532 * of an ubi_btRoot structure.
533 * ------------------------------------------------------------------------ **
536 ubi_btNodePtr ubi_btLast( ubi_btNodePtr P );
537 /* ------------------------------------------------------------------------ **
538 * Given the node indicated by P, find the (sorted order) Last node in the
539 * subtree of which *P is the root.
540 * Input: P - a pointer to a node that exists in a binary tree.
541 * Output: A pointer to the "last" node in a subtree that has *P as its
542 * root. This function will return NULL only if P is NULL.
543 * Note: In general, you will be passing in the value of the root field
544 * of an ubi_btRoot structure.
545 * ------------------------------------------------------------------------ **
548 ubi_btNodePtr ubi_btFirstOf( ubi_btRootPtr RootPtr,
549 ubi_btItemPtr MatchMe,
551 /* ------------------------------------------------------------------------ **
552 * Given a tree that a allows duplicate keys, and a pointer to a node in
553 * the tree, this function will return a pointer to the first (traversal
554 * order) node with the same key value.
556 * Input: RootPtr - A pointer to the root of the tree.
557 * MatchMe - A pointer to the key value. This should probably
558 * point to the key within node *p.
559 * p - A pointer to a node in the tree.
560 * Output: A pointer to the first node in the set of nodes with keys
562 * Notes: Node *p MUST be in the set of nodes with keys matching
563 * <FindMe>. If not, this function will return NULL.
564 * ------------------------------------------------------------------------ **
567 ubi_btNodePtr ubi_btLastOf( ubi_btRootPtr RootPtr,
568 ubi_btItemPtr MatchMe,
570 /* ------------------------------------------------------------------------ **
571 * Given a tree that a allows duplicate keys, and a pointer to a node in
572 * the tree, this function will return a pointer to the last (traversal
573 * order) node with the same key value.
575 * Input: RootPtr - A pointer to the root of the tree.
576 * MatchMe - A pointer to the key value. This should probably
577 * point to the key within node *p.
578 * p - A pointer to a node in the tree.
579 * Output: A pointer to the last node in the set of nodes with keys
581 * Notes: Node *p MUST be in the set of nodes with keys matching
582 * <FindMe>. If not, this function will return NULL.
583 * ------------------------------------------------------------------------ **
586 ubi_trBool ubi_btTraverse( ubi_btRootPtr RootPtr,
587 ubi_btActionRtn EachNode,
589 /* ------------------------------------------------------------------------ **
590 * Traverse a tree in sorted order (non-recursively). At each node, call
591 * (*EachNode)(), passing a pointer to the current node, and UserData as the
593 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
594 * the tree to be traversed.
595 * EachNode - a pointer to a function to be called at each node
596 * as the node is visited.
597 * UserData - a generic pointer that may point to anything that
599 * Output: A boolean value. FALSE if the tree is empty, otherwise TRUE.
600 * ------------------------------------------------------------------------ **
603 ubi_trBool ubi_btKillTree( ubi_btRootPtr RootPtr,
604 ubi_btKillNodeRtn FreeNode );
605 /* ------------------------------------------------------------------------ **
606 * Delete an entire tree (non-recursively) and reinitialize the ubi_btRoot
607 * structure. Note that this function will return FALSE if either parameter
610 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
611 * the root of the tree to delete.
612 * FreeNode - a function that will be called for each node in the
613 * tree to deallocate the memory used by the node.
615 * Output: A boolean value. FALSE if either input parameter was NULL, else
618 * ------------------------------------------------------------------------ **
621 ubi_btNodePtr ubi_btLeafNode( ubi_btNodePtr leader );
622 /* ------------------------------------------------------------------------ **
623 * Returns a pointer to a leaf node.
625 * Input: leader - Pointer to a node at which to start the descent.
627 * Output: A pointer to a leaf node selected in a somewhat arbitrary
630 * Notes: I wrote this function because I was using splay trees as a
631 * database cache. The cache had a maximum size on it, and I
632 * needed a way of choosing a node to sacrifice if the cache
633 * became full. In a splay tree, less recently accessed nodes
634 * tend toward the bottom of the tree, meaning that leaf nodes
635 * are good candidates for removal. (I really can't think of
636 * any other reason to use this function.)
637 * + In a simple binary tree or an AVL tree, the most recently
638 * added nodes tend to be nearer the bottom, making this a *bad*
639 * way to choose which node to remove from the cache.
640 * + Randomizing the traversal order is probably a good idea. You
641 * can improve the randomization of leaf node selection by passing
642 * in pointers to nodes other than the root node each time. A
643 * pointer to any node in the tree will do. Of course, if you
644 * pass a pointer to a leaf node you'll get the same thing back.
646 * ------------------------------------------------------------------------ **
650 int ubi_btModuleID( int size, char *list[] );
651 /* ------------------------------------------------------------------------ **
652 * Returns a set of strings that identify the module.
654 * Input: size - The number of elements in the array <list>.
655 * list - An array of pointers of type (char *). This array
656 * should, initially, be empty. This function will fill
657 * in the array with pointers to strings.
658 * Output: The number of elements of <list> that were used. If this value
659 * is less than <size>, the values of the remaining elements are
662 * Notes: Please keep in mind that the pointers returned indicate strings
663 * stored in static memory. Don't free() them, don't write over
664 * them, etc. Just read them.
665 * ------------------------------------------------------------------------ **
668 /* -------------------------------------------------------------------------- **
671 * This set of defines allows you to write programs that will use any of the
672 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
673 * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
674 * including the appropriate module header.
677 #define ubi_trItemPtr ubi_btItemPtr
679 #define ubi_trNode ubi_btNode
680 #define ubi_trNodePtr ubi_btNodePtr
682 #define ubi_trRoot ubi_btRoot
683 #define ubi_trRootPtr ubi_btRootPtr
685 #define ubi_trCompFunc ubi_btCompFunc
686 #define ubi_trActionRtn ubi_btActionRtn
687 #define ubi_trKillNodeRtn ubi_btKillNodeRtn
689 #define ubi_trSgn( x ) ubi_btSgn( x )
691 #define ubi_trInitNode( Np ) ubi_btInitNode( (ubi_btNodePtr)(Np) )
693 #define ubi_trInitTree( Rp, Cf, Fl ) \
694 ubi_btInitTree( (ubi_btRootPtr)(Rp), (ubi_btCompFunc)(Cf), (Fl) )
696 #define ubi_trInsert( Rp, Nn, Ip, On ) \
697 ubi_btInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
698 (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
700 #define ubi_trRemove( Rp, Dn ) \
701 ubi_btRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
703 #define ubi_trLocate( Rp, Ip, Op ) \
704 ubi_btLocate( (ubi_btRootPtr)(Rp), \
705 (ubi_btItemPtr)(Ip), \
706 (ubi_trCompOps)(Op) )
708 #define ubi_trFind( Rp, Ip ) \
709 ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
711 #define ubi_trNext( P ) ubi_btNext( (ubi_btNodePtr)(P) )
713 #define ubi_trPrev( P ) ubi_btPrev( (ubi_btNodePtr)(P) )
715 #define ubi_trFirst( P ) ubi_btFirst( (ubi_btNodePtr)(P) )
717 #define ubi_trLast( P ) ubi_btLast( (ubi_btNodePtr)(P) )
719 #define ubi_trFirstOf( Rp, Ip, P ) \
720 ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
721 (ubi_btItemPtr)(Ip), \
724 #define ubi_trLastOf( Rp, Ip, P ) \
725 ubi_btLastOf( (ubi_btRootPtr)(Rp), \
726 (ubi_btItemPtr)(Ip), \
729 #define ubi_trTraverse( Rp, En, Ud ) \
730 ubi_btTraverse((ubi_btRootPtr)(Rp), (ubi_btActionRtn)(En), (void *)(Ud))
732 #define ubi_trKillTree( Rp, Fn ) \
733 ubi_btKillTree( (ubi_btRootPtr)(Rp), (ubi_btKillNodeRtn)(Fn) )
735 #define ubi_trLeafNode( Nd ) \
736 ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
738 #define ubi_trModuleID( s, l ) ubi_btModuleID( s, l )
740 /* ========================================================================== */
741 #endif /* ubi_BinTree_H */