dnscrypto-proxy: Support files updated.
[tomato.git] / release / src / router / samba / source / ubiqx / ubi_BinTree.h
blob53758246572ce5cbc28891af0dec9b18302e2c28
1 #ifndef UBI_BINTREE_H
2 #define UBI_BINTREE_H
3 /* ========================================================================== **
4 * ubi_BinTree.h
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.7 1998/10/21 06:15:07 crh
33 * Fixed bugs in FirstOf() and LastOf() reported by Massimo Campostrini.
34 * See function comments.
36 * Revision 4.6 1998/07/25 17:02:10 crh
37 * Added the ubi_trNewTree() macro.
39 * Revision 4.5 1998/06/04 21:29:27 crh
40 * Upper-cased defined constants (eg UBI_BINTREE_H) in some header files.
41 * This is more "standard", and is what people expect. Weird, eh?
43 * Revision 4.4 1998/06/03 17:42:46 crh
44 * Further fiddling with sys_include.h. It's now in ubi_BinTree.h which is
45 * included by all of the binary tree files.
47 * Reminder: Some of the ubi_tr* macros in ubi_BinTree.h are redefined in
48 * ubi_AVLtree.h and ubi_SplayTree.h. This allows easy swapping
49 * of tree types by simply changing a header. Unfortunately, the
50 * macro redefinitions in ubi_AVLtree.h and ubi_SplayTree.h will
51 * conflict if used together. You must either choose a single tree
52 * type, or use the underlying function calls directly. Compare
53 * the two header files for more information.
55 * Revision 4.3 1998/06/02 01:28:43 crh
56 * Changed ubi_null.h to sys_include.h to make it more generic.
58 * Revision 4.2 1998/05/20 04:32:36 crh
59 * The C file now includes ubi_null.h. See ubi_null.h for more info.
60 * Also, the balance and gender fields of the node were declared as
61 * signed char. As I understand it, at least one SunOS or Solaris
62 * compiler doesn't like "signed char". The declarations were
63 * wrong anyway, so I changed them to simple "char".
65 * Revision 4.1 1998/03/31 06:13:47 crh
66 * Thomas Aglassinger sent E'mail pointing out errors in the
67 * dereferencing of function pointers, and a missing typecast.
68 * Thanks, Thomas!
70 * Revision 4.0 1998/03/10 03:16:04 crh
71 * Added the AVL field 'balance' to the ubi_btNode structure. This means
72 * that all BinTree modules now use the same basic node structure, which
73 * greatly simplifies the AVL module.
74 * Decided that this was a big enough change to justify a new major revision
75 * number. 3.0 was an error, so we're at 4.0.
77 * Revision 2.6 1998/01/24 06:27:30 crh
78 * Added ubi_trCount() macro.
80 * Revision 2.5 1997/12/23 03:59:21 crh
81 * In this version, all constants & macros defined in the header file have
82 * the ubi_tr prefix. Also cleaned up anything that gcc complained about
83 * when run with '-pedantic -fsyntax-only -Wall'.
85 * Revision 2.4 1997/07/26 04:11:14 crh
86 * + Just to be annoying I changed ubi_TRUE and ubi_FALSE to ubi_trTRUE
87 * and ubi_trFALSE.
88 * + There is now a type ubi_trBool to go with ubi_trTRUE and ubi_trFALSE.
89 * + There used to be something called "ubi_TypeDefs.h". I got rid of it.
90 * + Added function ubi_btLeafNode().
92 * Revision 2.3 1997/06/03 05:15:27 crh
93 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid conflicts.
94 * Also changed the interface to function InitTree(). See the comments
95 * for this function for more information.
97 * Revision 2.2 1995/10/03 22:00:40 CRH
98 * Ubisized!
100 * Revision 2.1 95/03/09 23:43:46 CRH
101 * Added the ModuleID static string and function. These modules are now
102 * self-identifying.
104 * Revision 2.0 95/02/27 22:00:33 CRH
105 * Revision 2.0 of this program includes the following changes:
107 * 1) A fix to a major typo in the RepaceNode() function.
108 * 2) The addition of the static function Border().
109 * 3) The addition of the public functions FirstOf() and LastOf(), which
110 * use Border(). These functions are used with trees that allow
111 * duplicate keys.
112 * 4) A complete rewrite of the Locate() function. Locate() now accepts
113 * a "comparison" operator.
114 * 5) Overall enhancements to both code and comments.
116 * I decided to give this a new major rev number because the interface has
117 * changed. In particular, there are two new functions, and changes to the
118 * Locate() function.
120 * Revision 1.0 93/10/15 22:55:04 CRH
121 * With this revision, I have added a set of #define's that provide a single,
122 * standard API to all existing tree modules. Until now, each of the three
123 * existing modules had a different function and typedef prefix, as follows:
125 * Module Prefix
126 * ubi_BinTree ubi_bt
127 * ubi_AVLtree ubi_avl
128 * ubi_SplayTree ubi_spt
130 * To further complicate matters, only those portions of the base module
131 * (ubi_BinTree) that were superceeded in the new module had the new names.
132 * For example, if you were using ubi_SplayTree, the locate function was
133 * called "ubi_sptLocate", but the next and previous functions remained
134 * "ubi_btNext" and "ubi_btPrev".
136 * This was not too terrible if you were familiar with the modules and knew
137 * exactly which tree model you wanted to use. If you wanted to be able to
138 * change modules (for speed comparisons, etc), things could get messy very
139 * quickly.
141 * So, I have added a set of defined names that get redefined in any of the
142 * descendant modules. To use this standardized interface in your code,
143 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
144 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or
145 * datatype names for the module that you are using. Just remember to
146 * include the header for that module in your program file. Because these
147 * names are handled by the preprocessor, there is no added run-time
148 * overhead.
150 * Note that the original names do still exist, and can be used if you wish
151 * to write code directly to a specific module. This should probably only be
152 * done if you are planning to implement a new descendant type, such as
153 * red/black trees. CRH
155 * V0.0 - June, 1991 - Written by Christopher R. Hertel (CRH).
157 * ========================================================================== **
160 #include "sys_include.h" /* Global include file, used to adapt the ubiqx
161 * modules to the host environment and the project
162 * with which the modules will be used. See
163 * sys_include.h for more info.
166 /* -------------------------------------------------------------------------- **
167 * Macros and constants.
169 * General purpose:
170 * ubi_trTRUE - Boolean TRUE.
171 * ubi_trFALSE - Boolean FALSE.
173 * Flags used in the tree header:
174 * ubi_trOVERWRITE - This flag indicates that an existing node may be
175 * overwritten by a new node with a matching key.
176 * ubi_trDUPKEY - This flag indicates that the tree allows duplicate
177 * keys. If the tree does allow duplicates, the
178 * overwrite flag is ignored.
180 * Node link array index constants: (Each node has an array of three
181 * pointers. One to the left, one to the right, and one back to the
182 * parent.)
183 * ubi_trLEFT - Left child pointer.
184 * ubi_trPARENT - Parent pointer.
185 * ubi_trRIGHT - Right child pointer.
186 * ubi_trEQUAL - Synonym for PARENT.
188 * ubi_trCompOps: These values are used in the ubi_trLocate() function.
189 * ubi_trLT - request the first instance of the greatest key less than
190 * the search key.
191 * ubi_trLE - request the first instance of the greatest key that is less
192 * than or equal to the search key.
193 * ubi_trEQ - request the first instance of key that is equal to the
194 * search key.
195 * ubi_trGE - request the first instance of a key that is greater than
196 * or equal to the search key.
197 * ubi_trGT - request the first instance of the first key that is greater
198 * than the search key.
199 * -------------------------------------------------------------------------- **
202 #define ubi_trTRUE 0xFF
203 #define ubi_trFALSE 0x00
205 #define ubi_trOVERWRITE 0x01 /* Turn on allow overwrite */
206 #define ubi_trDUPKEY 0x02 /* Turn on allow duplicate keys */
208 /* Pointer array index constants... */
209 #define ubi_trLEFT 0x00
210 #define ubi_trPARENT 0x01
211 #define ubi_trRIGHT 0x02
212 #define ubi_trEQUAL ubi_trPARENT
214 typedef enum {
215 ubi_trLT = 1,
216 ubi_trLE,
217 ubi_trEQ,
218 ubi_trGE,
219 ubi_trGT
220 } ubi_trCompOps;
222 /* -------------------------------------------------------------------------- **
223 * These three macros allow simple manipulation of pointer index values (LEFT,
224 * RIGHT, and PARENT).
226 * Normalize() - converts {LEFT, PARENT, RIGHT} into {-1, 0 ,1}. C
227 * uses {negative, zero, positive} values to indicate
228 * {less than, equal to, greater than}.
229 * AbNormal() - converts {negative, zero, positive} to {LEFT, PARENT,
230 * RIGHT} (opposite of Normalize()). Note: C comparison
231 * functions, such as strcmp(), return {negative, zero,
232 * positive} values, which are not necessarily {-1, 0,
233 * 1}. This macro uses the the ubi_btSgn() function to
234 * compensate.
235 * RevWay() - converts LEFT to RIGHT and RIGHT to LEFT. PARENT (EQUAL)
236 * is left as is.
237 * -------------------------------------------------------------------------- **
239 #define ubi_trNormalize(W) ((char)( (W) - ubi_trEQUAL ))
240 #define ubi_trAbNormal(W) ((char)( ((char)ubi_btSgn( (long)(W) )) \
241 + ubi_trEQUAL ))
242 #define ubi_trRevWay(W) ((char)( ubi_trEQUAL - ((W) - ubi_trEQUAL) ))
244 /* -------------------------------------------------------------------------- **
245 * These macros allow us to quickly read the values of the OVERWRITE and
246 * DUPlicate KEY bits of the tree root flags field.
247 * -------------------------------------------------------------------------- **
249 #define ubi_trDups_OK(A) \
250 ((ubi_trDUPKEY & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
251 #define ubi_trOvwt_OK(A) \
252 ((ubi_trOVERWRITE & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
254 /* -------------------------------------------------------------------------- **
255 * Additional Macros...
257 * ubi_trCount() - Given a pointer to a tree root, this macro returns the
258 * number of nodes currently in the tree.
260 * ubi_trNewTree() - This macro makes it easy to declare and initialize a
261 * tree header in one step. The line
263 * static ubi_trNewTree( MyTree, cmpfn, ubi_trDUPKEY );
265 * is equivalent to
267 * static ubi_trRoot MyTree[1]
268 * = {{ NULL, cmpfn, 0, ubi_trDUPKEY }};
270 * -------------------------------------------------------------------------- **
273 #define ubi_trCount( R ) (((ubi_trRootPtr)(R))->count)
275 #define ubi_trNewTree( N, C, F ) ubi_trRoot (N)[1] = {{ NULL, (C), 0, (F) }}
277 /* -------------------------------------------------------------------------- **
278 * Typedefs...
280 * ubi_trBool - Your typcial true or false...
282 * Item Pointer: The ubi_btItemPtr is a generic pointer. It is used to
283 * indicate a key that is being searched for within the tree.
284 * Searching occurs whenever the ubi_trFind(), ubi_trLocate(),
285 * or ubi_trInsert() functions are called.
286 * -------------------------------------------------------------------------- **
289 typedef unsigned char ubi_trBool;
291 typedef void *ubi_btItemPtr; /* A pointer to key data within a node. */
293 /* ------------------------------------------------------------------------- **
294 * Binary Tree Node Structure: This structure defines the basic elements of
295 * the tree nodes. In general you *SHOULD NOT PLAY WITH THESE FIELDS*!
296 * But, of course, I have to put the structure into this header so that
297 * you can use it as a building block.
299 * The fields are as follows:
300 * Link - an array of pointers. These pointers are manipulated by
301 * the BT routines. The pointers indicate the left and right
302 * child nodes and the parent node. By keeping track of the
303 * parent pointer, we avoid the need for recursive routines or
304 * hand-tooled stacks to keep track of our path back to the
305 * root. The use of these pointers is subject to change without
306 * notice.
307 * gender - a one-byte field indicating whether the node is the RIGHT or
308 * LEFT child of its parent. If the node is the root of the
309 * tree, gender will be PARENT.
310 * balance - only used by the AVL tree module. This field indicates
311 * the height balance at a given node. See ubi_AVLtree for
312 * details.
314 * ------------------------------------------------------------------------- **
316 typedef struct ubi_btNodeStruct {
317 struct ubi_btNodeStruct *Link[ 3 ];
318 char gender;
319 char balance;
320 } ubi_btNode;
322 typedef ubi_btNode *ubi_btNodePtr; /* Pointer to an ubi_btNode structure. */
324 /* ------------------------------------------------------------------------- **
325 * The next three typedefs define standard function types used by the binary
326 * tree management routines. In particular:
328 * ubi_btCompFunc is a pointer to a comparison function. Comparison
329 * functions are passed an ubi_btItemPtr and an
330 * ubi_btNodePtr. They return a value that is (<0), 0,
331 * or (>0) to indicate that the Item is (respectively)
332 * "less than", "equal to", or "greater than" the Item
333 * contained within the node. (See ubi_btInitTree()).
334 * ubi_btActionRtn is a pointer to a function that may be called for each
335 * node visited when performing a tree traversal (see
336 * ubi_btTraverse()). The function will be passed two
337 * parameters: the first is a pointer to a node in the
338 * tree, the second is a generic pointer that may point to
339 * anything that you like.
340 * ubi_btKillNodeRtn is a pointer to a function that will deallocate the
341 * memory used by a node (see ubi_btKillTree()). Since
342 * memory management is left up to you, deallocation may
343 * mean anything that you want it to mean. Just remember
344 * that the tree *will* be destroyed and that none of the
345 * node pointers will be valid any more.
346 * ------------------------------------------------------------------------- **
349 typedef int (*ubi_btCompFunc)( ubi_btItemPtr, ubi_btNodePtr );
351 typedef void (*ubi_btActionRtn)( ubi_btNodePtr, void * );
353 typedef void (*ubi_btKillNodeRtn)( ubi_btNodePtr );
355 /* -------------------------------------------------------------------------- **
356 * Tree Root Structure: This structure gives us a convenient handle for
357 * accessing whole binary trees. The fields are:
358 * root - A pointer to the root node of the tree.
359 * count - A count of the number of nodes stored in the tree.
360 * cmp - A pointer to the comparison routine to be used when building or
361 * searching the tree.
362 * flags - A set of bit flags. Two flags are currently defined:
364 * ubi_trOVERWRITE - If set, this flag indicates that a new node should
365 * (bit 0x01) overwrite an old node if the two have identical
366 * keys (ie., the keys are equal).
367 * ubi_trDUPKEY - If set, this flag indicates that the tree is
368 * (bit 0x02) allowed to contain nodes with duplicate keys.
370 * NOTE: ubi_trInsert() tests ubi_trDUPKEY before ubi_trOVERWRITE.
372 * All of these values are set when you initialize the root structure by
373 * calling ubi_trInitTree().
374 * -------------------------------------------------------------------------- **
377 typedef struct {
378 ubi_btNodePtr root; /* A pointer to the root node of the tree */
379 ubi_btCompFunc cmp; /* A pointer to the tree's comparison function */
380 unsigned long count; /* A count of the number of nodes in the tree */
381 char flags; /* Overwrite Y|N, Duplicate keys Y|N... */
382 } ubi_btRoot;
384 typedef ubi_btRoot *ubi_btRootPtr; /* Pointer to an ubi_btRoot structure. */
387 /* -------------------------------------------------------------------------- **
388 * Function Prototypes.
391 long ubi_btSgn( long x );
392 /* ------------------------------------------------------------------------ **
393 * Return the sign of x; {negative,zero,positive} ==> {-1, 0, 1}.
395 * Input: x - a signed long integer value.
397 * Output: the "sign" of x, represented as follows:
398 * -1 == negative
399 * 0 == zero (no sign)
400 * 1 == positive
402 * Note: This utility is provided in order to facilitate the conversion
403 * of C comparison function return values into BinTree direction
404 * values: {LEFT, PARENT, EQUAL}. It is INCORPORATED into the
405 * AbNormal() conversion macro!
407 * ------------------------------------------------------------------------ **
410 ubi_btNodePtr ubi_btInitNode( ubi_btNodePtr NodePtr );
411 /* ------------------------------------------------------------------------ **
412 * Initialize a tree node.
414 * Input: a pointer to a ubi_btNode structure to be initialized.
415 * Output: a pointer to the initialized ubi_btNode structure (ie. the
416 * same as the input pointer).
417 * ------------------------------------------------------------------------ **
420 ubi_btRootPtr ubi_btInitTree( ubi_btRootPtr RootPtr,
421 ubi_btCompFunc CompFunc,
422 char Flags );
423 /* ------------------------------------------------------------------------ **
424 * Initialize the fields of a Tree Root header structure.
426 * Input: RootPtr - a pointer to an ubi_btRoot structure to be
427 * initialized.
428 * CompFunc - a pointer to a comparison function that will be used
429 * whenever nodes in the tree must be compared against
430 * outside values.
431 * Flags - One bytes worth of flags. Flags include
432 * ubi_trOVERWRITE and ubi_trDUPKEY. See the header
433 * file for more info.
435 * Output: a pointer to the initialized ubi_btRoot structure (ie. the
436 * same value as RootPtr).
438 * Note: The interface to this function has changed from that of
439 * previous versions. The <Flags> parameter replaces two
440 * boolean parameters that had the same basic effect.
441 * ------------------------------------------------------------------------ **
444 ubi_trBool ubi_btInsert( ubi_btRootPtr RootPtr,
445 ubi_btNodePtr NewNode,
446 ubi_btItemPtr ItemPtr,
447 ubi_btNodePtr *OldNode );
448 /* ------------------------------------------------------------------------ **
449 * This function uses a non-recursive algorithm to add a new element to the
450 * tree.
452 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates
453 * the root of the tree to which NewNode is to be added.
454 * NewNode - a pointer to an ubi_btNode structure that is NOT
455 * part of any tree.
456 * ItemPtr - A pointer to the sort key that is stored within
457 * *NewNode. ItemPtr MUST point to information stored
458 * in *NewNode or an EXACT DUPLICATE. The key data
459 * indicated by ItemPtr is used to place the new node
460 * into the tree.
461 * OldNode - a pointer to an ubi_btNodePtr. When searching
462 * the tree, a duplicate node may be found. If
463 * duplicates are allowed, then the new node will
464 * be simply placed into the tree. If duplicates
465 * are not allowed, however, then one of two things
466 * may happen.
467 * 1) if overwritting *is not* allowed, this
468 * function will return FALSE (indicating that
469 * the new node could not be inserted), and
470 * *OldNode will point to the duplicate that is
471 * still in the tree.
472 * 2) if overwritting *is* allowed, then this
473 * function will swap **OldNode for *NewNode.
474 * In this case, *OldNode will point to the node
475 * that was removed (thus allowing you to free
476 * the node).
477 * ** If you are using overwrite mode, ALWAYS **
478 * ** check the return value of this parameter! **
479 * Note: You may pass NULL in this parameter, the
480 * function knows how to cope. If you do this,
481 * however, there will be no way to return a
482 * pointer to an old (ie. replaced) node (which is
483 * a problem if you are using overwrite mode).
485 * Output: a boolean value indicating success or failure. The function
486 * will return FALSE if the node could not be added to the tree.
487 * Such failure will only occur if duplicates are not allowed,
488 * nodes cannot be overwritten, AND a duplicate key was found
489 * within the tree.
490 * ------------------------------------------------------------------------ **
493 ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr,
494 ubi_btNodePtr DeadNode );
495 /* ------------------------------------------------------------------------ **
496 * This function removes the indicated node from the tree.
498 * Input: RootPtr - A pointer to the header of the tree that contains
499 * the node to be removed.
500 * DeadNode - A pointer to the node that will be removed.
502 * Output: This function returns a pointer to the node that was removed
503 * from the tree (ie. the same as DeadNode).
505 * Note: The node MUST be in the tree indicated by RootPtr. If not,
506 * strange and evil things will happen to your trees.
507 * ------------------------------------------------------------------------ **
510 ubi_btNodePtr ubi_btLocate( ubi_btRootPtr RootPtr,
511 ubi_btItemPtr FindMe,
512 ubi_trCompOps CompOp );
513 /* ------------------------------------------------------------------------ **
514 * The purpose of ubi_btLocate() is to find a node or set of nodes given
515 * a target value and a "comparison operator". The Locate() function is
516 * more flexible and (in the case of trees that may contain dupicate keys)
517 * more precise than the ubi_btFind() function. The latter is faster,
518 * but it only searches for exact matches and, if the tree contains
519 * duplicates, Find() may return a pointer to any one of the duplicate-
520 * keyed records.
522 * Input:
523 * RootPtr - A pointer to the header of the tree to be searched.
524 * FindMe - An ubi_btItemPtr that indicates the key for which to
525 * search.
526 * CompOp - One of the following:
527 * CompOp Return a pointer to the node with
528 * ------ ---------------------------------
529 * ubi_trLT - the last key value that is less
530 * than FindMe.
531 * ubi_trLE - the first key matching FindMe, or
532 * the last key that is less than
533 * FindMe.
534 * ubi_trEQ - the first key matching FindMe.
535 * ubi_trGE - the first key matching FindMe, or the
536 * first key greater than FindMe.
537 * ubi_trGT - the first key greater than FindMe.
538 * Output:
539 * A pointer to the node matching the criteria listed above under
540 * CompOp, or NULL if no node matched the criteria.
542 * Notes:
543 * In the case of trees with duplicate keys, Locate() will behave as
544 * follows:
546 * Find: 3 Find: 3
547 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6
548 * ^ ^ ^ ^ ^
549 * LT EQ GT LE GE
551 * That is, when returning a pointer to a node with a key that is LESS
552 * THAN the target key (FindMe), Locate() will return a pointer to the
553 * LAST matching node.
554 * When returning a pointer to a node with a key that is GREATER
555 * THAN the target key (FindMe), Locate() will return a pointer to the
556 * FIRST matching node.
558 * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
559 * ------------------------------------------------------------------------ **
562 ubi_btNodePtr ubi_btFind( ubi_btRootPtr RootPtr,
563 ubi_btItemPtr FindMe );
564 /* ------------------------------------------------------------------------ **
565 * This function performs a non-recursive search of a tree for any node
566 * matching a specific key.
568 * Input:
569 * RootPtr - a pointer to the header of the tree to be searched.
570 * FindMe - a pointer to the key value for which to search.
572 * Output:
573 * A pointer to a node with a key that matches the key indicated by
574 * FindMe, or NULL if no such node was found.
576 * Note: In a tree that allows duplicates, the pointer returned *might
577 * not* point to the (sequentially) first occurance of the
578 * desired key. In such a tree, it may be more useful to use
579 * ubi_btLocate().
580 * ------------------------------------------------------------------------ **
583 ubi_btNodePtr ubi_btNext( ubi_btNodePtr P );
584 /* ------------------------------------------------------------------------ **
585 * Given the node indicated by P, find the (sorted order) Next node in the
586 * tree.
587 * Input: P - a pointer to a node that exists in a binary tree.
588 * Output: A pointer to the "next" node in the tree, or NULL if P pointed
589 * to the "last" node in the tree or was NULL.
590 * ------------------------------------------------------------------------ **
593 ubi_btNodePtr ubi_btPrev( ubi_btNodePtr P );
594 /* ------------------------------------------------------------------------ **
595 * Given the node indicated by P, find the (sorted order) Previous node in
596 * the tree.
597 * Input: P - a pointer to a node that exists in a binary tree.
598 * Output: A pointer to the "previous" node in the tree, or NULL if P
599 * pointed to the "first" node in the tree or was NULL.
600 * ------------------------------------------------------------------------ **
603 ubi_btNodePtr ubi_btFirst( ubi_btNodePtr P );
604 /* ------------------------------------------------------------------------ **
605 * Given the node indicated by P, find the (sorted order) First node in the
606 * subtree of which *P is the root.
607 * Input: P - a pointer to a node that exists in a binary tree.
608 * Output: A pointer to the "first" node in a subtree that has *P as its
609 * root. This function will return NULL only if P is NULL.
610 * Note: In general, you will be passing in the value of the root field
611 * of an ubi_btRoot structure.
612 * ------------------------------------------------------------------------ **
615 ubi_btNodePtr ubi_btLast( ubi_btNodePtr P );
616 /* ------------------------------------------------------------------------ **
617 * Given the node indicated by P, find the (sorted order) Last node in the
618 * subtree of which *P is the root.
619 * Input: P - a pointer to a node that exists in a binary tree.
620 * Output: A pointer to the "last" node in a subtree that has *P as its
621 * root. This function will return NULL only if P is NULL.
622 * Note: In general, you will be passing in the value of the root field
623 * of an ubi_btRoot structure.
624 * ------------------------------------------------------------------------ **
627 ubi_btNodePtr ubi_btFirstOf( ubi_btRootPtr RootPtr,
628 ubi_btItemPtr MatchMe,
629 ubi_btNodePtr p );
630 /* ------------------------------------------------------------------------ **
631 * Given a tree that a allows duplicate keys, and a pointer to a node in
632 * the tree, this function will return a pointer to the first (traversal
633 * order) node with the same key value.
635 * Input: RootPtr - A pointer to the root of the tree.
636 * MatchMe - A pointer to the key value. This should probably
637 * point to the key within node *p.
638 * p - A pointer to a node in the tree.
639 * Output: A pointer to the first node in the set of nodes with keys
640 * matching <FindMe>.
641 * Notes: Node *p MUST be in the set of nodes with keys matching
642 * <FindMe>. If not, this function will return NULL.
644 * 4.7: Bug found & fixed by Massimo Campostrini,
645 * Istituto Nazionale di Fisica Nucleare, Sezione di Pisa.
647 * ------------------------------------------------------------------------ **
650 ubi_btNodePtr ubi_btLastOf( ubi_btRootPtr RootPtr,
651 ubi_btItemPtr MatchMe,
652 ubi_btNodePtr p );
653 /* ------------------------------------------------------------------------ **
654 * Given a tree that a allows duplicate keys, and a pointer to a node in
655 * the tree, this function will return a pointer to the last (traversal
656 * order) node with the same key value.
658 * Input: RootPtr - A pointer to the root of the tree.
659 * MatchMe - A pointer to the key value. This should probably
660 * point to the key within node *p.
661 * p - A pointer to a node in the tree.
662 * Output: A pointer to the last node in the set of nodes with keys
663 * matching <FindMe>.
664 * Notes: Node *p MUST be in the set of nodes with keys matching
665 * <FindMe>. If not, this function will return NULL.
667 * 4.7: Bug found & fixed by Massimo Campostrini,
668 * Istituto Nazionale di Fisica Nucleare, Sezione di Pisa.
670 * ------------------------------------------------------------------------ **
673 ubi_trBool ubi_btTraverse( ubi_btRootPtr RootPtr,
674 ubi_btActionRtn EachNode,
675 void *UserData );
676 /* ------------------------------------------------------------------------ **
677 * Traverse a tree in sorted order (non-recursively). At each node, call
678 * (*EachNode)(), passing a pointer to the current node, and UserData as the
679 * second parameter.
680 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
681 * the tree to be traversed.
682 * EachNode - a pointer to a function to be called at each node
683 * as the node is visited.
684 * UserData - a generic pointer that may point to anything that
685 * you choose.
686 * Output: A boolean value. FALSE if the tree is empty, otherwise TRUE.
687 * ------------------------------------------------------------------------ **
690 ubi_trBool ubi_btKillTree( ubi_btRootPtr RootPtr,
691 ubi_btKillNodeRtn FreeNode );
692 /* ------------------------------------------------------------------------ **
693 * Delete an entire tree (non-recursively) and reinitialize the ubi_btRoot
694 * structure. Note that this function will return FALSE if either parameter
695 * is NULL.
697 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates
698 * the root of the tree to delete.
699 * FreeNode - a function that will be called for each node in the
700 * tree to deallocate the memory used by the node.
702 * Output: A boolean value. FALSE if either input parameter was NULL, else
703 * TRUE.
705 * ------------------------------------------------------------------------ **
708 ubi_btNodePtr ubi_btLeafNode( ubi_btNodePtr leader );
709 /* ------------------------------------------------------------------------ **
710 * Returns a pointer to a leaf node.
712 * Input: leader - Pointer to a node at which to start the descent.
714 * Output: A pointer to a leaf node selected in a somewhat arbitrary
715 * manner.
717 * Notes: I wrote this function because I was using splay trees as a
718 * database cache. The cache had a maximum size on it, and I
719 * needed a way of choosing a node to sacrifice if the cache
720 * became full. In a splay tree, less recently accessed nodes
721 * tend toward the bottom of the tree, meaning that leaf nodes
722 * are good candidates for removal. (I really can't think of
723 * any other reason to use this function.)
724 * + In a simple binary tree or an AVL tree, the most recently
725 * added nodes tend to be nearer the bottom, making this a *bad*
726 * way to choose which node to remove from the cache.
727 * + Randomizing the traversal order is probably a good idea. You
728 * can improve the randomization of leaf node selection by passing
729 * in pointers to nodes other than the root node each time. A
730 * pointer to any node in the tree will do. Of course, if you
731 * pass a pointer to a leaf node you'll get the same thing back.
733 * ------------------------------------------------------------------------ **
737 int ubi_btModuleID( int size, char *list[] );
738 /* ------------------------------------------------------------------------ **
739 * Returns a set of strings that identify the module.
741 * Input: size - The number of elements in the array <list>.
742 * list - An array of pointers of type (char *). This array
743 * should, initially, be empty. This function will fill
744 * in the array with pointers to strings.
745 * Output: The number of elements of <list> that were used. If this value
746 * is less than <size>, the values of the remaining elements are
747 * not guaranteed.
749 * Notes: Please keep in mind that the pointers returned indicate strings
750 * stored in static memory. Don't free() them, don't write over
751 * them, etc. Just read them.
752 * ------------------------------------------------------------------------ **
755 /* -------------------------------------------------------------------------- **
756 * Masquarade...
758 * This set of defines allows you to write programs that will use any of the
759 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
760 * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
761 * including the appropriate module header.
764 #define ubi_trItemPtr ubi_btItemPtr
766 #define ubi_trNode ubi_btNode
767 #define ubi_trNodePtr ubi_btNodePtr
769 #define ubi_trRoot ubi_btRoot
770 #define ubi_trRootPtr ubi_btRootPtr
772 #define ubi_trCompFunc ubi_btCompFunc
773 #define ubi_trActionRtn ubi_btActionRtn
774 #define ubi_trKillNodeRtn ubi_btKillNodeRtn
776 #define ubi_trSgn( x ) ubi_btSgn( x )
778 #define ubi_trInitNode( Np ) ubi_btInitNode( (ubi_btNodePtr)(Np) )
780 #define ubi_trInitTree( Rp, Cf, Fl ) \
781 ubi_btInitTree( (ubi_btRootPtr)(Rp), (ubi_btCompFunc)(Cf), (Fl) )
783 #define ubi_trInsert( Rp, Nn, Ip, On ) \
784 ubi_btInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
785 (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
787 #define ubi_trRemove( Rp, Dn ) \
788 ubi_btRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
790 #define ubi_trLocate( Rp, Ip, Op ) \
791 ubi_btLocate( (ubi_btRootPtr)(Rp), \
792 (ubi_btItemPtr)(Ip), \
793 (ubi_trCompOps)(Op) )
795 #define ubi_trFind( Rp, Ip ) \
796 ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
798 #define ubi_trNext( P ) ubi_btNext( (ubi_btNodePtr)(P) )
800 #define ubi_trPrev( P ) ubi_btPrev( (ubi_btNodePtr)(P) )
802 #define ubi_trFirst( P ) ubi_btFirst( (ubi_btNodePtr)(P) )
804 #define ubi_trLast( P ) ubi_btLast( (ubi_btNodePtr)(P) )
806 #define ubi_trFirstOf( Rp, Ip, P ) \
807 ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
808 (ubi_btItemPtr)(Ip), \
809 (ubi_btNodePtr)(P) )
811 #define ubi_trLastOf( Rp, Ip, P ) \
812 ubi_btLastOf( (ubi_btRootPtr)(Rp), \
813 (ubi_btItemPtr)(Ip), \
814 (ubi_btNodePtr)(P) )
816 #define ubi_trTraverse( Rp, En, Ud ) \
817 ubi_btTraverse((ubi_btRootPtr)(Rp), (ubi_btActionRtn)(En), (void *)(Ud))
819 #define ubi_trKillTree( Rp, Fn ) \
820 ubi_btKillTree( (ubi_btRootPtr)(Rp), (ubi_btKillNodeRtn)(Fn) )
822 #define ubi_trLeafNode( Nd ) \
823 ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
825 #define ubi_trModuleID( s, l ) ubi_btModuleID( s, l )
827 /* ========================================================================== */
828 #endif /* UBI_BINTREE_H */