3 /* ========================================================================== **
6 * Copyright (C) 1991-1998 by Christopher R. Hertel
8 * Email: crh@ubiqx.mn.org
9 * -------------------------------------------------------------------------- **
11 * This module implements a simple binary tree.
13 * -------------------------------------------------------------------------- **
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Library General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Library General Public License for more details.
25 * You should have received a copy of the GNU Library General Public
26 * License along with this library; if not, write to the Free
27 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * -------------------------------------------------------------------------- **
31 * Log: ubi_BinTree.h,v
32 * Revision 4.1 1998/03/31 06:13:47 crh
33 * Thomas Aglassinger sent E'mail pointing out errors in the
34 * dereferencing of function pointers, and a missing typecast.
37 * Revision 4.0 1998/03/10 03:16:04 crh
38 * Added the AVL field 'balance' to the ubi_btNode structure. This means
39 * that all BinTree modules now use the same basic node structure, which
40 * greatly simplifies the AVL module.
41 * Decided that this was a big enough change to justify a new major revision
42 * number. 3.0 was an error, so we're at 4.0.
44 * Revision 2.6 1998/01/24 06:27:30 crh
45 * Added ubi_trCount() macro.
47 * Revision 2.5 1997/12/23 03:59:21 crh
48 * In this version, all constants & macros defined in the header file have
49 * the ubi_tr prefix. Also cleaned up anything that gcc complained about
50 * when run with '-pedantic -fsyntax-only -Wall'.
52 * Revision 2.4 1997/07/26 04:11:14 crh
53 * + Just to be annoying I changed ubi_TRUE and ubi_FALSE to ubi_trTRUE
55 * + There is now a type ubi_trBool to go with ubi_trTRUE and ubi_trFALSE.
56 * + There used to be something called "ubi_TypeDefs.h". I got rid of it.
57 * + Added function ubi_btLeafNode().
59 * Revision 2.3 1997/06/03 05:15:27 crh
60 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid conflicts.
61 * Also changed the interface to function InitTree(). See the comments
62 * for this function for more information.
64 * Revision 2.2 1995/10/03 22:00:40 CRH
67 * Revision 2.1 95/03/09 23:43:46 CRH
68 * Added the ModuleID static string and function. These modules are now
71 * Revision 2.0 95/02/27 22:00:33 CRH
72 * Revision 2.0 of this program includes the following changes:
74 * 1) A fix to a major typo in the RepaceNode() function.
75 * 2) The addition of the static function Border().
76 * 3) The addition of the public functions FirstOf() and LastOf(), which
77 * use Border(). These functions are used with trees that allow
79 * 4) A complete rewrite of the Locate() function. Locate() now accepts
80 * a "comparison" operator.
81 * 5) Overall enhancements to both code and comments.
83 * I decided to give this a new major rev number because the interface has
84 * changed. In particular, there are two new functions, and changes to the
87 * Revision 1.0 93/10/15 22:55:04 CRH
88 * With this revision, I have added a set of #define's that provide a single,
89 * standard API to all existing tree modules. Until now, each of the three
90 * existing modules had a different function and typedef prefix, as follows:
95 * ubi_SplayTree ubi_spt
97 * To further complicate matters, only those portions of the base module
98 * (ubi_BinTree) that were superceeded in the new module had the new names.
99 * For example, if you were using ubi_SplayTree, the locate function was
100 * called "ubi_sptLocate", but the next and previous functions remained
101 * "ubi_btNext" and "ubi_btPrev".
103 * This was not too terrible if you were familiar with the modules and knew
104 * exactly which tree model you wanted to use. If you wanted to be able to
105 * change modules (for speed comparisons, etc), things could get messy very
108 * So, I have added a set of defined names that get redefined in any of the
109 * descendant modules. To use this standardized interface in your code,
110 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
111 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
112 * datatype names for the module that you are using. Just remember to
113 * include the header for that module in your program file. Because these
114 * names are handled by the preprocessor, there is no added run-time
117 * Note that the original names do still exist, and can be used if you wish
118 * to write code directly to a specific module. This should probably only be
119 * done if you are planning to implement a new descendant type, such as
120 * red/black trees. CRH
122 * V0.0 - June, 1991 - Written by Christopher R. Hertel (CRH).
124 * ========================================================================== **
127 /* -------------------------------------------------------------------------- **
128 * Macros and constants.
131 * ubi_trTRUE - Boolean TRUE.
132 * ubi_trFALSE - Boolean FALSE.
134 * Flags used in the tree header:
135 * ubi_trOVERWRITE - This flag indicates that an existing node may be
136 * overwritten by a new node with a matching key.
137 * ubi_trDUPKEY - This flag indicates that the tree allows duplicate
138 * keys. If the tree does allow duplicates, the
139 * overwrite flag is ignored.
141 * Node link array index constants: (Each node has an array of three
142 * pointers. One to the left, one to the right, and one back to the
144 * ubi_trLEFT - Left child pointer.
145 * ubi_trPARENT - Parent pointer.
146 * ubi_trRIGHT - Right child pointer.
147 * ubi_trEQUAL - Synonym for PARENT.
149 * ubi_trCompOps: These values are used in the ubi_trLocate() function.
150 * ubi_trLT - request the first instance of the greatest key less than
152 * ubi_trLE - request the first instance of the greatest key that is less
153 * than or equal to the search key.
154 * ubi_trEQ - request the first instance of key that is equal to the
156 * ubi_trGE - request the first instance of a key that is greater than
157 * or equal to the search key.
158 * ubi_trGT - request the first instance of the first key that is greater
159 * than the search key.
160 * -------------------------------------------------------------------------- **
163 #define ubi_trTRUE 0xFF
164 #define ubi_trFALSE 0x00
166 #define ubi_trOVERWRITE 0x01 /* Turn on allow overwrite */
167 #define ubi_trDUPKEY 0x02 /* Turn on allow duplicate keys */
169 /* Pointer array index constants... */
170 #define ubi_trLEFT 0x00
171 #define ubi_trPARENT 0x01
172 #define ubi_trRIGHT 0x02
173 #define ubi_trEQUAL ubi_trPARENT
183 /* -------------------------------------------------------------------------- **
184 * These three macros allow simple manipulation of pointer index values (LEFT,
185 * RIGHT, and PARENT).
187 * Normalize() - converts {LEFT, PARENT, RIGHT} into {-1, 0 ,1}. C
188 * uses {negative, zero, positive} values to indicate
189 * {less than, equal to, greater than}.
190 * AbNormal() - converts {negative, zero, positive} to {LEFT, PARENT,
191 * RIGHT} (opposite of Normalize()). Note: C comparison
192 * functions, such as strcmp(), return {negative, zero,
193 * positive} values, which are not necessarily {-1, 0,
194 * 1}. This macro uses the the ubi_btSgn() function to
196 * RevWay() - converts LEFT to RIGHT and RIGHT to LEFT. PARENT (EQUAL)
198 * -------------------------------------------------------------------------- **
200 #define ubi_trNormalize(W) ((char)( (W) - ubi_trEQUAL ))
201 #define ubi_trAbNormal(W) ((char)( ((char)ubi_btSgn( (long)(W) )) \
203 #define ubi_trRevWay(W) ((char)( ubi_trEQUAL - ((W) - ubi_trEQUAL) ))
205 /* -------------------------------------------------------------------------- **
206 * These macros allow us to quickly read the values of the OVERWRITE and
207 * DUPlicate KEY bits of the tree root flags field.
208 * -------------------------------------------------------------------------- **
210 #define ubi_trDups_OK(A) \
211 ((ubi_trDUPKEY & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
212 #define ubi_trOvwt_OK(A) \
213 ((ubi_trOVERWRITE & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
215 /* -------------------------------------------------------------------------- **
216 * A quickie for consistency.
217 * ubi_trCount() - Given a pointer to a tree root, this macro returns the
218 * number of nodes currently in the tree.
220 * -------------------------------------------------------------------------- **
223 #define ubi_trCount( R ) (((ubi_trRootPtr)(R))->count)
225 /* -------------------------------------------------------------------------- **
228 * ubi_trBool - Your typcial true or false...
230 * Item Pointer: The ubi_btItemPtr is a generic pointer. It is used to
231 * indicate a key that is being searched for within the tree.
232 * Searching occurs whenever the ubi_trFind(), ubi_trLocate(),
233 * or ubi_trInsert() functions are called.
234 * -------------------------------------------------------------------------- **
237 typedef unsigned char ubi_trBool;
239 typedef void *ubi_btItemPtr; /* A pointer to key data within a node. */
241 /* ------------------------------------------------------------------------- **
242 * Binary Tree Node Structure: This structure defines the basic elements of
243 * the tree nodes. In general you *SHOULD NOT PLAY WITH THESE FIELDS*!
244 * But, of course, I have to put the structure into this header so that
245 * you can use it as a building block.
247 * The fields are as follows:
248 * Link - an array of pointers. These pointers are manipulated by
249 * the BT routines. The pointers indicate the left and right
250 * child nodes and the parent node. By keeping track of the
251 * parent pointer, we avoid the need for recursive routines or
252 * hand-tooled stacks to keep track of our path back to the
253 * root. The use of these pointers is subject to change without
255 * gender - a one-byte field indicating whether the node is the RIGHT or
256 * LEFT child of its parent. If the node is the root of the
257 * tree, gender will be PARENT.
258 * balance - only used by the AVL tree module. This field indicates
259 * the height balance at a given node. See ubi_AVLtree for
262 * ------------------------------------------------------------------------- **
264 typedef struct ubi_btNodeStruct {
265 struct ubi_btNodeStruct *Link[ 3 ];
270 typedef ubi_btNode *ubi_btNodePtr; /* Pointer to an ubi_btNode structure. */
272 /* ------------------------------------------------------------------------- **
273 * The next three typedefs define standard function types used by the binary
274 * tree management routines. In particular:
276 * ubi_btCompFunc is a pointer to a comparison function. Comparison
277 * functions are passed an ubi_btItemPtr and an
278 * ubi_btNodePtr. They return a value that is (<0), 0,
279 * or (>0) to indicate that the Item is (respectively)
280 * "less than", "equal to", or "greater than" the Item
281 * contained within the node. (See ubi_btInitTree()).
282 * ubi_btActionRtn is a pointer to a function that may be called for each
283 * node visited when performing a tree traversal (see
284 * ubi_btTraverse()). The function will be passed two
285 * parameters: the first is a pointer to a node in the
286 * tree, the second is a generic pointer that may point to
287 * anything that you like.
288 * ubi_btKillNodeRtn is a pointer to a function that will deallocate the
289 * memory used by a node (see ubi_btKillTree()). Since
290 * memory management is left up to you, deallocation may
291 * mean anything that you want it to mean. Just remember
292 * that the tree *will* be destroyed and that none of the
293 * node pointers will be valid any more.
294 * ------------------------------------------------------------------------- **
297 typedef int (*ubi_btCompFunc)( ubi_btItemPtr, ubi_btNodePtr );
299 typedef void (*ubi_btActionRtn)( ubi_btNodePtr, void * );
301 typedef void (*ubi_btKillNodeRtn)( ubi_btNodePtr );
303 /* -------------------------------------------------------------------------- **
304 * Tree Root Structure: This structure gives us a convenient handle for
305 * accessing whole binary trees. The fields are:
306 * root - A pointer to the root node of the tree.
307 * count - A count of the number of nodes stored in the tree.
308 * cmp - A pointer to the comparison routine to be used when building or
309 * searching the tree.
310 * flags - A set of bit flags. Two flags are currently defined:
312 * ubi_trOVERWRITE - If set, this flag indicates that a new node should
313 * (bit 0x01) overwrite an old node if the two have identical
314 * keys (ie., the keys are equal).
315 * ubi_trDUPKEY - If set, this flag indicates that the tree is
316 * (bit 0x02) allowed to contain nodes with duplicate keys.
318 * NOTE: ubi_trInsert() tests ubi_trDUPKEY before ubi_trOVERWRITE.
320 * All of these values are set when you initialize the root structure by
321 * calling ubi_trInitTree().
322 * -------------------------------------------------------------------------- **
326 ubi_btNodePtr root; /* A pointer to the root node of the tree */
327 ubi_btCompFunc cmp; /* A pointer to the tree's comparison function */
328 unsigned long count; /* A count of the number of nodes in the tree */
329 unsigned char flags; /* Overwrite Y|N, Duplicate keys Y|N... */
332 typedef ubi_btRoot *ubi_btRootPtr; /* Pointer to an ubi_btRoot structure. */
335 /* -------------------------------------------------------------------------- **
336 * Function Prototypes.
339 long ubi_btSgn( long x );
340 /* ------------------------------------------------------------------------ **
341 * Return the sign of x; {negative,zero,positive} ==> {-1, 0, 1}.
343 * Input: x - a signed long integer value.
345 * Output: the "sign" of x, represented as follows:
347 * 0 == zero (no sign)
350 * Note: This utility is provided in order to facilitate the conversion
351 * of C comparison function return values into BinTree direction
352 * values: {LEFT, PARENT, EQUAL}. It is INCORPORATED into the
353 * AbNormal() conversion macro!
355 * ------------------------------------------------------------------------ **
358 ubi_btNodePtr ubi_btInitNode( ubi_btNodePtr NodePtr );
359 /* ------------------------------------------------------------------------ **
360 * Initialize a tree node.
362 * Input: a pointer to a ubi_btNode structure to be initialized.
363 * Output: a pointer to the initialized ubi_btNode structure (ie. the
364 * same as the input pointer).
365 * ------------------------------------------------------------------------ **
368 ubi_btRootPtr ubi_btInitTree( ubi_btRootPtr RootPtr,
369 ubi_btCompFunc CompFunc,
370 unsigned char Flags );
371 /* ------------------------------------------------------------------------ **
372 * Initialize the fields of a Tree Root header structure.
374 * Input: RootPtr - a pointer to an ubi_btRoot structure to be
376 * CompFunc - a pointer to a comparison function that will be used
377 * whenever nodes in the tree must be compared against
379 * Flags - One bytes worth of flags. Flags include
380 * ubi_trOVERWRITE and ubi_trDUPKEY. See the header
381 * file for more info.
383 * Output: a pointer to the initialized ubi_btRoot structure (ie. the
384 * same value as RootPtr).
386 * Note: The interface to this function has changed from that of
387 * previous versions. The <Flags> parameter replaces two
388 * boolean parameters that had the same basic effect.
389 * ------------------------------------------------------------------------ **
392 ubi_trBool ubi_btInsert( ubi_btRootPtr RootPtr,
393 ubi_btNodePtr NewNode,
394 ubi_btItemPtr ItemPtr,
395 ubi_btNodePtr *OldNode );
396 /* ------------------------------------------------------------------------ **
397 * This function uses a non-recursive algorithm to add a new element to the
400 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
401 * the root of the tree to which NewNode is to be added.
402 * NewNode - a pointer to an ubi_btNode structure that is NOT
404 * ItemPtr - A pointer to the sort key that is stored within
405 * *NewNode. ItemPtr MUST point to information stored
406 * in *NewNode or an EXACT DUPLICATE. The key data
407 * indicated by ItemPtr is used to place the new node
409 * OldNode - a pointer to an ubi_btNodePtr. When searching
410 * the tree, a duplicate node may be found. If
411 * duplicates are allowed, then the new node will
412 * be simply placed into the tree. If duplicates
413 * are not allowed, however, then one of two things
415 * 1) if overwritting *is not* allowed, this
416 * function will return FALSE (indicating that
417 * the new node could not be inserted), and
418 * *OldNode will point to the duplicate that is
420 * 2) if overwritting *is* allowed, then this
421 * function will swap **OldNode for *NewNode.
422 * In this case, *OldNode will point to the node
423 * that was removed (thus allowing you to free
425 * ** If you are using overwrite mode, ALWAYS **
426 * ** check the return value of this parameter! **
427 * Note: You may pass NULL in this parameter, the
428 * function knows how to cope. If you do this,
429 * however, there will be no way to return a
430 * pointer to an old (ie. replaced) node (which is
431 * a problem if you are using overwrite mode).
433 * Output: a boolean value indicating success or failure. The function
434 * will return FALSE if the node could not be added to the tree.
435 * Such failure will only occur if duplicates are not allowed,
436 * nodes cannot be overwritten, AND a duplicate key was found
438 * ------------------------------------------------------------------------ **
441 ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr,
442 ubi_btNodePtr DeadNode );
443 /* ------------------------------------------------------------------------ **
444 * This function removes the indicated node from the tree.
446 * Input: RootPtr - A pointer to the header of the tree that contains
447 * the node to be removed.
448 * DeadNode - A pointer to the node that will be removed.
450 * Output: This function returns a pointer to the node that was removed
451 * from the tree (ie. the same as DeadNode).
453 * Note: The node MUST be in the tree indicated by RootPtr. If not,
454 * strange and evil things will happen to your trees.
455 * ------------------------------------------------------------------------ **
458 ubi_btNodePtr ubi_btLocate( ubi_btRootPtr RootPtr,
459 ubi_btItemPtr FindMe,
460 ubi_trCompOps CompOp );
461 /* ------------------------------------------------------------------------ **
462 * The purpose of ubi_btLocate() is to find a node or set of nodes given
463 * a target value and a "comparison operator". The Locate() function is
464 * more flexible and (in the case of trees that may contain dupicate keys)
465 * more precise than the ubi_btFind() function. The latter is faster,
466 * but it only searches for exact matches and, if the tree contains
467 * duplicates, Find() may return a pointer to any one of the duplicate-
471 * RootPtr - A pointer to the header of the tree to be searched.
472 * FindMe - An ubi_btItemPtr that indicates the key for which to
474 * CompOp - One of the following:
475 * CompOp Return a pointer to the node with
476 * ------ ---------------------------------
477 * ubi_trLT - the last key value that is less
479 * ubi_trLE - the first key matching FindMe, or
480 * the last key that is less than
482 * ubi_trEQ - the first key matching FindMe.
483 * ubi_trGE - the first key matching FindMe, or the
484 * first key greater than FindMe.
485 * ubi_trGT - the first key greater than FindMe.
487 * A pointer to the node matching the criteria listed above under
488 * CompOp, or NULL if no node matched the criteria.
491 * In the case of trees with duplicate keys, Locate() will behave as
495 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6
499 * That is, when returning a pointer to a node with a key that is LESS
500 * THAN the target key (FindMe), Locate() will return a pointer to the
501 * LAST matching node.
502 * When returning a pointer to a node with a key that is GREATER
503 * THAN the target key (FindMe), Locate() will return a pointer to the
504 * FIRST matching node.
506 * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
507 * ------------------------------------------------------------------------ **
510 ubi_btNodePtr ubi_btFind( ubi_btRootPtr RootPtr,
511 ubi_btItemPtr FindMe );
512 /* ------------------------------------------------------------------------ **
513 * This function performs a non-recursive search of a tree for any node
514 * matching a specific key.
517 * RootPtr - a pointer to the header of the tree to be searched.
518 * FindMe - a pointer to the key value for which to search.
521 * A pointer to a node with a key that matches the key indicated by
522 * FindMe, or NULL if no such node was found.
524 * Note: In a tree that allows duplicates, the pointer returned *might
525 * not* point to the (sequentially) first occurance of the
526 * desired key. In such a tree, it may be more useful to use
528 * ------------------------------------------------------------------------ **
531 ubi_btNodePtr ubi_btNext( ubi_btNodePtr P );
532 /* ------------------------------------------------------------------------ **
533 * Given the node indicated by P, find the (sorted order) Next node in the
535 * Input: P - a pointer to a node that exists in a binary tree.
536 * Output: A pointer to the "next" node in the tree, or NULL if P pointed
537 * to the "last" node in the tree or was NULL.
538 * ------------------------------------------------------------------------ **
541 ubi_btNodePtr ubi_btPrev( ubi_btNodePtr P );
542 /* ------------------------------------------------------------------------ **
543 * Given the node indicated by P, find the (sorted order) Previous node in
545 * Input: P - a pointer to a node that exists in a binary tree.
546 * Output: A pointer to the "previous" node in the tree, or NULL if P
547 * pointed to the "first" node in the tree or was NULL.
548 * ------------------------------------------------------------------------ **
551 ubi_btNodePtr ubi_btFirst( ubi_btNodePtr P );
552 /* ------------------------------------------------------------------------ **
553 * Given the node indicated by P, find the (sorted order) First node in the
554 * subtree of which *P is the root.
555 * Input: P - a pointer to a node that exists in a binary tree.
556 * Output: A pointer to the "first" node in a subtree that has *P as its
557 * root. This function will return NULL only if P is NULL.
558 * Note: In general, you will be passing in the value of the root field
559 * of an ubi_btRoot structure.
560 * ------------------------------------------------------------------------ **
563 ubi_btNodePtr ubi_btLast( ubi_btNodePtr P );
564 /* ------------------------------------------------------------------------ **
565 * Given the node indicated by P, find the (sorted order) Last node in the
566 * subtree of which *P is the root.
567 * Input: P - a pointer to a node that exists in a binary tree.
568 * Output: A pointer to the "last" node in a subtree that has *P as its
569 * root. This function will return NULL only if P is NULL.
570 * Note: In general, you will be passing in the value of the root field
571 * of an ubi_btRoot structure.
572 * ------------------------------------------------------------------------ **
575 ubi_btNodePtr ubi_btFirstOf( ubi_btRootPtr RootPtr,
576 ubi_btItemPtr MatchMe,
578 /* ------------------------------------------------------------------------ **
579 * Given a tree that a allows duplicate keys, and a pointer to a node in
580 * the tree, this function will return a pointer to the first (traversal
581 * order) node with the same key value.
583 * Input: RootPtr - A pointer to the root of the tree.
584 * MatchMe - A pointer to the key value. This should probably
585 * point to the key within node *p.
586 * p - A pointer to a node in the tree.
587 * Output: A pointer to the first node in the set of nodes with keys
589 * Notes: Node *p MUST be in the set of nodes with keys matching
590 * <FindMe>. If not, this function will return NULL.
591 * ------------------------------------------------------------------------ **
594 ubi_btNodePtr ubi_btLastOf( ubi_btRootPtr RootPtr,
595 ubi_btItemPtr MatchMe,
597 /* ------------------------------------------------------------------------ **
598 * Given a tree that a allows duplicate keys, and a pointer to a node in
599 * the tree, this function will return a pointer to the last (traversal
600 * order) node with the same key value.
602 * Input: RootPtr - A pointer to the root of the tree.
603 * MatchMe - A pointer to the key value. This should probably
604 * point to the key within node *p.
605 * p - A pointer to a node in the tree.
606 * Output: A pointer to the last node in the set of nodes with keys
608 * Notes: Node *p MUST be in the set of nodes with keys matching
609 * <FindMe>. If not, this function will return NULL.
610 * ------------------------------------------------------------------------ **
613 ubi_trBool ubi_btTraverse( ubi_btRootPtr RootPtr,
614 ubi_btActionRtn EachNode,
616 /* ------------------------------------------------------------------------ **
617 * Traverse a tree in sorted order (non-recursively). At each node, call
618 * (*EachNode)(), passing a pointer to the current node, and UserData as the
620 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
621 * the tree to be traversed.
622 * EachNode - a pointer to a function to be called at each node
623 * as the node is visited.
624 * UserData - a generic pointer that may point to anything that
626 * Output: A boolean value. FALSE if the tree is empty, otherwise TRUE.
627 * ------------------------------------------------------------------------ **
630 ubi_trBool ubi_btKillTree( ubi_btRootPtr RootPtr,
631 ubi_btKillNodeRtn FreeNode );
632 /* ------------------------------------------------------------------------ **
633 * Delete an entire tree (non-recursively) and reinitialize the ubi_btRoot
634 * structure. Note that this function will return FALSE if either parameter
637 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
638 * the root of the tree to delete.
639 * FreeNode - a function that will be called for each node in the
640 * tree to deallocate the memory used by the node.
642 * Output: A boolean value. FALSE if either input parameter was NULL, else
645 * ------------------------------------------------------------------------ **
648 ubi_btNodePtr ubi_btLeafNode( ubi_btNodePtr leader );
649 /* ------------------------------------------------------------------------ **
650 * Returns a pointer to a leaf node.
652 * Input: leader - Pointer to a node at which to start the descent.
654 * Output: A pointer to a leaf node selected in a somewhat arbitrary
657 * Notes: I wrote this function because I was using splay trees as a
658 * database cache. The cache had a maximum size on it, and I
659 * needed a way of choosing a node to sacrifice if the cache
660 * became full. In a splay tree, less recently accessed nodes
661 * tend toward the bottom of the tree, meaning that leaf nodes
662 * are good candidates for removal. (I really can't think of
663 * any other reason to use this function.)
664 * + In a simple binary tree or an AVL tree, the most recently
665 * added nodes tend to be nearer the bottom, making this a *bad*
666 * way to choose which node to remove from the cache.
667 * + Randomizing the traversal order is probably a good idea. You
668 * can improve the randomization of leaf node selection by passing
669 * in pointers to nodes other than the root node each time. A
670 * pointer to any node in the tree will do. Of course, if you
671 * pass a pointer to a leaf node you'll get the same thing back.
673 * ------------------------------------------------------------------------ **
677 int ubi_btModuleID( int size, char *list[] );
678 /* ------------------------------------------------------------------------ **
679 * Returns a set of strings that identify the module.
681 * Input: size - The number of elements in the array <list>.
682 * list - An array of pointers of type (char *). This array
683 * should, initially, be empty. This function will fill
684 * in the array with pointers to strings.
685 * Output: The number of elements of <list> that were used. If this value
686 * is less than <size>, the values of the remaining elements are
689 * Notes: Please keep in mind that the pointers returned indicate strings
690 * stored in static memory. Don't free() them, don't write over
691 * them, etc. Just read them.
692 * ------------------------------------------------------------------------ **
695 /* -------------------------------------------------------------------------- **
698 * This set of defines allows you to write programs that will use any of the
699 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
700 * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
701 * including the appropriate module header.
704 #define ubi_trItemPtr ubi_btItemPtr
706 #define ubi_trNode ubi_btNode
707 #define ubi_trNodePtr ubi_btNodePtr
709 #define ubi_trRoot ubi_btRoot
710 #define ubi_trRootPtr ubi_btRootPtr
712 #define ubi_trCompFunc ubi_btCompFunc
713 #define ubi_trActionRtn ubi_btActionRtn
714 #define ubi_trKillNodeRtn ubi_btKillNodeRtn
716 #define ubi_trSgn( x ) ubi_btSgn( x )
718 #define ubi_trInitNode( Np ) ubi_btInitNode( (ubi_btNodePtr)(Np) )
720 #define ubi_trInitTree( Rp, Cf, Fl ) \
721 ubi_btInitTree( (ubi_btRootPtr)(Rp), (ubi_btCompFunc)(Cf), (Fl) )
723 #define ubi_trInsert( Rp, Nn, Ip, On ) \
724 ubi_btInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
725 (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
727 #define ubi_trRemove( Rp, Dn ) \
728 ubi_btRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
730 #define ubi_trLocate( Rp, Ip, Op ) \
731 ubi_btLocate( (ubi_btRootPtr)(Rp), \
732 (ubi_btItemPtr)(Ip), \
733 (ubi_trCompOps)(Op) )
735 #define ubi_trFind( Rp, Ip ) \
736 ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
738 #define ubi_trNext( P ) ubi_btNext( (ubi_btNodePtr)(P) )
740 #define ubi_trPrev( P ) ubi_btPrev( (ubi_btNodePtr)(P) )
742 #define ubi_trFirst( P ) ubi_btFirst( (ubi_btNodePtr)(P) )
744 #define ubi_trLast( P ) ubi_btLast( (ubi_btNodePtr)(P) )
746 #define ubi_trFirstOf( Rp, Ip, P ) \
747 ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
748 (ubi_btItemPtr)(Ip), \
751 #define ubi_trLastOf( Rp, Ip, P ) \
752 ubi_btLastOf( (ubi_btRootPtr)(Rp), \
753 (ubi_btItemPtr)(Ip), \
756 #define ubi_trTraverse( Rp, En, Ud ) \
757 ubi_btTraverse((ubi_btRootPtr)(Rp), (ubi_btActionRtn)(En), (void *)(Ud))
759 #define ubi_trKillTree( Rp, Fn ) \
760 ubi_btKillTree( (ubi_btRootPtr)(Rp), (ubi_btKillNodeRtn)(Fn) )
762 #define ubi_trLeafNode( Nd ) \
763 ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
765 #define ubi_trModuleID( s, l ) ubi_btModuleID( s, l )
767 /* ========================================================================== */
768 #endif /* ubi_BinTree_H */