00001 #ifndef UBI_BINTREE_H 00002 #define UBI_BINTREE_H 00003 /* ========================================================================== ** 00004 * ubi_BinTree.h 00005 * 00006 * Copyright (C) 1991-1998 by Christopher R. Hertel 00007 * 00008 * Email: crh@ubiqx.mn.org 00009 * -------------------------------------------------------------------------- ** 00010 * 00011 * This module implements a simple binary tree. 00012 * 00013 * -------------------------------------------------------------------------- ** 00014 * 00015 * This library is free software; you can redistribute it and/or 00016 * modify it under the terms of the GNU Library General Public 00017 * License as published by the Free Software Foundation; either 00018 * version 2 of the License, or (at your option) any later version. 00019 * 00020 * This library is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00023 * Library General Public License for more details. 00024 * 00025 * You should have received a copy of the GNU Library General Public 00026 * License along with this library; if not, write to the Free 00027 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00028 * 00029 * -------------------------------------------------------------------------- ** 00030 * 00031 * $Log: ubi_BinTree.h,v $ 00032 * Revision 1.3 2005/01/14 18:31:39 ruim 00033 * Integration of multithreaded system 00034 * 00035 * Revision 1.2 2004/01/29 18:44:09 dwarren 00036 * Modified implementation of profiling to avoid the extra psc-record 00037 * field for the count. Now the counts are kept in the code-ptr records 00038 * and accumulated only at accumulation time. Someday may want to improve 00039 * accumulation algorithm. 00040 * 00041 * Revision 1.1 2004/01/14 20:27:14 dwarren 00042 * XSB Prolog Profiling as command line option -p 00043 * 00044 * Revision 4.10 2000/06/06 20:38:40 crh 00045 * In the ReplaceNode() function, the old node header was being copied 00046 * to the new node header using a byte-by-byte copy. This was causing 00047 * the 'insure' software testing program to report a memory leak. The 00048 * fix was to do a simple assignement: *newnode = *oldnode; 00049 * This quieted the (errant) memory leak reports and is probably a bit 00050 * faster than the bytewise copy. 00051 * 00052 * Revision 4.9 2000/01/08 23:24:30 crh 00053 * Clarified a variety of if( pointer ) lines, replacing them with 00054 * if( NULL != pointer ). This is more correct, and I have heard 00055 * of at least one (obscure?) system out there that uses a non-zero 00056 * value for NULL. 00057 * Also, speed improvement in Neighbor(). It was comparing pointers 00058 * when it could have compared two gender values. The pointer 00059 * comparison was somewhat indirect (does pointer equal the pointer 00060 * of the parent of the node pointed to by pointer). Urq. 00061 * 00062 * Revision 4.8 1999/09/22 03:40:30 crh 00063 * Modified ubi_btTraverse() and ubi_btKillTree(). They now return an 00064 * unsigned long indicating the number of nodes processed. The change 00065 * is subtle. An empty tree formerly returned False, and now returns 00066 * zero. 00067 * 00068 * Revision 4.7 1998/10/21 06:15:07 crh 00069 * Fixed bugs in FirstOf() and LastOf() reported by Massimo Campostrini. 00070 * See function comments. 00071 * 00072 * Revision 4.6 1998/07/25 17:02:10 crh 00073 * Added the ubi_trNewTree() macro. 00074 * 00075 * Revision 4.5 1998/06/04 21:29:27 crh 00076 * Upper-cased defined constants (eg UBI_BINTREE_H) in some header files. 00077 * This is more "standard", and is what people expect. Weird, eh? 00078 * 00079 * Revision 4.4 1998/06/03 17:42:46 crh 00080 * Further fiddling with sys_include.h. It's now in ubi_BinTree.h which is 00081 * included by all of the binary tree files. 00082 * 00083 * Reminder: Some of the ubi_tr* macros in ubi_BinTree.h are redefined in 00084 * ubi_AVLtree.h and ubi_SplayTree.h. This allows easy swapping 00085 * of tree types by simply changing a header. Unfortunately, the 00086 * macro redefinitions in ubi_AVLtree.h and ubi_SplayTree.h will 00087 * conflict if used together. You must either choose a single tree 00088 * type, or use the underlying function calls directly. Compare 00089 * the two header files for more information. 00090 * 00091 * Revision 4.3 1998/06/02 01:28:43 crh 00092 * Changed ubi_null.h to sys_include.h to make it more generic. 00093 * 00094 * Revision 4.2 1998/05/20 04:32:36 crh 00095 * The C file now includes ubi_null.h. See ubi_null.h for more info. 00096 * Also, the balance and gender fields of the node were declared as 00097 * signed char. As I understand it, at least one SunOS or Solaris 00098 * compiler doesn't like "signed char". The declarations were 00099 * wrong anyway, so I changed them to simple "char". 00100 * 00101 * Revision 4.1 1998/03/31 06:13:47 crh 00102 * Thomas Aglassinger sent E'mail pointing out errors in the 00103 * dereferencing of function pointers, and a missing typecast. 00104 * Thanks, Thomas! 00105 * 00106 * Revision 4.0 1998/03/10 03:16:04 crh 00107 * Added the AVL field 'balance' to the ubi_btNode structure. This means 00108 * that all BinTree modules now use the same basic node structure, which 00109 * greatly simplifies the AVL module. 00110 * Decided that this was a big enough change to justify a new major revision 00111 * number. 3.0 was an error, so we're at 4.0. 00112 * 00113 * Revision 2.6 1998/01/24 06:27:30 crh 00114 * Added ubi_trCount() macro. 00115 * 00116 * Revision 2.5 1997/12/23 03:59:21 crh 00117 * In this version, all constants & macros defined in the header file have 00118 * the ubi_tr prefix. Also cleaned up anything that gcc complained about 00119 * when run with '-pedantic -fsyntax-only -Wall'. 00120 * 00121 * Revision 2.4 1997/07/26 04:11:14 crh 00122 * + Just to be annoying I changed ubi_TRUE and ubi_FALSE to ubi_trTRUE 00123 * and ubi_trFALSE. 00124 * + There is now a type ubi_trBool to go with ubi_trTRUE and ubi_trFALSE. 00125 * + There used to be something called "ubi_TypeDefs.h". I got rid of it. 00126 * + Added function ubi_btLeafNode(). 00127 * 00128 * Revision 2.3 1997/06/03 05:15:27 crh 00129 * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid conflicts. 00130 * Also changed the interface to function InitTree(). See the comments 00131 * for this function for more information. 00132 * 00133 * Revision 2.2 1995/10/03 22:00:40 CRH 00134 * Ubisized! 00135 * 00136 * Revision 2.1 95/03/09 23:43:46 CRH 00137 * Added the ModuleID static string and function. These modules are now 00138 * self-identifying. 00139 * 00140 * Revision 2.0 95/02/27 22:00:33 CRH 00141 * Revision 2.0 of this program includes the following changes: 00142 * 00143 * 1) A fix to a major typo in the RepaceNode() function. 00144 * 2) The addition of the static function Border(). 00145 * 3) The addition of the public functions FirstOf() and LastOf(), which 00146 * use Border(). These functions are used with trees that allow 00147 * duplicate keys. 00148 * 4) A complete rewrite of the Locate() function. Locate() now accepts 00149 * a "comparison" operator. 00150 * 5) Overall enhancements to both code and comments. 00151 * 00152 * I decided to give this a new major rev number because the interface has 00153 * changed. In particular, there are two new functions, and changes to the 00154 * Locate() function. 00155 * 00156 * Revision 1.0 93/10/15 22:55:04 CRH 00157 * With this revision, I have added a set of #define's that provide a single, 00158 * standard API to all existing tree modules. Until now, each of the three 00159 * existing modules had a different function and typedef prefix, as follows: 00160 * 00161 * Module Prefix 00162 * ubi_BinTree ubi_bt 00163 * ubi_AVLtree ubi_avl 00164 * ubi_SplayTree ubi_spt 00165 * 00166 * To further complicate matters, only those portions of the base module 00167 * (ubi_BinTree) that were superceeded in the new module had the new names. 00168 * For example, if you were using ubi_SplayTree, the locate function was 00169 * called "ubi_sptLocate", but the next and previous functions remained 00170 * "ubi_btNext" and "ubi_btPrev". 00171 * 00172 * This was not too terrible if you were familiar with the modules and knew 00173 * exactly which tree model you wanted to use. If you wanted to be able to 00174 * change modules (for speed comparisons, etc), things could get messy very 00175 * quickly. 00176 * 00177 * So, I have added a set of defined names that get redefined in any of the 00178 * descendant modules. To use this standardized interface in your code, 00179 * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with 00180 * "ubi_tr". The "ubi_tr" names will resolve to the correct function or 00181 * datatype names for the module that you are using. Just remember to 00182 * include the header for that module in your program file. Because these 00183 * names are handled by the preprocessor, there is no added run-time 00184 * overhead. 00185 * 00186 * Note that the original names do still exist, and can be used if you wish 00187 * to write code directly to a specific module. This should probably only be 00188 * done if you are planning to implement a new descendant type, such as 00189 * red/black trees. CRH 00190 * 00191 * V0.0 - June, 1991 - Written by Christopher R. Hertel (CRH). 00192 * 00193 * ========================================================================== ** 00194 */ 00195 00196 #include "sys_include.h" /* Global include file, used to adapt the ubiqx 00197 * modules to the host environment and the project 00198 * with which the modules will be used. See 00199 * sys_include.h for more info. 00200 */ 00201 00202 #include "xsb_config.h" 00203 #include "auxlry.h" 00204 #include "cell_xsb.h" 00205 #include "psc_xsb.h" 00206 00207 /* -------------------------------------------------------------------------- ** 00208 * Macros and constants. 00209 * 00210 * General purpose: 00211 * ubi_trTRUE - Boolean TRUE. 00212 * ubi_trFALSE - Boolean FALSE. 00213 * 00214 * Flags used in the tree header: 00215 * ubi_trOVERWRITE - This flag indicates that an existing node may be 00216 * overwritten by a new node with a matching key. 00217 * ubi_trDUPKEY - This flag indicates that the tree allows duplicate 00218 * keys. If the tree does allow duplicates, the 00219 * overwrite flag is ignored. 00220 * 00221 * Node link array index constants: (Each node has an array of three 00222 * pointers. One to the left, one to the right, and one back to the 00223 * parent.) 00224 * ubi_trLEFT - Left child pointer. 00225 * ubi_trPARENT - Parent pointer. 00226 * ubi_trRIGHT - Right child pointer. 00227 * ubi_trEQUAL - Synonym for PARENT. 00228 * 00229 * ubi_trCompOps: These values are used in the ubi_trLocate() function. 00230 * ubi_trLT - request the first instance of the greatest key less than 00231 * the search key. 00232 * ubi_trLE - request the first instance of the greatest key that is less 00233 * than or equal to the search key. 00234 * ubi_trEQ - request the first instance of key that is equal to the 00235 * search key. 00236 * ubi_trGE - request the first instance of a key that is greater than 00237 * or equal to the search key. 00238 * ubi_trGT - request the first instance of the first key that is greater 00239 * than the search key. 00240 * -------------------------------------------------------------------------- ** 00241 */ 00242 00243 #define ubi_trTRUE 0xFF 00244 #define ubi_trFALSE 0x00 00245 00246 #define ubi_trOVERWRITE 0x01 /* Turn on allow overwrite */ 00247 #define ubi_trDUPKEY 0x02 /* Turn on allow duplicate keys */ 00248 00249 /* Pointer array index constants... */ 00250 #define ubi_trLEFT 0x00 00251 #define ubi_trPARENT 0x01 00252 #define ubi_trRIGHT 0x02 00253 #define ubi_trEQUAL ubi_trPARENT 00254 00255 typedef enum { 00256 ubi_trLT = 1, 00257 ubi_trLE, 00258 ubi_trEQ, 00259 ubi_trGE, 00260 ubi_trGT 00261 } ubi_trCompOps; 00262 00263 /* -------------------------------------------------------------------------- ** 00264 * These three macros allow simple manipulation of pointer index values (LEFT, 00265 * RIGHT, and PARENT). 00266 * 00267 * Normalize() - converts {LEFT, PARENT, RIGHT} into {-1, 0 ,1}. C 00268 * uses {negative, zero, positive} values to indicate 00269 * {less than, equal to, greater than}. 00270 * AbNormal() - converts {negative, zero, positive} to {LEFT, PARENT, 00271 * RIGHT} (opposite of Normalize()). Note: C comparison 00272 * functions, such as strcmp(), return {negative, zero, 00273 * positive} values, which are not necessarily {-1, 0, 00274 * 1}. This macro uses the the ubi_btSgn() function to 00275 * compensate. 00276 * RevWay() - converts LEFT to RIGHT and RIGHT to LEFT. PARENT (EQUAL) 00277 * is left as is. 00278 * -------------------------------------------------------------------------- ** 00279 */ 00280 #define ubi_trNormalize(W) ((char)( (W) - ubi_trEQUAL )) 00281 #define ubi_trAbNormal(W) ((char)( ((char)ubi_btSgn( (long)(W) )) \ 00282 + ubi_trEQUAL )) 00283 #define ubi_trRevWay(W) ((char)( ubi_trEQUAL - ((W) - ubi_trEQUAL) )) 00284 00285 /* -------------------------------------------------------------------------- ** 00286 * These macros allow us to quickly read the values of the OVERWRITE and 00287 * DUPlicate KEY bits of the tree root flags field. 00288 * -------------------------------------------------------------------------- ** 00289 */ 00290 #define ubi_trDups_OK(A) \ 00291 ((ubi_trDUPKEY & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE)) 00292 #define ubi_trOvwt_OK(A) \ 00293 ((ubi_trOVERWRITE & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE)) 00294 00295 /* -------------------------------------------------------------------------- ** 00296 * Additional Macros... 00297 * 00298 * ubi_trCount() - Given a pointer to a tree root, this macro returns the 00299 * number of nodes currently in the tree. 00300 * 00301 * ubi_trNewTree() - This macro makes it easy to declare and initialize a 00302 * tree header in one step. The line 00303 * 00304 * static ubi_trNewTree( MyTree, cmpfn, ubi_trDUPKEY ); 00305 * 00306 * is equivalent to 00307 * 00308 * static ubi_trRoot MyTree[1] 00309 * = {{ NULL, cmpfn, 0, ubi_trDUPKEY }}; 00310 * 00311 * -------------------------------------------------------------------------- ** 00312 */ 00313 00314 #define ubi_trCount( R ) (((ubi_trRootPtr)(R))->count) 00315 00316 #define ubi_trNewTree( N, C, F ) ubi_trRoot (N)[1] = {{ NULL, (C), 0, (F) }} 00317 00318 /* -------------------------------------------------------------------------- ** 00319 * Typedefs... 00320 * 00321 * ubi_trBool - Your typcial true or false... 00322 * 00323 * Item Pointer: The ubi_btItemPtr is a generic pointer. It is used to 00324 * indicate a key that is being searched for within the tree. 00325 * Searching occurs whenever the ubi_trFind(), ubi_trLocate(), 00326 * or ubi_trInsert() functions are called. 00327 * -------------------------------------------------------------------------- ** 00328 */ 00329 00330 typedef unsigned char ubi_trBool; 00331 00332 typedef unsigned char **ubi_btItemPtr; /* A pointer to key data within a node. */ 00333 00334 /* ------------------------------------------------------------------------- ** 00335 * Binary Tree Node Structure: This structure defines the basic elements of 00336 * the tree nodes. In general you *SHOULD NOT PLAY WITH THESE FIELDS*! 00337 * But, of course, I have to put the structure into this header so that 00338 * you can use it as a building block. 00339 * 00340 * The fields are as follows: 00341 * Link - an array of pointers. These pointers are manipulated by 00342 * the BT routines. The pointers indicate the left and right 00343 * child nodes and the parent node. By keeping track of the 00344 * parent pointer, we avoid the need for recursive routines or 00345 * hand-tooled stacks to keep track of our path back to the 00346 * root. The use of these pointers is subject to change without 00347 * notice. 00348 * gender - a one-byte field indicating whether the node is the RIGHT or 00349 * LEFT child of its parent. If the node is the root of the 00350 * tree, gender will be PARENT. 00351 * balance - only used by the AVL tree module. This field indicates 00352 * the height balance at a given node. See ubi_AVLtree for 00353 * details. 00354 * 00355 * ------------------------------------------------------------------------- ** 00356 */ 00357 typedef struct ubi_btNodeStruct { 00358 struct ubi_btNodeStruct *Link[ 3 ]; 00359 char gender; 00360 char balance; 00361 unsigned char * code_begin; 00362 unsigned char * code_end; 00363 Psc code_psc; 00364 int i_count; 00365 } ubi_btNode; 00366 00367 typedef ubi_btNode *ubi_btNodePtr; /* Pointer to an ubi_btNode structure. */ 00368 00369 /* ------------------------------------------------------------------------- ** 00370 * The next three typedefs define standard function types used by the binary 00371 * tree management routines. In particular: 00372 * 00373 * ubi_btCompFunc is a pointer to a comparison function. Comparison 00374 * functions are passed an ubi_btItemPtr and an 00375 * ubi_btNodePtr. They return a value that is (<0), 0, 00376 * or (>0) to indicate that the Item is (respectively) 00377 * "less than", "equal to", or "greater than" the Item 00378 * contained within the node. (See ubi_btInitTree()). 00379 * ubi_btActionRtn is a pointer to a function that may be called for each 00380 * node visited when performing a tree traversal (see 00381 * ubi_btTraverse()). The function will be passed two 00382 * parameters: the first is a pointer to a node in the 00383 * tree, the second is a generic pointer that may point to 00384 * anything that you like. 00385 * ubi_btKillNodeRtn is a pointer to a function that will deallocate the 00386 * memory used by a node (see ubi_btKillTree()). Since 00387 * memory management is left up to you, deallocation may 00388 * mean anything that you want it to mean. Just remember 00389 * that the tree *will* be destroyed and that none of the 00390 * node pointers will be valid any more. 00391 * ------------------------------------------------------------------------- ** 00392 */ 00393 00394 typedef int (*ubi_btCompFunc)( ubi_btItemPtr, ubi_btNodePtr ); 00395 00396 typedef void (*ubi_btActionRtn)( ubi_btNodePtr, void * ); 00397 00398 typedef void (*ubi_btKillNodeRtn)( ubi_btNodePtr ); 00399 00400 /* -------------------------------------------------------------------------- ** 00401 * Tree Root Structure: This structure gives us a convenient handle for 00402 * accessing whole binary trees. The fields are: 00403 * root - A pointer to the root node of the tree. 00404 * count - A count of the number of nodes stored in the tree. 00405 * cmp - A pointer to the comparison routine to be used when building or 00406 * searching the tree. 00407 * flags - A set of bit flags. Two flags are currently defined: 00408 * 00409 * ubi_trOVERWRITE - If set, this flag indicates that a new node should 00410 * (bit 0x01) overwrite an old node if the two have identical 00411 * keys (ie., the keys are equal). 00412 * ubi_trDUPKEY - If set, this flag indicates that the tree is 00413 * (bit 0x02) allowed to contain nodes with duplicate keys. 00414 * 00415 * NOTE: ubi_trInsert() tests ubi_trDUPKEY before ubi_trOVERWRITE. 00416 * 00417 * All of these values are set when you initialize the root structure by 00418 * calling ubi_trInitTree(). 00419 * -------------------------------------------------------------------------- ** 00420 */ 00421 00422 typedef struct { 00423 ubi_btNodePtr root; /* A pointer to the root node of the tree */ 00424 ubi_btCompFunc cmp; /* A pointer to the tree's comparison function */ 00425 unsigned long count; /* A count of the number of nodes in the tree */ 00426 char flags; /* Overwrite Y|N, Duplicate keys Y|N... */ 00427 } ubi_btRoot; 00428 00429 typedef ubi_btRoot *ubi_btRootPtr; /* Pointer to an ubi_btRoot structure. */ 00430 00431 00432 /* -------------------------------------------------------------------------- ** 00433 * Function Prototypes. 00434 */ 00435 00436 long ubi_btSgn( long x ); 00437 /* ------------------------------------------------------------------------ ** 00438 * Return the sign of x; {negative,zero,positive} ==> {-1, 0, 1}. 00439 * 00440 * Input: x - a signed long integer value. 00441 * 00442 * Output: the "sign" of x, represented as follows: 00443 * -1 == negative 00444 * 0 == zero (no sign) 00445 * 1 == positive 00446 * 00447 * Note: This utility is provided in order to facilitate the conversion 00448 * of C comparison function return values into BinTree direction 00449 * values: {LEFT, PARENT, EQUAL}. It is INCORPORATED into the 00450 * AbNormal() conversion macro! 00451 * 00452 * ------------------------------------------------------------------------ ** 00453 */ 00454 00455 ubi_btNodePtr ubi_btInitNode( ubi_btNodePtr uNodePtr ); 00456 /* ------------------------------------------------------------------------ ** 00457 * Initialize a tree node. 00458 * 00459 * Input: a pointer to a ubi_btNode structure to be initialized. 00460 * Output: a pointer to the initialized ubi_btNode structure (ie. the 00461 * same as the input pointer). 00462 * ------------------------------------------------------------------------ ** 00463 */ 00464 00465 ubi_btRootPtr ubi_btInitTree( ubi_btRootPtr RootPtr, 00466 ubi_btCompFunc CompFunc, 00467 char Flags ); 00468 /* ------------------------------------------------------------------------ ** 00469 * Initialize the fields of a Tree Root header structure. 00470 * 00471 * Input: RootPtr - a pointer to an ubi_btRoot structure to be 00472 * initialized. 00473 * CompFunc - a pointer to a comparison function that will be used 00474 * whenever nodes in the tree must be compared against 00475 * outside values. 00476 * Flags - One bytes worth of flags. Flags include 00477 * ubi_trOVERWRITE and ubi_trDUPKEY. See the header 00478 * file for more info. 00479 * 00480 * Output: a pointer to the initialized ubi_btRoot structure (ie. the 00481 * same value as RootPtr). 00482 * 00483 * Note: The interface to this function has changed from that of 00484 * previous versions. The <Flags> parameter replaces two 00485 * boolean parameters that had the same basic effect. 00486 * ------------------------------------------------------------------------ ** 00487 */ 00488 00489 ubi_trBool ubi_btInsert( ubi_btRootPtr RootPtr, 00490 ubi_btNodePtr NewNode, 00491 ubi_btItemPtr ItemPtr, 00492 ubi_btNodePtr *OldNode ); 00493 /* ------------------------------------------------------------------------ ** 00494 * This function uses a non-recursive algorithm to add a new element to the 00495 * tree. 00496 * 00497 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates 00498 * the root of the tree to which NewNode is to be added. 00499 * NewNode - a pointer to an ubi_btNode structure that is NOT 00500 * part of any tree. 00501 * ItemPtr - A pointer to the sort key that is stored within 00502 * *NewNode. ItemPtr MUST point to information stored 00503 * in *NewNode or an EXACT DUPLICATE. The key data 00504 * indicated by ItemPtr is used to place the new node 00505 * into the tree. 00506 * OldNode - a pointer to an ubi_btNodePtr. When searching 00507 * the tree, a duplicate node may be found. If 00508 * duplicates are allowed, then the new node will 00509 * be simply placed into the tree. If duplicates 00510 * are not allowed, however, then one of two things 00511 * may happen. 00512 * 1) if overwritting *is not* allowed, this 00513 * function will return FALSE (indicating that 00514 * the new node could not be inserted), and 00515 * *OldNode will point to the duplicate that is 00516 * still in the tree. 00517 * 2) if overwritting *is* allowed, then this 00518 * function will swap **OldNode for *NewNode. 00519 * In this case, *OldNode will point to the node 00520 * that was removed (thus allowing you to free 00521 * the node). 00522 * ** If you are using overwrite mode, ALWAYS ** 00523 * ** check the return value of this parameter! ** 00524 * Note: You may pass NULL in this parameter, the 00525 * function knows how to cope. If you do this, 00526 * however, there will be no way to return a 00527 * pointer to an old (ie. replaced) node (which is 00528 * a problem if you are using overwrite mode). 00529 * 00530 * Output: a boolean value indicating success or failure. The function 00531 * will return FALSE if the node could not be added to the tree. 00532 * Such failure will only occur if duplicates are not allowed, 00533 * nodes cannot be overwritten, AND a duplicate key was found 00534 * within the tree. 00535 * ------------------------------------------------------------------------ ** 00536 */ 00537 00538 ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr, 00539 ubi_btNodePtr DeadNode ); 00540 /* ------------------------------------------------------------------------ ** 00541 * This function removes the indicated node from the tree. 00542 * 00543 * Input: RootPtr - A pointer to the header of the tree that contains 00544 * the node to be removed. 00545 * DeadNode - A pointer to the node that will be removed. 00546 * 00547 * Output: This function returns a pointer to the node that was removed 00548 * from the tree (ie. the same as DeadNode). 00549 * 00550 * Note: The node MUST be in the tree indicated by RootPtr. If not, 00551 * strange and evil things will happen to your trees. 00552 * ------------------------------------------------------------------------ ** 00553 */ 00554 00555 ubi_btNodePtr ubi_btLocate( ubi_btRootPtr RootPtr, 00556 ubi_btItemPtr FindMe, 00557 ubi_trCompOps CompOp ); 00558 /* ------------------------------------------------------------------------ ** 00559 * The purpose of ubi_btLocate() is to find a node or set of nodes given 00560 * a target value and a "comparison operator". The Locate() function is 00561 * more flexible and (in the case of trees that may contain dupicate keys) 00562 * more precise than the ubi_btFind() function. The latter is faster, 00563 * but it only searches for exact matches and, if the tree contains 00564 * duplicates, Find() may return a pointer to any one of the duplicate- 00565 * keyed records. 00566 * 00567 * Input: 00568 * RootPtr - A pointer to the header of the tree to be searched. 00569 * FindMe - An ubi_btItemPtr that indicates the key for which to 00570 * search. 00571 * CompOp - One of the following: 00572 * CompOp Return a pointer to the node with 00573 * ------ --------------------------------- 00574 * ubi_trLT - the last key value that is less 00575 * than FindMe. 00576 * ubi_trLE - the first key matching FindMe, or 00577 * the last key that is less than 00578 * FindMe. 00579 * ubi_trEQ - the first key matching FindMe. 00580 * ubi_trGE - the first key matching FindMe, or the 00581 * first key greater than FindMe. 00582 * ubi_trGT - the first key greater than FindMe. 00583 * Output: 00584 * A pointer to the node matching the criteria listed above under 00585 * CompOp, or NULL if no node matched the criteria. 00586 * 00587 * Notes: 00588 * In the case of trees with duplicate keys, Locate() will behave as 00589 * follows: 00590 * 00591 * Find: 3 Find: 3 00592 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6 00593 * ^ ^ ^ ^ ^ 00594 * LT EQ GT LE GE 00595 * 00596 * That is, when returning a pointer to a node with a key that is LESS 00597 * THAN the target key (FindMe), Locate() will return a pointer to the 00598 * LAST matching node. 00599 * When returning a pointer to a node with a key that is GREATER 00600 * THAN the target key (FindMe), Locate() will return a pointer to the 00601 * FIRST matching node. 00602 * 00603 * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf(). 00604 * ------------------------------------------------------------------------ ** 00605 */ 00606 00607 ubi_btNodePtr ubi_btFind( ubi_btRootPtr RootPtr, 00608 ubi_btItemPtr FindMe ); 00609 /* ------------------------------------------------------------------------ ** 00610 * This function performs a non-recursive search of a tree for any node 00611 * matching a specific key. 00612 * 00613 * Input: 00614 * RootPtr - a pointer to the header of the tree to be searched. 00615 * FindMe - a pointer to the key value for which to search. 00616 * 00617 * Output: 00618 * A pointer to a node with a key that matches the key indicated by 00619 * FindMe, or NULL if no such node was found. 00620 * 00621 * Note: In a tree that allows duplicates, the pointer returned *might 00622 * not* point to the (sequentially) first occurance of the 00623 * desired key. In such a tree, it may be more useful to use 00624 * ubi_btLocate(). 00625 * ------------------------------------------------------------------------ ** 00626 */ 00627 00628 ubi_btNodePtr ubi_btNext( ubi_btNodePtr Pr ); 00629 /* ------------------------------------------------------------------------ ** 00630 * Given the node indicated by P, find the (sorted order) Next node in the 00631 * tree. 00632 * Input: P - a pointer to a node that exists in a binary tree. 00633 * Output: A pointer to the "next" node in the tree, or NULL if P pointed 00634 * to the "last" node in the tree or was NULL. 00635 * ------------------------------------------------------------------------ ** 00636 */ 00637 00638 ubi_btNodePtr ubi_btPrev( ubi_btNodePtr Pr ); 00639 /* ------------------------------------------------------------------------ ** 00640 * Given the node indicated by P, find the (sorted order) Previous node in 00641 * the tree. 00642 * Input: P - a pointer to a node that exists in a binary tree. 00643 * Output: A pointer to the "previous" node in the tree, or NULL if P 00644 * pointed to the "first" node in the tree or was NULL. 00645 * ------------------------------------------------------------------------ ** 00646 */ 00647 00648 ubi_btNodePtr ubi_btFirst( ubi_btNodePtr Pr ); 00649 /* ------------------------------------------------------------------------ ** 00650 * Given the node indicated by P, find the (sorted order) First node in the 00651 * subtree of which *P is the root. 00652 * Input: P - a pointer to a node that exists in a binary tree. 00653 * Output: A pointer to the "first" node in a subtree that has *P as its 00654 * root. This function will return NULL only if P is NULL. 00655 * Note: In general, you will be passing in the value of the root field 00656 * of an ubi_btRoot structure. 00657 * ------------------------------------------------------------------------ ** 00658 */ 00659 00660 ubi_btNodePtr ubi_btLast( ubi_btNodePtr Pr ); 00661 /* ------------------------------------------------------------------------ ** 00662 * Given the node indicated by P, find the (sorted order) Last node in the 00663 * subtree of which *P is the root. 00664 * Input: P - a pointer to a node that exists in a binary tree. 00665 * Output: A pointer to the "last" node in a subtree that has *P as its 00666 * root. This function will return NULL only if P is NULL. 00667 * Note: In general, you will be passing in the value of the root field 00668 * of an ubi_btRoot structure. 00669 * ------------------------------------------------------------------------ ** 00670 */ 00671 00672 ubi_btNodePtr ubi_btFirstOf( ubi_btRootPtr RootPtr, 00673 ubi_btItemPtr MatchMe, 00674 ubi_btNodePtr p ); 00675 /* ------------------------------------------------------------------------ ** 00676 * Given a tree that a allows duplicate keys, and a pointer to a node in 00677 * the tree, this function will return a pointer to the first (traversal 00678 * order) node with the same key value. 00679 * 00680 * Input: RootPtr - A pointer to the root of the tree. 00681 * MatchMe - A pointer to the key value. This should probably 00682 * point to the key within node *p. 00683 * p - A pointer to a node in the tree. 00684 * Output: A pointer to the first node in the set of nodes with keys 00685 * matching <FindMe>. 00686 * Notes: Node *p MUST be in the set of nodes with keys matching 00687 * <FindMe>. If not, this function will return NULL. 00688 * 00689 * 4.7: Bug found & fixed by Massimo Campostrini, 00690 * Istituto Nazionale di Fisica Nucleare, Sezione di Pisa. 00691 * 00692 * ------------------------------------------------------------------------ ** 00693 */ 00694 00695 ubi_btNodePtr ubi_btLastOf( ubi_btRootPtr RootPtr, 00696 ubi_btItemPtr MatchMe, 00697 ubi_btNodePtr p ); 00698 /* ------------------------------------------------------------------------ ** 00699 * Given a tree that a allows duplicate keys, and a pointer to a node in 00700 * the tree, this function will return a pointer to the last (traversal 00701 * order) node with the same key value. 00702 * 00703 * Input: RootPtr - A pointer to the root of the tree. 00704 * MatchMe - A pointer to the key value. This should probably 00705 * point to the key within node *p. 00706 * p - A pointer to a node in the tree. 00707 * Output: A pointer to the last node in the set of nodes with keys 00708 * matching <FindMe>. 00709 * Notes: Node *p MUST be in the set of nodes with keys matching 00710 * <FindMe>. If not, this function will return NULL. 00711 * 00712 * 4.7: Bug found & fixed by Massimo Campostrini, 00713 * Istituto Nazionale di Fisica Nucleare, Sezione di Pisa. 00714 * 00715 * ------------------------------------------------------------------------ ** 00716 */ 00717 00718 unsigned long ubi_btTraverse( ubi_btRootPtr RootPtr, 00719 ubi_btActionRtn EachNode, 00720 void *UserData ); 00721 /* ------------------------------------------------------------------------ ** 00722 * Traverse a tree in sorted order (non-recursively). At each node, call 00723 * (*EachNode)(), passing a pointer to the current node, and UserData as the 00724 * second parameter. 00725 * 00726 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates 00727 * the tree to be traversed. 00728 * EachNode - a pointer to a function to be called at each node 00729 * as the node is visited. 00730 * UserData - a generic pointer that may point to anything that 00731 * you choose. 00732 * 00733 * Output: A count of the number of nodes visited. This will be zero 00734 * if the tree is empty. 00735 * 00736 * ------------------------------------------------------------------------ ** 00737 */ 00738 00739 00740 unsigned long ubi_btKillTree( ubi_btRootPtr RootPtr, 00741 ubi_btKillNodeRtn FreeNode ); 00742 /* ------------------------------------------------------------------------ ** 00743 * Delete an entire tree (non-recursively) and reinitialize the ubi_btRoot 00744 * structure. Return a count of the number of nodes deleted. 00745 * 00746 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates 00747 * the root of the tree to delete. 00748 * FreeNode - a function that will be called for each node in the 00749 * tree to deallocate the memory used by the node. 00750 * 00751 * Output: The number of nodes removed from the tree. 00752 * A value of 0 will be returned if: 00753 * - The tree actually contains 0 entries. 00754 * - the value of <RootPtr> is NULL, in which case the tree is 00755 * assumed to be empty 00756 * - the value of <FreeNode> is NULL, in which case entries 00757 * cannot be removed, so 0 is returned. *Make sure that you 00758 * provide a valid value for <FreeNode>*. 00759 * In all other cases, you should get a positive value equal to 00760 * the value of RootPtr->count upon entry. 00761 * 00762 * ------------------------------------------------------------------------ ** 00763 */ 00764 00765 ubi_btNodePtr ubi_btLeafNode( ubi_btNodePtr leader ); 00766 /* ------------------------------------------------------------------------ ** 00767 * Returns a pointer to a leaf node. 00768 * 00769 * Input: leader - Pointer to a node at which to start the descent. 00770 * 00771 * Output: A pointer to a leaf node selected in a somewhat arbitrary 00772 * manner. 00773 * 00774 * Notes: I wrote this function because I was using splay trees as a 00775 * database cache. The cache had a maximum size on it, and I 00776 * needed a way of choosing a node to sacrifice if the cache 00777 * became full. In a splay tree, less recently accessed nodes 00778 * tend toward the bottom of the tree, meaning that leaf nodes 00779 * are good candidates for removal. (I really can't think of 00780 * any other reason to use this function.) 00781 * + In a simple binary tree or an AVL tree, the most recently 00782 * added nodes tend to be nearer the bottom, making this a *bad* 00783 * way to choose which node to remove from the cache. 00784 * + Randomizing the traversal order is probably a good idea. You 00785 * can improve the randomization of leaf node selection by passing 00786 * in pointers to nodes other than the root node each time. A 00787 * pointer to any node in the tree will do. Of course, if you 00788 * pass a pointer to a leaf node you'll get the same thing back. 00789 * 00790 * ------------------------------------------------------------------------ ** 00791 */ 00792 00793 00794 int ubi_btModuleID( int size, char *list[] ); 00795 /* ------------------------------------------------------------------------ ** 00796 * Returns a set of strings that identify the module. 00797 * 00798 * Input: size - The number of elements in the array <list>. 00799 * list - An array of pointers of type (char *). This array 00800 * should, initially, be empty. This function will fill 00801 * in the array with pointers to strings. 00802 * Output: The number of elements of <list> that were used. If this value 00803 * is less than <size>, the values of the remaining elements are 00804 * not guaranteed. 00805 * 00806 * Notes: Please keep in mind that the pointers returned indicate strings 00807 * stored in static memory. Don't free() them, don't write over 00808 * them, etc. Just read them. 00809 * ------------------------------------------------------------------------ ** 00810 */ 00811 00812 /* -------------------------------------------------------------------------- ** 00813 * Masquarade... 00814 * 00815 * This set of defines allows you to write programs that will use any of the 00816 * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree). 00817 * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by 00818 * including the appropriate module header. 00819 */ 00820 00821 #define ubi_trItemPtr ubi_btItemPtr 00822 00823 #define ubi_trNode ubi_btNode 00824 #define ubi_trNodePtr ubi_btNodePtr 00825 00826 #define ubi_trRoot ubi_btRoot 00827 #define ubi_trRootPtr ubi_btRootPtr 00828 00829 #define ubi_trCompFunc ubi_btCompFunc 00830 #define ubi_trActionRtn ubi_btActionRtn 00831 #define ubi_trKillNodeRtn ubi_btKillNodeRtn 00832 00833 #define ubi_trSgn( x ) ubi_btSgn( x ) 00834 00835 #define ubi_trInitNode( Np ) ubi_btInitNode( (ubi_btNodePtr)(Np) ) 00836 00837 #define ubi_trInitTree( Rp, Cf, Fl ) \ 00838 ubi_btInitTree( (ubi_btRootPtr)(Rp), (ubi_btCompFunc)(Cf), (Fl) ) 00839 00840 #define ubi_trInsert( Rp, Nn, Ip, On ) \ 00841 ubi_btInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \ 00842 (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) ) 00843 00844 #define ubi_trRemove( Rp, Dn ) \ 00845 ubi_btRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) ) 00846 00847 #define ubi_trLocate( Rp, Ip, Op ) \ 00848 ubi_btLocate( (ubi_btRootPtr)(Rp), \ 00849 (ubi_btItemPtr)(Ip), \ 00850 (ubi_trCompOps)(Op) ) 00851 00852 #define ubi_trFind( Rp, Ip ) \ 00853 ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) ) 00854 00855 #define ubi_trNext( P ) ubi_btNext( (ubi_btNodePtr)(P) ) 00856 00857 #define ubi_trPrev( P ) ubi_btPrev( (ubi_btNodePtr)(P) ) 00858 00859 #define ubi_trFirst( P ) ubi_btFirst( (ubi_btNodePtr)(P) ) 00860 00861 #define ubi_trLast( P ) ubi_btLast( (ubi_btNodePtr)(P) ) 00862 00863 #define ubi_trFirstOf( Rp, Ip, P ) \ 00864 ubi_btFirstOf( (ubi_btRootPtr)(Rp), \ 00865 (ubi_btItemPtr)(Ip), \ 00866 (ubi_btNodePtr)(P) ) 00867 00868 #define ubi_trLastOf( Rp, Ip, P ) \ 00869 ubi_btLastOf( (ubi_btRootPtr)(Rp), \ 00870 (ubi_btItemPtr)(Ip), \ 00871 (ubi_btNodePtr)(P) ) 00872 00873 #define ubi_trTraverse( Rp, En, Ud ) \ 00874 ubi_btTraverse((ubi_btRootPtr)(Rp), (ubi_btActionRtn)(En), (void *)(Ud)) 00875 00876 #define ubi_trKillTree( Rp, Fn ) \ 00877 ubi_btKillTree( (ubi_btRootPtr)(Rp), (ubi_btKillNodeRtn)(Fn) ) 00878 00879 #define ubi_trLeafNode( Nd ) \ 00880 ubi_btLeafNode( (ubi_btNodePtr)(Nd) ) 00881 00882 #define ubi_trModuleID( s, l ) ubi_btModuleID( s, l ) 00883 00884 /* ========================================================================== */ 00885 #endif /* UBI_BINTREE_H */