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