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.4 1998/06/03 17:42:46 crh
33 * Further fiddling with sys_include.h. It's now in ubi_BinTree.h which is
34 * included by all of the binary tree files.
36 * Reminder: Some of the ubi_tr* macros in ubi_BinTree.h are redefined in
37 * ubi_AVLtree.h and ubi_SplayTree.h. This allows easy swapping
38 * of tree types by simply changing a header. Unfortunately, the
39 * macro redefinitions in ubi_AVLtree.h and ubi_SplayTree.h will
40 * conflict if used together. You must either choose a single tree
41 * type, or use the underlying function calls directly. Compare
42 * the two header files for more information.
44 * Revision 4.3 1998/06/02 01:28:43 crh
45 * Changed ubi_null.h to sys_include.h to make it more generic.
47 * Revision 4.2 1998/05/20 04:32:36 crh
48 * The C file now includes ubi_null.h. See ubi_null.h for more info.
49 * Also, the balance and gender fields of the node were declared as
50 * signed char. As I understand it, at least one SunOS or Solaris
51 * compiler doesn't like "signed char". The declarations were
52 * wrong anyway, so I changed them to simple "char".
54 * Revision 4.1 1998/03/31 06:13:47 crh
55 * Thomas Aglassinger sent E'mail pointing out errors in the
56 * dereferencing of function pointers, and a missing typecast.
59 * Revision 4.0 1998/03/10 03:16:04 crh
60 * Added the AVL field 'balance' to the ubi_btNode structure. This means
61 * that all BinTree modules now use the same basic node structure, which
62 * greatly simplifies the AVL module.
63 * Decided that this was a big enough change to justify a new major revision
64 * number. 3.0 was an error, so we're at 4.0.
66 * Revision 2.6 1998/01/24 06:27:30 crh
67 * Added ubi_trCount() macro.
69 * Revision 2.5 1997/12/23 03:59:21 crh
70 * In this version, all constants & macros defined in the header file have
71 * the ubi_tr prefix. Also cleaned up anything that gcc complained about
72 * when run with '-pedantic -fsyntax-only -Wall'.
74 * Revision 2.4 1997/07/26 04:11:14 crh
75 * + Just to be annoying I changed ubi_TRUE and ubi_FALSE to ubi_trTRUE
77 * + There is now a type ubi_trBool to go with ubi_trTRUE and ubi_trFALSE.
78 * + There used to be something called "ubi_TypeDefs.h". I got rid of it.
79 * + Added function ubi_btLeafNode().
81 * Revision 2.3 1997/06/03 05:15:27 crh
82 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid conflicts.
83 * Also changed the interface to function InitTree(). See the comments
84 * for this function for more information.
86 * Revision 2.2 1995/10/03 22:00:40 CRH
89 * Revision 2.1 95/03/09 23:43:46 CRH
90 * Added the ModuleID static string and function. These modules are now
93 * Revision 2.0 95/02/27 22:00:33 CRH
94 * Revision 2.0 of this program includes the following changes:
96 * 1) A fix to a major typo in the RepaceNode() function.
97 * 2) The addition of the static function Border().
98 * 3) The addition of the public functions FirstOf() and LastOf(), which
99 * use Border(). These functions are used with trees that allow
101 * 4) A complete rewrite of the Locate() function. Locate() now accepts
102 * a "comparison" operator.
103 * 5) Overall enhancements to both code and comments.
105 * I decided to give this a new major rev number because the interface has
106 * changed. In particular, there are two new functions, and changes to the
109 * Revision 1.0 93/10/15 22:55:04 CRH
110 * With this revision, I have added a set of #define's that provide a single,
111 * standard API to all existing tree modules. Until now, each of the three
112 * existing modules had a different function and typedef prefix, as follows:
116 * ubi_AVLtree ubi_avl
117 * ubi_SplayTree ubi_spt
119 * To further complicate matters, only those portions of the base module
120 * (ubi_BinTree) that were superceeded in the new module had the new names.
121 * For example, if you were using ubi_SplayTree, the locate function was
122 * called "ubi_sptLocate", but the next and previous functions remained
123 * "ubi_btNext" and "ubi_btPrev".
125 * This was not too terrible if you were familiar with the modules and knew
126 * exactly which tree model you wanted to use. If you wanted to be able to
127 * change modules (for speed comparisons, etc), things could get messy very
130 * So, I have added a set of defined names that get redefined in any of the
131 * descendant modules. To use this standardized interface in your code,
132 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
133 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
134 * datatype names for the module that you are using. Just remember to
135 * include the header for that module in your program file. Because these
136 * names are handled by the preprocessor, there is no added run-time
139 * Note that the original names do still exist, and can be used if you wish
140 * to write code directly to a specific module. This should probably only be
141 * done if you are planning to implement a new descendant type, such as
142 * red/black trees. CRH
144 * V0.0 - June, 1991 - Written by Christopher R. Hertel (CRH).
146 * ========================================================================== **
149 #include "sys_include.h" /* Global include file, used to adapt the ubiqx
150 * modules to the host environment and the project
151 * with which the modules will be used. See
152 * sys_include.h for more info.
155 /* -------------------------------------------------------------------------- **
156 * Macros and constants.
159 * ubi_trTRUE - Boolean TRUE.
160 * ubi_trFALSE - Boolean FALSE.
162 * Flags used in the tree header:
163 * ubi_trOVERWRITE - This flag indicates that an existing node may be
164 * overwritten by a new node with a matching key.
165 * ubi_trDUPKEY - This flag indicates that the tree allows duplicate
166 * keys. If the tree does allow duplicates, the
167 * overwrite flag is ignored.
169 * Node link array index constants: (Each node has an array of three
170 * pointers. One to the left, one to the right, and one back to the
172 * ubi_trLEFT - Left child pointer.
173 * ubi_trPARENT - Parent pointer.
174 * ubi_trRIGHT - Right child pointer.
175 * ubi_trEQUAL - Synonym for PARENT.
177 * ubi_trCompOps: These values are used in the ubi_trLocate() function.
178 * ubi_trLT - request the first instance of the greatest key less than
180 * ubi_trLE - request the first instance of the greatest key that is less
181 * than or equal to the search key.
182 * ubi_trEQ - request the first instance of key that is equal to the
184 * ubi_trGE - request the first instance of a key that is greater than
185 * or equal to the search key.
186 * ubi_trGT - request the first instance of the first key that is greater
187 * than the search key.
188 * -------------------------------------------------------------------------- **
191 #define ubi_trTRUE 0xFF
192 #define ubi_trFALSE 0x00
194 #define ubi_trOVERWRITE 0x01 /* Turn on allow overwrite */
195 #define ubi_trDUPKEY 0x02 /* Turn on allow duplicate keys */
197 /* Pointer array index constants... */
198 #define ubi_trLEFT 0x00
199 #define ubi_trPARENT 0x01
200 #define ubi_trRIGHT 0x02
201 #define ubi_trEQUAL ubi_trPARENT
211 /* -------------------------------------------------------------------------- **
212 * These three macros allow simple manipulation of pointer index values (LEFT,
213 * RIGHT, and PARENT).
215 * Normalize() - converts {LEFT, PARENT, RIGHT} into {-1, 0 ,1}. C
216 * uses {negative, zero, positive} values to indicate
217 * {less than, equal to, greater than}.
218 * AbNormal() - converts {negative, zero, positive} to {LEFT, PARENT,
219 * RIGHT} (opposite of Normalize()). Note: C comparison
220 * functions, such as strcmp(), return {negative, zero,
221 * positive} values, which are not necessarily {-1, 0,
222 * 1}. This macro uses the the ubi_btSgn() function to
224 * RevWay() - converts LEFT to RIGHT and RIGHT to LEFT. PARENT (EQUAL)
226 * -------------------------------------------------------------------------- **
228 #define ubi_trNormalize(W) ((char)( (W) - ubi_trEQUAL ))
229 #define ubi_trAbNormal(W) ((char)( ((char)ubi_btSgn( (long)(W) )) \
231 #define ubi_trRevWay(W) ((char)( ubi_trEQUAL - ((W) - ubi_trEQUAL) ))
233 /* -------------------------------------------------------------------------- **
234 * These macros allow us to quickly read the values of the OVERWRITE and
235 * DUPlicate KEY bits of the tree root flags field.
236 * -------------------------------------------------------------------------- **
238 #define ubi_trDups_OK(A) \
239 ((ubi_trDUPKEY & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
240 #define ubi_trOvwt_OK(A) \
241 ((ubi_trOVERWRITE & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
243 /* -------------------------------------------------------------------------- **
244 * A quickie for consistency.
245 * ubi_trCount() - Given a pointer to a tree root, this macro returns the
246 * number of nodes currently in the tree.
248 * -------------------------------------------------------------------------- **
251 #define ubi_trCount( R ) (((ubi_trRootPtr)(R))->count)
253 /* -------------------------------------------------------------------------- **
256 * ubi_trBool - Your typcial true or false...
258 * Item Pointer: The ubi_btItemPtr is a generic pointer. It is used to
259 * indicate a key that is being searched for within the tree.
260 * Searching occurs whenever the ubi_trFind(), ubi_trLocate(),
261 * or ubi_trInsert() functions are called.
262 * -------------------------------------------------------------------------- **
265 typedef unsigned char ubi_trBool;
267 typedef void *ubi_btItemPtr; /* A pointer to key data within a node. */
269 /* ------------------------------------------------------------------------- **
270 * Binary Tree Node Structure: This structure defines the basic elements of
271 * the tree nodes. In general you *SHOULD NOT PLAY WITH THESE FIELDS*!
272 * But, of course, I have to put the structure into this header so that
273 * you can use it as a building block.
275 * The fields are as follows:
276 * Link - an array of pointers. These pointers are manipulated by
277 * the BT routines. The pointers indicate the left and right
278 * child nodes and the parent node. By keeping track of the
279 * parent pointer, we avoid the need for recursive routines or
280 * hand-tooled stacks to keep track of our path back to the
281 * root. The use of these pointers is subject to change without
283 * gender - a one-byte field indicating whether the node is the RIGHT or
284 * LEFT child of its parent. If the node is the root of the
285 * tree, gender will be PARENT.
286 * balance - only used by the AVL tree module. This field indicates
287 * the height balance at a given node. See ubi_AVLtree for
290 * ------------------------------------------------------------------------- **
292 typedef struct ubi_btNodeStruct {
293 struct ubi_btNodeStruct *Link[ 3 ];
298 typedef ubi_btNode *ubi_btNodePtr; /* Pointer to an ubi_btNode structure. */
300 /* ------------------------------------------------------------------------- **
301 * The next three typedefs define standard function types used by the binary
302 * tree management routines. In particular:
304 * ubi_btCompFunc is a pointer to a comparison function. Comparison
305 * functions are passed an ubi_btItemPtr and an
306 * ubi_btNodePtr. They return a value that is (<0), 0,
307 * or (>0) to indicate that the Item is (respectively)
308 * "less than", "equal to", or "greater than" the Item
309 * contained within the node. (See ubi_btInitTree()).
310 * ubi_btActionRtn is a pointer to a function that may be called for each
311 * node visited when performing a tree traversal (see
312 * ubi_btTraverse()). The function will be passed two
313 * parameters: the first is a pointer to a node in the
314 * tree, the second is a generic pointer that may point to
315 * anything that you like.
316 * ubi_btKillNodeRtn is a pointer to a function that will deallocate the
317 * memory used by a node (see ubi_btKillTree()). Since
318 * memory management is left up to you, deallocation may
319 * mean anything that you want it to mean. Just remember
320 * that the tree *will* be destroyed and that none of the
321 * node pointers will be valid any more.
322 * ------------------------------------------------------------------------- **
325 typedef int (*ubi_btCompFunc)( ubi_btItemPtr, ubi_btNodePtr );
327 typedef void (*ubi_btActionRtn)( ubi_btNodePtr, void * );
329 typedef void (*ubi_btKillNodeRtn)( ubi_btNodePtr );
331 /* -------------------------------------------------------------------------- **
332 * Tree Root Structure: This structure gives us a convenient handle for
333 * accessing whole binary trees. The fields are:
334 * root - A pointer to the root node of the tree.
335 * count - A count of the number of nodes stored in the tree.
336 * cmp - A pointer to the comparison routine to be used when building or
337 * searching the tree.
338 * flags - A set of bit flags. Two flags are currently defined:
340 * ubi_trOVERWRITE - If set, this flag indicates that a new node should
341 * (bit 0x01) overwrite an old node if the two have identical
342 * keys (ie., the keys are equal).
343 * ubi_trDUPKEY - If set, this flag indicates that the tree is
344 * (bit 0x02) allowed to contain nodes with duplicate keys.
346 * NOTE: ubi_trInsert() tests ubi_trDUPKEY before ubi_trOVERWRITE.
348 * All of these values are set when you initialize the root structure by
349 * calling ubi_trInitTree().
350 * -------------------------------------------------------------------------- **
354 ubi_btNodePtr root; /* A pointer to the root node of the tree */
355 ubi_btCompFunc cmp; /* A pointer to the tree's comparison function */
356 unsigned long count; /* A count of the number of nodes in the tree */
357 char flags; /* Overwrite Y|N, Duplicate keys Y|N... */
360 typedef ubi_btRoot *ubi_btRootPtr; /* Pointer to an ubi_btRoot structure. */
363 /* -------------------------------------------------------------------------- **
364 * Function Prototypes.
367 long ubi_btSgn( long x );
368 /* ------------------------------------------------------------------------ **
369 * Return the sign of x; {negative,zero,positive} ==> {-1, 0, 1}.
371 * Input: x - a signed long integer value.
373 * Output: the "sign" of x, represented as follows:
375 * 0 == zero (no sign)
378 * Note: This utility is provided in order to facilitate the conversion
379 * of C comparison function return values into BinTree direction
380 * values: {LEFT, PARENT, EQUAL}. It is INCORPORATED into the
381 * AbNormal() conversion macro!
383 * ------------------------------------------------------------------------ **
386 ubi_btNodePtr ubi_btInitNode( ubi_btNodePtr NodePtr );
387 /* ------------------------------------------------------------------------ **
388 * Initialize a tree node.
390 * Input: a pointer to a ubi_btNode structure to be initialized.
391 * Output: a pointer to the initialized ubi_btNode structure (ie. the
392 * same as the input pointer).
393 * ------------------------------------------------------------------------ **
396 ubi_btRootPtr ubi_btInitTree( ubi_btRootPtr RootPtr,
397 ubi_btCompFunc CompFunc,
399 /* ------------------------------------------------------------------------ **
400 * Initialize the fields of a Tree Root header structure.
402 * Input: RootPtr - a pointer to an ubi_btRoot structure to be
404 * CompFunc - a pointer to a comparison function that will be used
405 * whenever nodes in the tree must be compared against
407 * Flags - One bytes worth of flags. Flags include
408 * ubi_trOVERWRITE and ubi_trDUPKEY. See the header
409 * file for more info.
411 * Output: a pointer to the initialized ubi_btRoot structure (ie. the
412 * same value as RootPtr).
414 * Note: The interface to this function has changed from that of
415 * previous versions. The <Flags> parameter replaces two
416 * boolean parameters that had the same basic effect.
417 * ------------------------------------------------------------------------ **
420 ubi_trBool ubi_btInsert( ubi_btRootPtr RootPtr,
421 ubi_btNodePtr NewNode,
422 ubi_btItemPtr ItemPtr,
423 ubi_btNodePtr *OldNode );
424 /* ------------------------------------------------------------------------ **
425 * This function uses a non-recursive algorithm to add a new element to the
428 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
429 * the root of the tree to which NewNode is to be added.
430 * NewNode - a pointer to an ubi_btNode structure that is NOT
432 * ItemPtr - A pointer to the sort key that is stored within
433 * *NewNode. ItemPtr MUST point to information stored
434 * in *NewNode or an EXACT DUPLICATE. The key data
435 * indicated by ItemPtr is used to place the new node
437 * OldNode - a pointer to an ubi_btNodePtr. When searching
438 * the tree, a duplicate node may be found. If
439 * duplicates are allowed, then the new node will
440 * be simply placed into the tree. If duplicates
441 * are not allowed, however, then one of two things
443 * 1) if overwritting *is not* allowed, this
444 * function will return FALSE (indicating that
445 * the new node could not be inserted), and
446 * *OldNode will point to the duplicate that is
448 * 2) if overwritting *is* allowed, then this
449 * function will swap **OldNode for *NewNode.
450 * In this case, *OldNode will point to the node
451 * that was removed (thus allowing you to free
453 * ** If you are using overwrite mode, ALWAYS **
454 * ** check the return value of this parameter! **
455 * Note: You may pass NULL in this parameter, the
456 * function knows how to cope. If you do this,
457 * however, there will be no way to return a
458 * pointer to an old (ie. replaced) node (which is
459 * a problem if you are using overwrite mode).
461 * Output: a boolean value indicating success or failure. The function
462 * will return FALSE if the node could not be added to the tree.
463 * Such failure will only occur if duplicates are not allowed,
464 * nodes cannot be overwritten, AND a duplicate key was found
466 * ------------------------------------------------------------------------ **
469 ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr,
470 ubi_btNodePtr DeadNode );
471 /* ------------------------------------------------------------------------ **
472 * This function removes the indicated node from the tree.
474 * Input: RootPtr - A pointer to the header of the tree that contains
475 * the node to be removed.
476 * DeadNode - A pointer to the node that will be removed.
478 * Output: This function returns a pointer to the node that was removed
479 * from the tree (ie. the same as DeadNode).
481 * Note: The node MUST be in the tree indicated by RootPtr. If not,
482 * strange and evil things will happen to your trees.
483 * ------------------------------------------------------------------------ **
486 ubi_btNodePtr ubi_btLocate( ubi_btRootPtr RootPtr,
487 ubi_btItemPtr FindMe,
488 ubi_trCompOps CompOp );
489 /* ------------------------------------------------------------------------ **
490 * The purpose of ubi_btLocate() is to find a node or set of nodes given
491 * a target value and a "comparison operator". The Locate() function is
492 * more flexible and (in the case of trees that may contain dupicate keys)
493 * more precise than the ubi_btFind() function. The latter is faster,
494 * but it only searches for exact matches and, if the tree contains
495 * duplicates, Find() may return a pointer to any one of the duplicate-
499 * RootPtr - A pointer to the header of the tree to be searched.
500 * FindMe - An ubi_btItemPtr that indicates the key for which to
502 * CompOp - One of the following:
503 * CompOp Return a pointer to the node with
504 * ------ ---------------------------------
505 * ubi_trLT - the last key value that is less
507 * ubi_trLE - the first key matching FindMe, or
508 * the last key that is less than
510 * ubi_trEQ - the first key matching FindMe.
511 * ubi_trGE - the first key matching FindMe, or the
512 * first key greater than FindMe.
513 * ubi_trGT - the first key greater than FindMe.
515 * A pointer to the node matching the criteria listed above under
516 * CompOp, or NULL if no node matched the criteria.
519 * In the case of trees with duplicate keys, Locate() will behave as
523 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6
527 * That is, when returning a pointer to a node with a key that is LESS
528 * THAN the target key (FindMe), Locate() will return a pointer to the
529 * LAST matching node.
530 * When returning a pointer to a node with a key that is GREATER
531 * THAN the target key (FindMe), Locate() will return a pointer to the
532 * FIRST matching node.
534 * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
535 * ------------------------------------------------------------------------ **
538 ubi_btNodePtr ubi_btFind( ubi_btRootPtr RootPtr,
539 ubi_btItemPtr FindMe );
540 /* ------------------------------------------------------------------------ **
541 * This function performs a non-recursive search of a tree for any node
542 * matching a specific key.
545 * RootPtr - a pointer to the header of the tree to be searched.
546 * FindMe - a pointer to the key value for which to search.
549 * A pointer to a node with a key that matches the key indicated by
550 * FindMe, or NULL if no such node was found.
552 * Note: In a tree that allows duplicates, the pointer returned *might
553 * not* point to the (sequentially) first occurance of the
554 * desired key. In such a tree, it may be more useful to use
556 * ------------------------------------------------------------------------ **
559 ubi_btNodePtr ubi_btNext( ubi_btNodePtr P );
560 /* ------------------------------------------------------------------------ **
561 * Given the node indicated by P, find the (sorted order) Next node in the
563 * Input: P - a pointer to a node that exists in a binary tree.
564 * Output: A pointer to the "next" node in the tree, or NULL if P pointed
565 * to the "last" node in the tree or was NULL.
566 * ------------------------------------------------------------------------ **
569 ubi_btNodePtr ubi_btPrev( ubi_btNodePtr P );
570 /* ------------------------------------------------------------------------ **
571 * Given the node indicated by P, find the (sorted order) Previous node in
573 * Input: P - a pointer to a node that exists in a binary tree.
574 * Output: A pointer to the "previous" node in the tree, or NULL if P
575 * pointed to the "first" node in the tree or was NULL.
576 * ------------------------------------------------------------------------ **
579 ubi_btNodePtr ubi_btFirst( ubi_btNodePtr P );
580 /* ------------------------------------------------------------------------ **
581 * Given the node indicated by P, find the (sorted order) First node in the
582 * subtree of which *P is the root.
583 * Input: P - a pointer to a node that exists in a binary tree.
584 * Output: A pointer to the "first" node in a subtree that has *P as its
585 * root. This function will return NULL only if P is NULL.
586 * Note: In general, you will be passing in the value of the root field
587 * of an ubi_btRoot structure.
588 * ------------------------------------------------------------------------ **
591 ubi_btNodePtr ubi_btLast( ubi_btNodePtr P );
592 /* ------------------------------------------------------------------------ **
593 * Given the node indicated by P, find the (sorted order) Last node in the
594 * subtree of which *P is the root.
595 * Input: P - a pointer to a node that exists in a binary tree.
596 * Output: A pointer to the "last" node in a subtree that has *P as its
597 * root. This function will return NULL only if P is NULL.
598 * Note: In general, you will be passing in the value of the root field
599 * of an ubi_btRoot structure.
600 * ------------------------------------------------------------------------ **
603 ubi_btNodePtr ubi_btFirstOf( ubi_btRootPtr RootPtr,
604 ubi_btItemPtr MatchMe,
606 /* ------------------------------------------------------------------------ **
607 * Given a tree that a allows duplicate keys, and a pointer to a node in
608 * the tree, this function will return a pointer to the first (traversal
609 * order) node with the same key value.
611 * Input: RootPtr - A pointer to the root of the tree.
612 * MatchMe - A pointer to the key value. This should probably
613 * point to the key within node *p.
614 * p - A pointer to a node in the tree.
615 * Output: A pointer to the first node in the set of nodes with keys
617 * Notes: Node *p MUST be in the set of nodes with keys matching
618 * <FindMe>. If not, this function will return NULL.
619 * ------------------------------------------------------------------------ **
622 ubi_btNodePtr ubi_btLastOf( ubi_btRootPtr RootPtr,
623 ubi_btItemPtr MatchMe,
625 /* ------------------------------------------------------------------------ **
626 * Given a tree that a allows duplicate keys, and a pointer to a node in
627 * the tree, this function will return a pointer to the last (traversal
628 * order) node with the same key value.
630 * Input: RootPtr - A pointer to the root of the tree.
631 * MatchMe - A pointer to the key value. This should probably
632 * point to the key within node *p.
633 * p - A pointer to a node in the tree.
634 * Output: A pointer to the last node in the set of nodes with keys
636 * Notes: Node *p MUST be in the set of nodes with keys matching
637 * <FindMe>. If not, this function will return NULL.
638 * ------------------------------------------------------------------------ **
641 ubi_trBool ubi_btTraverse( ubi_btRootPtr RootPtr,
642 ubi_btActionRtn EachNode,
644 /* ------------------------------------------------------------------------ **
645 * Traverse a tree in sorted order (non-recursively). At each node, call
646 * (*EachNode)(), passing a pointer to the current node, and UserData as the
648 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
649 * the tree to be traversed.
650 * EachNode - a pointer to a function to be called at each node
651 * as the node is visited.
652 * UserData - a generic pointer that may point to anything that
654 * Output: A boolean value. FALSE if the tree is empty, otherwise TRUE.
655 * ------------------------------------------------------------------------ **
658 ubi_trBool ubi_btKillTree( ubi_btRootPtr RootPtr,
659 ubi_btKillNodeRtn FreeNode );
660 /* ------------------------------------------------------------------------ **
661 * Delete an entire tree (non-recursively) and reinitialize the ubi_btRoot
662 * structure. Note that this function will return FALSE if either parameter
665 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
666 * the root of the tree to delete.
667 * FreeNode - a function that will be called for each node in the
668 * tree to deallocate the memory used by the node.
670 * Output: A boolean value. FALSE if either input parameter was NULL, else
673 * ------------------------------------------------------------------------ **
676 ubi_btNodePtr ubi_btLeafNode( ubi_btNodePtr leader );
677 /* ------------------------------------------------------------------------ **
678 * Returns a pointer to a leaf node.
680 * Input: leader - Pointer to a node at which to start the descent.
682 * Output: A pointer to a leaf node selected in a somewhat arbitrary
685 * Notes: I wrote this function because I was using splay trees as a
686 * database cache. The cache had a maximum size on it, and I
687 * needed a way of choosing a node to sacrifice if the cache
688 * became full. In a splay tree, less recently accessed nodes
689 * tend toward the bottom of the tree, meaning that leaf nodes
690 * are good candidates for removal. (I really can't think of
691 * any other reason to use this function.)
692 * + In a simple binary tree or an AVL tree, the most recently
693 * added nodes tend to be nearer the bottom, making this a *bad*
694 * way to choose which node to remove from the cache.
695 * + Randomizing the traversal order is probably a good idea. You
696 * can improve the randomization of leaf node selection by passing
697 * in pointers to nodes other than the root node each time. A
698 * pointer to any node in the tree will do. Of course, if you
699 * pass a pointer to a leaf node you'll get the same thing back.
701 * ------------------------------------------------------------------------ **
705 int ubi_btModuleID( int size, char *list[] );
706 /* ------------------------------------------------------------------------ **
707 * Returns a set of strings that identify the module.
709 * Input: size - The number of elements in the array <list>.
710 * list - An array of pointers of type (char *). This array
711 * should, initially, be empty. This function will fill
712 * in the array with pointers to strings.
713 * Output: The number of elements of <list> that were used. If this value
714 * is less than <size>, the values of the remaining elements are
717 * Notes: Please keep in mind that the pointers returned indicate strings
718 * stored in static memory. Don't free() them, don't write over
719 * them, etc. Just read them.
720 * ------------------------------------------------------------------------ **
723 /* -------------------------------------------------------------------------- **
726 * This set of defines allows you to write programs that will use any of the
727 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
728 * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
729 * including the appropriate module header.
732 #define ubi_trItemPtr ubi_btItemPtr
734 #define ubi_trNode ubi_btNode
735 #define ubi_trNodePtr ubi_btNodePtr
737 #define ubi_trRoot ubi_btRoot
738 #define ubi_trRootPtr ubi_btRootPtr
740 #define ubi_trCompFunc ubi_btCompFunc
741 #define ubi_trActionRtn ubi_btActionRtn
742 #define ubi_trKillNodeRtn ubi_btKillNodeRtn
744 #define ubi_trSgn( x ) ubi_btSgn( x )
746 #define ubi_trInitNode( Np ) ubi_btInitNode( (ubi_btNodePtr)(Np) )
748 #define ubi_trInitTree( Rp, Cf, Fl ) \
749 ubi_btInitTree( (ubi_btRootPtr)(Rp), (ubi_btCompFunc)(Cf), (Fl) )
751 #define ubi_trInsert( Rp, Nn, Ip, On ) \
752 ubi_btInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
753 (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
755 #define ubi_trRemove( Rp, Dn ) \
756 ubi_btRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
758 #define ubi_trLocate( Rp, Ip, Op ) \
759 ubi_btLocate( (ubi_btRootPtr)(Rp), \
760 (ubi_btItemPtr)(Ip), \
761 (ubi_trCompOps)(Op) )
763 #define ubi_trFind( Rp, Ip ) \
764 ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
766 #define ubi_trNext( P ) ubi_btNext( (ubi_btNodePtr)(P) )
768 #define ubi_trPrev( P ) ubi_btPrev( (ubi_btNodePtr)(P) )
770 #define ubi_trFirst( P ) ubi_btFirst( (ubi_btNodePtr)(P) )
772 #define ubi_trLast( P ) ubi_btLast( (ubi_btNodePtr)(P) )
774 #define ubi_trFirstOf( Rp, Ip, P ) \
775 ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
776 (ubi_btItemPtr)(Ip), \
779 #define ubi_trLastOf( Rp, Ip, P ) \
780 ubi_btLastOf( (ubi_btRootPtr)(Rp), \
781 (ubi_btItemPtr)(Ip), \
784 #define ubi_trTraverse( Rp, En, Ud ) \
785 ubi_btTraverse((ubi_btRootPtr)(Rp), (ubi_btActionRtn)(En), (void *)(Ud))
787 #define ubi_trKillTree( Rp, Fn ) \
788 ubi_btKillTree( (ubi_btRootPtr)(Rp), (ubi_btKillNodeRtn)(Fn) )
790 #define ubi_trLeafNode( Nd ) \
791 ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
793 #define ubi_trModuleID( s, l ) ubi_btModuleID( s, l )
795 /* ========================================================================== */
796 #endif /* UBI_BINTREE_H */