Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

ubi_BinTree.h

Go to the documentation of this file.
00001 #ifndef UBI_BINTREE_H
00002 #define UBI_BINTREE_H
00003 /* $Id$ */
00004 /* ========================================================================== **
00005  *                              ubi_BinTree.h
00006  *
00007  *  Copyright (C) 1991-1998 by Christopher R. Hertel
00008  *
00009  *  Email:  crh@ubiqx.mn.org
00010  * -------------------------------------------------------------------------- **
00011  *
00012  *  This module implements a simple binary tree.
00013  *
00014  * -------------------------------------------------------------------------- **
00015  *
00016  *  This library is free software; you can redistribute it and/or
00017  *  modify it under the terms of the GNU Library General Public
00018  *  License as published by the Free Software Foundation; either
00019  *  version 2 of the License, or (at your option) any later version.
00020  *
00021  *  This library is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  *  Library General Public License for more details.
00025  *
00026  *  You should have received a copy of the GNU Library General Public
00027  *  License along with this library; if not, write to the Free
00028  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00029  *
00030  * ========================================================================== **
00031  */
00032 
00033 #include "sys_include.h"  /* Global include file, used to adapt the ubiqx
00034                            * modules to the host environment and the project
00035                            * with which the modules will be used.  See
00036                            * sys_include.h for more info.
00037                            */
00038 
00039 /* -------------------------------------------------------------------------- **
00040  * Macros and constants.
00041  *
00042  *  General purpose:
00043  *    ubi_trTRUE  - Boolean TRUE.
00044  *    ubi_trFALSE - Boolean FALSE.
00045  *
00046  *  Flags used in the tree header:
00047  *    ubi_trOVERWRITE   - This flag indicates that an existing node may be
00048  *                        overwritten by a new node with a matching key.
00049  *    ubi_trDUPKEY      - This flag indicates that the tree allows duplicate
00050  *                        keys.  If the tree does allow duplicates, the
00051  *                        overwrite flag is ignored.
00052  *
00053  *  Node link array index constants:  (Each node has an array of three
00054  *  pointers.  One to the left, one to the right, and one back to the
00055  *  parent.)
00056  *    ubi_trLEFT    - Left child pointer.
00057  *    ubi_trPARENT  - Parent pointer.
00058  *    ubi_trRIGHT   - Right child pointer.
00059  *    ubi_trEQUAL   - Synonym for PARENT.
00060  *
00061  *  ubi_trCompOps:  These values are used in the ubi_trLocate() function.
00062  *    ubi_trLT  - request the first instance of the greatest key less than
00063  *                the search key.
00064  *    ubi_trLE  - request the first instance of the greatest key that is less
00065  *                than or equal to the search key.
00066  *    ubi_trEQ  - request the first instance of key that is equal to the
00067  *                search key.
00068  *    ubi_trGE  - request the first instance of a key that is greater than
00069  *                or equal to the search key.
00070  *    ubi_trGT  - request the first instance of the first key that is greater
00071  *                than the search key.
00072  * -------------------------------------------------------------------------- **
00073  */
00074 
00075 #define ubi_trTRUE  0xFF
00076 #define ubi_trFALSE 0x00
00077 
00078 #define ubi_trOVERWRITE 0x01        /* Turn on allow overwrite      */
00079 #define ubi_trDUPKEY    0x02        /* Turn on allow duplicate keys */
00080 
00081 /* Pointer array index constants... */
00082 #define ubi_trLEFT   0x00
00083 #define ubi_trPARENT 0x01
00084 #define ubi_trRIGHT  0x02
00085 #define ubi_trEQUAL  ubi_trPARENT
00086 
00087 typedef enum {
00088   ubi_trLT = 1,
00089   ubi_trLE,
00090   ubi_trEQ,
00091   ubi_trGE,
00092   ubi_trGT
00093   } ubi_trCompOps;
00094 
00095 /* -------------------------------------------------------------------------- **
00096  * These three macros allow simple manipulation of pointer index values (LEFT,
00097  * RIGHT, and PARENT).
00098  *
00099  *    Normalize() -  converts {LEFT, PARENT, RIGHT} into {-1, 0 ,1}.  C
00100  *                   uses {negative, zero, positive} values to indicate
00101  *                   {less than, equal to, greater than}.
00102  *    AbNormal()  -  converts {negative, zero, positive} to {LEFT, PARENT,
00103  *                   RIGHT} (opposite of Normalize()).  Note: C comparison
00104  *                   functions, such as strcmp(), return {negative, zero,
00105  *                   positive} values, which are not necessarily {-1, 0,
00106  *                   1}.  This macro uses the the ubi_btSgn() function to
00107  *                   compensate.
00108  *    RevWay()    -  converts LEFT to RIGHT and RIGHT to LEFT.  PARENT (EQUAL)
00109  *                   is left as is.
00110  * -------------------------------------------------------------------------- **
00111  */
00112 #define ubi_trNormalize(W) ((char)( (W) - ubi_trEQUAL ))
00113 #define ubi_trAbNormal(W)  ((char)( ((char)ubi_btSgn( (long)(W) )) \
00114                                          + ubi_trEQUAL ))
00115 #define ubi_trRevWay(W)    ((char)( ubi_trEQUAL - ((W) - ubi_trEQUAL) ))
00116 
00117 /* -------------------------------------------------------------------------- **
00118  * These macros allow us to quickly read the values of the OVERWRITE and
00119  * DUPlicate KEY bits of the tree root flags field.
00120  * -------------------------------------------------------------------------- **
00121  */
00122 #define ubi_trDups_OK(A) \
00123         ((ubi_trDUPKEY & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
00124 #define ubi_trOvwt_OK(A) \
00125         ((ubi_trOVERWRITE & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))
00126 
00127 /* -------------------------------------------------------------------------- **
00128  * Additional Macros...
00129  *
00130  *  ubi_trCount()   - Given a pointer to a tree root, this macro returns the
00131  *                    number of nodes currently in the tree.
00132  *
00133  *  ubi_trNewTree() - This macro makes it easy to declare and initialize a
00134  *                    tree header in one step.  The line
00135  *
00136  *                      static ubi_trNewTree( MyTree, cmpfn, ubi_trDUPKEY );
00137  *
00138  *                    is equivalent to
00139  *
00140  *                      static ubi_trRoot MyTree[1]
00141  *                        = {{ NULL, cmpfn, 0, ubi_trDUPKEY }};
00142  *
00143  * -------------------------------------------------------------------------- **
00144  */
00145 
00146 #define ubi_trCount( R ) (((ubi_trRootPtr)(R))->count)
00147 
00148 #define ubi_trNewTree( N, C, F ) ubi_trRoot (N)[1] = {{ NULL, (C), 0, (F) }}
00149 
00150 /* -------------------------------------------------------------------------- **
00151  * Typedefs...
00152  * 
00153  * ubi_trBool   - Your typcial true or false...
00154  *
00155  * Item Pointer:  The ubi_btItemPtr is a generic pointer. It is used to
00156  *                indicate a key that is being searched for within the tree.
00157  *                Searching occurs whenever the ubi_trFind(), ubi_trLocate(),
00158  *                or ubi_trInsert() functions are called.
00159  * -------------------------------------------------------------------------- **
00160  */
00161 
00162 typedef unsigned char ubi_trBool;
00163 
00164 typedef void *ubi_btItemPtr;          /* A pointer to key data within a node. */
00165 
00166 /*  ------------------------------------------------------------------------- **
00167  *  Binary Tree Node Structure:  This structure defines the basic elements of
00168  *       the tree nodes.  In general you *SHOULD NOT PLAY WITH THESE FIELDS*!
00169  *       But, of course, I have to put the structure into this header so that
00170  *       you can use it as a building block.
00171  *
00172  *  The fields are as follows:
00173  *    Link     -  an array of pointers.  These pointers are manipulated by
00174  *                the BT routines.  The pointers indicate the left and right
00175  *                child nodes and the parent node.  By keeping track of the
00176  *                parent pointer, we avoid the need for recursive routines or
00177  *                hand-tooled stacks to keep track of our path back to the
00178  *                root.  The use of these pointers is subject to change without
00179  *                notice.
00180  *    gender   -  a one-byte field indicating whether the node is the RIGHT or
00181  *                LEFT child of its parent.  If the node is the root of the
00182  *                tree, gender will be PARENT.
00183  *    balance  -  only used by the AVL tree module.  This field indicates
00184  *                the height balance at a given node.  See ubi_AVLtree for
00185  *                details.
00186  *
00187  *  ------------------------------------------------------------------------- **
00188  */
00189 typedef struct ubi_btNodeStruct {
00190   struct ubi_btNodeStruct *Link[ 3 ];
00191   char                     gender;
00192   char                     balance;
00193   } ubi_btNode;
00194 
00195 typedef ubi_btNode *ubi_btNodePtr;     /* Pointer to an ubi_btNode structure. */
00196 
00197 /*  ------------------------------------------------------------------------- **
00198  * The next three typedefs define standard function types used by the binary
00199  * tree management routines.  In particular:
00200  *
00201  *    ubi_btCompFunc    is a pointer to a comparison function.  Comparison
00202  *                      functions are passed an ubi_btItemPtr and an
00203  *                      ubi_btNodePtr.  They return a value that is (<0), 0,
00204  *                      or (>0) to indicate that the Item is (respectively)
00205  *                      "less than", "equal to", or "greater than" the Item
00206  *                      contained within the node.  (See ubi_btInitTree()).
00207  *    ubi_btActionRtn   is a pointer to a function that may be called for each
00208  *                      node visited when performing a tree traversal (see
00209  *                      ubi_btTraverse()).  The function will be passed two
00210  *                      parameters: the first is a pointer to a node in the
00211  *                      tree, the second is a generic pointer that may point to
00212  *                      anything that you like.
00213  *    ubi_btKillNodeRtn is a pointer to a function that will deallocate the
00214  *                      memory used by a node (see ubi_btKillTree()).  Since
00215  *                      memory management is left up to you, deallocation may
00216  *                      mean anything that you want it to mean.  Just remember
00217  *                      that the tree *will* be destroyed and that none of the
00218  *                      node pointers will be valid any more.
00219  *  ------------------------------------------------------------------------- **
00220  */
00221 
00222 typedef  int (*ubi_btCheckFunc)( ubi_btNodePtr, void * );
00223 
00224 typedef  int (*ubi_btCompFunc)( ubi_btItemPtr, ubi_btNodePtr );
00225 
00226 typedef void (*ubi_btActionRtn)( ubi_btNodePtr, void * );
00227 
00228 typedef void (*ubi_btKillNodeRtn)( ubi_btNodePtr );
00229 
00230 /* -------------------------------------------------------------------------- **
00231  * Tree Root Structure: This structure gives us a convenient handle for
00232  *                      accessing whole binary trees.  The fields are:
00233  *    root  -  A pointer to the root node of the tree.
00234  *    count -  A count of the number of nodes stored in the tree.
00235  *    cmp   -  A pointer to the comparison routine to be used when building or
00236  *             searching the tree.
00237  *    flags -  A set of bit flags.  Two flags are currently defined:
00238  *
00239  *       ubi_trOVERWRITE -  If set, this flag indicates that a new node should
00240  *         (bit 0x01)       overwrite an old node if the two have identical
00241  *                          keys (ie., the keys are equal).
00242  *       ubi_trDUPKEY    -  If set, this flag indicates that the tree is
00243  *         (bit 0x02)       allowed to contain nodes with duplicate keys.
00244  *
00245  *       NOTE: ubi_trInsert() tests ubi_trDUPKEY before ubi_trOVERWRITE.
00246  *
00247  * All of these values are set when you initialize the root structure by
00248  * calling ubi_trInitTree().
00249  * -------------------------------------------------------------------------- **
00250  */
00251 
00252 typedef struct {
00253   ubi_btNodePtr  root;     /* A pointer to the root node of the tree       */
00254   ubi_btCompFunc cmp;      /* A pointer to the tree's comparison function  */
00255   unsigned long  count;    /* A count of the number of nodes in the tree   */
00256   char           flags;    /* Overwrite Y|N, Duplicate keys Y|N...         */
00257   } ubi_btRoot;
00258 
00259 typedef ubi_btRoot *ubi_btRootPtr;  /* Pointer to an ubi_btRoot structure. */
00260 
00261 
00262 /* -------------------------------------------------------------------------- **
00263  * Function Prototypes.
00264  */
00265 
00266 long ubi_btSgn( long x );
00267   /* ------------------------------------------------------------------------ **
00268    * Return the sign of x; {negative,zero,positive} ==> {-1, 0, 1}.
00269    *
00270    *  Input:  x - a signed long integer value.
00271    *
00272    *  Output: the "sign" of x, represented as follows:
00273    *            -1 == negative
00274    *             0 == zero (no sign)
00275    *             1 == positive
00276    *
00277    * Note: This utility is provided in order to facilitate the conversion
00278    *       of C comparison function return values into BinTree direction
00279    *       values: {LEFT, PARENT, EQUAL}.  It is INCORPORATED into the
00280    *       AbNormal() conversion macro!
00281    *
00282    * ------------------------------------------------------------------------ **
00283    */
00284 
00285 ubi_btNodePtr ubi_btInitNode( ubi_btNodePtr NodePtr );
00286   /* ------------------------------------------------------------------------ **
00287    * Initialize a tree node.
00288    *
00289    *  Input:   a pointer to a ubi_btNode structure to be initialized.
00290    *  Output:  a pointer to the initialized ubi_btNode structure (ie. the
00291    *           same as the input pointer).
00292    * ------------------------------------------------------------------------ **
00293    */
00294 
00295 ubi_btRootPtr  ubi_btInitTree( ubi_btRootPtr   RootPtr,
00296                                ubi_btCompFunc  CompFunc,
00297                                char            Flags );
00298   /* ------------------------------------------------------------------------ **
00299    * Initialize the fields of a Tree Root header structure.
00300    *  
00301    *  Input:   RootPtr   - a pointer to an ubi_btRoot structure to be
00302    *                       initialized.   
00303    *           CompFunc  - a pointer to a comparison function that will be used
00304    *                       whenever nodes in the tree must be compared against
00305    *                       outside values.
00306    *           Flags     - One bytes worth of flags.  Flags include
00307    *                       ubi_trOVERWRITE and ubi_trDUPKEY.  See the header
00308    *                       file for more info.
00309    *
00310    *  Output:  a pointer to the initialized ubi_btRoot structure (ie. the
00311    *           same value as RootPtr).
00312    * 
00313    *  Note:    The interface to this function has changed from that of 
00314    *           previous versions.  The <Flags> parameter replaces two      
00315    *           boolean parameters that had the same basic effect.
00316    * ------------------------------------------------------------------------ **
00317    */
00318 
00319 ubi_trBool ubi_btInsert( ubi_btRootPtr  RootPtr,
00320                          ubi_btNodePtr  NewNode,
00321                          ubi_btItemPtr  ItemPtr,
00322                          ubi_btNodePtr *OldNode );
00323   /* ------------------------------------------------------------------------ **
00324    * This function uses a non-recursive algorithm to add a new element to the
00325    * tree.
00326    *
00327    *  Input:   RootPtr  -  a pointer to the ubi_btRoot structure that indicates
00328    *                       the root of the tree to which NewNode is to be added.
00329    *           NewNode  -  a pointer to an ubi_btNode structure that is NOT
00330    *                       part of any tree.
00331    *           ItemPtr  -  A pointer to the sort key that is stored within
00332    *                       *NewNode.  ItemPtr MUST point to information stored
00333    *                       in *NewNode or an EXACT DUPLICATE.  The key data
00334    *                       indicated by ItemPtr is used to place the new node
00335    *                       into the tree.
00336    *           OldNode  -  a pointer to an ubi_btNodePtr.  When searching
00337    *                       the tree, a duplicate node may be found.  If
00338    *                       duplicates are allowed, then the new node will
00339    *                       be simply placed into the tree.  If duplicates
00340    *                       are not allowed, however, then one of two things
00341    *                       may happen.
00342    *                       1) if overwritting *is not* allowed, this
00343    *                          function will return FALSE (indicating that
00344    *                          the new node could not be inserted), and
00345    *                          *OldNode will point to the duplicate that is
00346    *                          still in the tree.
00347    *                       2) if overwritting *is* allowed, then this
00348    *                          function will swap **OldNode for *NewNode.
00349    *                          In this case, *OldNode will point to the node
00350    *                          that was removed (thus allowing you to free
00351    *                          the node).
00352    *                          **  If you are using overwrite mode, ALWAYS  **
00353    *                          ** check the return value of this parameter! **
00354    *                 Note: You may pass NULL in this parameter, the
00355    *                       function knows how to cope.  If you do this,
00356    *                       however, there will be no way to return a
00357    *                       pointer to an old (ie. replaced) node (which is
00358    *                       a problem if you are using overwrite mode).
00359    *
00360    *  Output:  a boolean value indicating success or failure.  The function
00361    *           will return FALSE if the node could not be added to the tree.
00362    *           Such failure will only occur if duplicates are not allowed,
00363    *           nodes cannot be overwritten, AND a duplicate key was found
00364    *           within the tree.
00365    * ------------------------------------------------------------------------ **
00366    */
00367 
00368 ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr,
00369                             ubi_btNodePtr DeadNode );
00370   /* ------------------------------------------------------------------------ **
00371    * This function removes the indicated node from the tree.
00372    *
00373    *  Input:   RootPtr  -  A pointer to the header of the tree that contains
00374    *                       the node to be removed.
00375    *           DeadNode -  A pointer to the node that will be removed.
00376    *
00377    *  Output:  This function returns a pointer to the node that was removed
00378    *           from the tree (ie. the same as DeadNode).
00379    *
00380    *  Note:    The node MUST be in the tree indicated by RootPtr.  If not,
00381    *           strange and evil things will happen to your trees.
00382    * ------------------------------------------------------------------------ **
00383    */
00384 
00385 ubi_btNodePtr ubi_btLocate( ubi_btRootPtr RootPtr,
00386                             ubi_btItemPtr FindMe,
00387                             ubi_trCompOps CompOp );
00388   /* ------------------------------------------------------------------------ **
00389    * The purpose of ubi_btLocate() is to find a node or set of nodes given
00390    * a target value and a "comparison operator".  The Locate() function is
00391    * more flexible and (in the case of trees that may contain dupicate keys)
00392    * more precise than the ubi_btFind() function.  The latter is faster,
00393    * but it only searches for exact matches and, if the tree contains
00394    * duplicates, Find() may return a pointer to any one of the duplicate-
00395    * keyed records.
00396    *
00397    *  Input:
00398    *     RootPtr  -  A pointer to the header of the tree to be searched.
00399    *     FindMe   -  An ubi_btItemPtr that indicates the key for which to
00400    *                 search.
00401    *     CompOp   -  One of the following:
00402    *                    CompOp     Return a pointer to the node with
00403    *                    ------     ---------------------------------
00404    *                   ubi_trLT - the last key value that is less
00405    *                              than FindMe.
00406    *                   ubi_trLE - the first key matching FindMe, or
00407    *                              the last key that is less than
00408    *                              FindMe.
00409    *                   ubi_trEQ - the first key matching FindMe.
00410    *                   ubi_trGE - the first key matching FindMe, or the
00411    *                              first key greater than FindMe.
00412    *                   ubi_trGT - the first key greater than FindMe.
00413    *  Output:
00414    *     A pointer to the node matching the criteria listed above under
00415    *     CompOp, or NULL if no node matched the criteria.
00416    *
00417    *  Notes:
00418    *     In the case of trees with duplicate keys, Locate() will behave as
00419    *     follows:
00420    *
00421    *     Find:  3                       Find: 3
00422    *     Keys:  1 2 2 2 3 3 3 3 3 4 4   Keys: 1 1 2 2 2 4 4 5 5 5 6
00423    *                  ^ ^         ^                   ^ ^
00424    *                 LT EQ        GT                 LE GE
00425    *
00426    *     That is, when returning a pointer to a node with a key that is LESS
00427    *     THAN the target key (FindMe), Locate() will return a pointer to the
00428    *     LAST matching node.
00429    *     When returning a pointer to a node with a key that is GREATER
00430    *     THAN the target key (FindMe), Locate() will return a pointer to the
00431    *     FIRST matching node.
00432    *
00433    *  See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
00434    * ------------------------------------------------------------------------ **
00435    */
00436 
00437 ubi_btNodePtr ubi_btFind( ubi_btRootPtr RootPtr,
00438                           ubi_btItemPtr FindMe );
00439   /* ------------------------------------------------------------------------ **
00440    * This function performs a non-recursive search of a tree for any node
00441    * matching a specific key.
00442    *
00443    *  Input:
00444    *     RootPtr  -  a pointer to the header of the tree to be searched.
00445    *     FindMe   -  a pointer to the key value for which to search.
00446    *
00447    *  Output:
00448    *     A pointer to a node with a key that matches the key indicated by
00449    *     FindMe, or NULL if no such node was found.
00450    *
00451    *  Note:   In a tree that allows duplicates, the pointer returned *might
00452    *          not* point to the (sequentially) first occurance of the
00453    *          desired key.  In such a tree, it may be more useful to use
00454    *          ubi_btLocate().
00455    * ------------------------------------------------------------------------ **
00456    */
00457 
00458 int ubi_btCheck( ubi_btRootPtr RootPtr, ubi_btCheckFunc func, void *UserData );
00459   /* ------------------------------------------------------------------------ **
00460    * This function performs a non-recursive search of a tree checking for any
00461    * node that matches the data supplied, by calling func.
00462    *
00463    *  Input:
00464    *     RootPtr  -  a pointer to the header of the tree to be searched.
00465    *     func     -  a pointer function to perform the check
00466    *     UserData -  a pointer to the user data to pass to func
00467    *
00468    *  Output:
00469    *     Returns 1 when found, 0 if not found
00470    *   
00471    *  Note:
00472    * ------------------------------------------------------------------------ **
00473    */
00474 
00475 
00476 ubi_btNodePtr ubi_btNext( ubi_btNodePtr P );
00477   /* ------------------------------------------------------------------------ **
00478    * Given the node indicated by P, find the (sorted order) Next node in the
00479    * tree.
00480    *  Input:   P  -  a pointer to a node that exists in a binary tree.
00481    *  Output:  A pointer to the "next" node in the tree, or NULL if P pointed
00482    *           to the "last" node in the tree or was NULL.
00483    * ------------------------------------------------------------------------ **
00484    */
00485 
00486 ubi_btNodePtr ubi_btPrev( ubi_btNodePtr P );
00487   /* ------------------------------------------------------------------------ **
00488    * Given the node indicated by P, find the (sorted order) Previous node in
00489    * the tree.
00490    *  Input:   P  -  a pointer to a node that exists in a binary tree.
00491    *  Output:  A pointer to the "previous" node in the tree, or NULL if P
00492    *           pointed to the "first" node in the tree or was NULL.
00493    * ------------------------------------------------------------------------ **
00494    */
00495 
00496 ubi_btNodePtr ubi_btFirst( ubi_btNodePtr P );
00497   /* ------------------------------------------------------------------------ **
00498    * Given the node indicated by P, find the (sorted order) First node in the
00499    * subtree of which *P is the root.
00500    *  Input:   P  -  a pointer to a node that exists in a binary tree.
00501    *  Output:  A pointer to the "first" node in a subtree that has *P as its
00502    *           root.  This function will return NULL only if P is NULL.
00503    *  Note:    In general, you will be passing in the value of the root field
00504    *           of an ubi_btRoot structure.
00505    * ------------------------------------------------------------------------ **
00506    */
00507 
00508 ubi_btNodePtr ubi_btLast( ubi_btNodePtr P );
00509   /* ------------------------------------------------------------------------ **
00510    * Given the node indicated by P, find the (sorted order) Last node in the
00511    * subtree of which *P is the root.
00512    *  Input:   P  -  a pointer to a node that exists in a binary tree.
00513    *  Output:  A pointer to the "last" node in a subtree that has *P as its
00514    *           root.  This function will return NULL only if P is NULL.
00515    *  Note:    In general, you will be passing in the value of the root field
00516    *           of an ubi_btRoot structure.
00517    * ------------------------------------------------------------------------ **
00518    */
00519 
00520 ubi_btNodePtr ubi_btFirstOf( ubi_btRootPtr RootPtr,
00521                              ubi_btItemPtr MatchMe,
00522                              ubi_btNodePtr p );
00523   /* ------------------------------------------------------------------------ **
00524    * Given a tree that a allows duplicate keys, and a pointer to a node in
00525    * the tree, this function will return a pointer to the first (traversal
00526    * order) node with the same key value.
00527    *
00528    *  Input:  RootPtr - A pointer to the root of the tree.
00529    *          MatchMe - A pointer to the key value.  This should probably
00530    *                    point to the key within node *p.
00531    *          p       - A pointer to a node in the tree.
00532    *  Output: A pointer to the first node in the set of nodes with keys
00533    *          matching <FindMe>.
00534    *  Notes:  Node *p MUST be in the set of nodes with keys matching
00535    *          <FindMe>.  If not, this function will return NULL.
00536    *
00537    *          4.7: Bug found & fixed by Massimo Campostrini,
00538    *               Istituto Nazionale di Fisica Nucleare, Sezione di Pisa.
00539    *
00540    * ------------------------------------------------------------------------ **
00541    */
00542 
00543 ubi_btNodePtr ubi_btLastOf( ubi_btRootPtr RootPtr,
00544                             ubi_btItemPtr MatchMe,
00545                             ubi_btNodePtr p );
00546   /* ------------------------------------------------------------------------ **
00547    * Given a tree that a allows duplicate keys, and a pointer to a node in
00548    * the tree, this function will return a pointer to the last (traversal
00549    * order) node with the same key value.
00550    *
00551    *  Input:  RootPtr - A pointer to the root of the tree.
00552    *          MatchMe - A pointer to the key value.  This should probably
00553    *                    point to the key within node *p.
00554    *          p       - A pointer to a node in the tree.
00555    *  Output: A pointer to the last node in the set of nodes with keys
00556    *          matching <FindMe>.
00557    *  Notes:  Node *p MUST be in the set of nodes with keys matching
00558    *          <FindMe>.  If not, this function will return NULL.
00559    *
00560    *          4.7: Bug found & fixed by Massimo Campostrini,
00561    *               Istituto Nazionale di Fisica Nucleare, Sezione di Pisa.
00562    *
00563    * ------------------------------------------------------------------------ **
00564    */
00565 
00566 unsigned long ubi_btTraverse( ubi_btRootPtr   RootPtr,
00567                               ubi_btActionRtn EachNode,
00568                               void           *UserData );
00569   /* ------------------------------------------------------------------------ **
00570    * Traverse a tree in sorted order (non-recursively).  At each node, call
00571    * (*EachNode)(), passing a pointer to the current node, and UserData as the
00572    * second parameter.
00573    *
00574    *  Input:   RootPtr  -  a pointer to an ubi_btRoot structure that indicates
00575    *                       the tree to be traversed.
00576    *           EachNode -  a pointer to a function to be called at each node  
00577    *                       as the node is visited.
00578    *           UserData -  a generic pointer that may point to anything that
00579    *                       you choose.
00580    *           
00581    *  Output:  A count of the number of nodes visited.  This will be zero
00582    *           if the tree is empty.
00583    *             
00584    * ------------------------------------------------------------------------ **
00585    */
00586 
00587 
00588 unsigned long ubi_btTraverseReverse( ubi_btRootPtr   RootPtr,
00589                               ubi_btActionRtn EachNode,
00590                               void           *UserData );
00591   /* ------------------------------------------------------------------------ **
00592    * Traverse a tree in reverse sorted order (non-recursively).  At each node,
00593    * call (*EachNode)(), passing a pointer to the current node, and UserData
00594    * as the second parameter.
00595    *
00596    *  Input:   RootPtr  -  a pointer to an ubi_btRoot structure that indicates
00597    *                       the tree to be traversed.
00598    *           EachNode -  a pointer to a function to be called at each node
00599    *                       as the node is visited.
00600    *           UserData -  a generic pointer that may point to anything that
00601    *                       you choose.
00602    *
00603    *  Output:  A count of the number of nodes visited.  This will be zero
00604    *           if the tree is empty.
00605    *
00606    * ------------------------------------------------------------------------ **
00607    */
00608 
00609 unsigned long ubi_btKillTree( ubi_btRootPtr     RootPtr,
00610                               ubi_btKillNodeRtn FreeNode );
00611   /* ------------------------------------------------------------------------ **
00612    * Delete an entire tree (non-recursively) and reinitialize the ubi_btRoot
00613    * structure.  Return a count of the number of nodes deleted.
00614    *
00615    *  Input:   RootPtr  -  a pointer to an ubi_btRoot structure that indicates
00616    *                       the root of the tree to delete.
00617    *           FreeNode -  a function that will be called for each node in the
00618    *                       tree to deallocate the memory used by the node.
00619    *
00620    *  Output:  The number of nodes removed from the tree.
00621    *           A value of 0 will be returned if:
00622    *           - The tree actually contains 0 entries.
00623    *           - the value of <RootPtr> is NULL, in which case the tree is
00624    *             assumed to be empty
00625    *           - the value of <FreeNode> is NULL, in which case entries
00626    *             cannot be removed, so 0 is returned.  *Make sure that you
00627    *             provide a valid value for <FreeNode>*.
00628    *           In all other cases, you should get a positive value equal to
00629    *           the value of RootPtr->count upon entry. 
00630    *
00631    * ------------------------------------------------------------------------ **
00632    */
00633 
00634 ubi_btNodePtr ubi_btLeafNode( ubi_btNodePtr leader );
00635   /* ------------------------------------------------------------------------ **
00636    * Returns a pointer to a leaf node.
00637    *
00638    *  Input:  leader  - Pointer to a node at which to start the descent.
00639    *
00640    *  Output: A pointer to a leaf node selected in a somewhat arbitrary
00641    *          manner.
00642    *
00643    *  Notes:  I wrote this function because I was using splay trees as a
00644    *          database cache.  The cache had a maximum size on it, and I
00645    *          needed a way of choosing a node to sacrifice if the cache
00646    *          became full.  In a splay tree, less recently accessed nodes
00647    *          tend toward the bottom of the tree, meaning that leaf nodes
00648    *          are good candidates for removal.  (I really can't think of
00649    *          any other reason to use this function.)
00650    *        + In a simple binary tree or an AVL tree, the most recently
00651    *          added nodes tend to be nearer the bottom, making this a *bad*
00652    *          way to choose which node to remove from the cache.
00653    *        + Randomizing the traversal order is probably a good idea.  You
00654    *          can improve the randomization of leaf node selection by passing
00655    *          in pointers to nodes other than the root node each time.  A
00656    *          pointer to any node in the tree will do.  Of course, if you
00657    *          pass a pointer to a leaf node you'll get the same thing back.
00658    *
00659    * ------------------------------------------------------------------------ **
00660    */
00661 
00662 
00663 int ubi_btModuleID( int size, char *list[] );
00664   /* ------------------------------------------------------------------------ **
00665    * Returns a set of strings that identify the module.
00666    *
00667    *  Input:  size  - The number of elements in the array <list>.
00668    *          list  - An array of pointers of type (char *).  This array
00669    *                  should, initially, be empty.  This function will fill
00670    *                  in the array with pointers to strings.
00671    *  Output: The number of elements of <list> that were used.  If this value
00672    *          is less than <size>, the values of the remaining elements are
00673    *          not guaranteed.
00674    *
00675    *  Notes:  Please keep in mind that the pointers returned indicate strings
00676    *          stored in static memory.  Don't free() them, don't write over
00677    *          them, etc.  Just read them.
00678    * ------------------------------------------------------------------------ **
00679    */
00680 
00681 /* -------------------------------------------------------------------------- **
00682  * Masquarade...
00683  *
00684  * This set of defines allows you to write programs that will use any of the
00685  * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
00686  * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
00687  * including the appropriate module header.
00688  */
00689 
00690 #define ubi_trItemPtr ubi_btItemPtr
00691 
00692 #define ubi_trNode    ubi_btNode
00693 #define ubi_trNodePtr ubi_btNodePtr
00694 
00695 #define ubi_trRoot    ubi_btRoot
00696 #define ubi_trRootPtr ubi_btRootPtr
00697 
00698 #define ubi_trCompFunc    ubi_btCompFunc
00699 #define ubi_trActionRtn   ubi_btActionRtn
00700 #define ubi_trKillNodeRtn ubi_btKillNodeRtn
00701 
00702 #define ubi_trSgn( x ) ubi_btSgn( x )
00703 
00704 #define ubi_trInitNode( Np ) ubi_btInitNode( (ubi_btNodePtr)(Np) )
00705 
00706 #define ubi_trInitTree( Rp, Cf, Fl ) \
00707         ubi_btInitTree( (ubi_btRootPtr)(Rp), (ubi_btCompFunc)(Cf), (Fl) )
00708 
00709 #define ubi_trInsert( Rp, Nn, Ip, On ) \
00710         ubi_btInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
00711                       (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
00712 
00713 #define ubi_trRemove( Rp, Dn ) \
00714         ubi_btRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
00715 
00716 #define ubi_trLocate( Rp, Ip, Op ) \
00717         ubi_btLocate( (ubi_btRootPtr)(Rp), \
00718                       (ubi_btItemPtr)(Ip), \
00719                       (ubi_trCompOps)(Op) )
00720 
00721 #define ubi_trFind( Rp, Ip ) \
00722         ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
00723 
00724 #define ubi_trCheck( Rp, Fp, Ud) \
00725         ubi_btCheck( (ubi_btRootPtr)(Rp), (ubi_btCheckFunc)(Fp), (void *)(Ud) )
00726 
00727 #define ubi_trNext( P ) ubi_btNext( (ubi_btNodePtr)(P) )
00728 
00729 #define ubi_trPrev( P ) ubi_btPrev( (ubi_btNodePtr)(P) )
00730 
00731 #define ubi_trFirst( P ) ubi_btFirst( (ubi_btNodePtr)(P) )
00732 
00733 #define ubi_trLast( P ) ubi_btLast( (ubi_btNodePtr)(P) )
00734 
00735 #define ubi_trFirstOf( Rp, Ip, P ) \
00736         ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
00737                        (ubi_btItemPtr)(Ip), \
00738                        (ubi_btNodePtr)(P) )
00739 
00740 #define ubi_trLastOf( Rp, Ip, P ) \
00741         ubi_btLastOf( (ubi_btRootPtr)(Rp), \
00742                       (ubi_btItemPtr)(Ip), \
00743                       (ubi_btNodePtr)(P) )
00744 
00745 #define ubi_trTraverse( Rp, En, Ud ) \
00746         ubi_btTraverse((ubi_btRootPtr)(Rp), (ubi_btActionRtn)(En), (void *)(Ud))
00747 
00748 #define ubi_trTraverseReverse( Rp, En, Ud ) \
00749         ubi_btTraverseReverse((ubi_btRootPtr)(Rp), (ubi_btActionRtn)(En), (void *)(Ud))
00750 
00751 #define ubi_trKillTree( Rp, Fn ) \
00752         ubi_btKillTree( (ubi_btRootPtr)(Rp), (ubi_btKillNodeRtn)(Fn) )
00753 
00754 #define ubi_trLeafNode( Nd ) \
00755         ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
00756 
00757 #define ubi_trModuleID( s, l ) ubi_btModuleID( s, l )
00758 
00759 /* ========================================================================== */
00760 #endif /* UBI_BINTREE_H */

Generated on Sun May 14 14:51:19 2006 by  doxygen 1.4.2