00001 /* ========================================================================== ** 00002 * $Id$ 00003 * 00004 * ubi_BinTree.c 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 00032 /* 00033 * 04 Feb 2005: SAS Updated to add ubi_btCheck function that calls a 00034 * user provided function to find a node in a 00035 * particular tree. 00036 * 00037 */ 00038 00039 #ifdef HAVE_CONFIG_H 00040 #include "config.h" 00041 #endif 00042 00043 #include "ubi_BinTree.h" /* Header for this module. */ 00044 00045 /* ========================================================================== ** 00046 * Static data. 00047 */ 00048 00049 static char ModuleID[] = "ubi_BinTree\n\ 00050 \t$Revision$\n\ 00051 \t$Date$\n\ 00052 \t$Author$\n"; 00053 00054 /* ========================================================================== ** 00055 * Internal (private) functions. 00056 */ 00057 00058 static ubi_btNodePtr qFind( ubi_btCompFunc cmp, 00059 ubi_btItemPtr FindMe, 00060 register ubi_btNodePtr p ) 00061 /* ------------------------------------------------------------------------ ** 00062 * This function performs a non-recursive search of a tree for a node 00063 * matching a specific key. It is called "qFind()" because it is 00064 * faster that TreeFind (below). 00065 * 00066 * Input: 00067 * cmp - a pointer to the tree's comparison function. 00068 * FindMe - a pointer to the key value for which to search. 00069 * p - a pointer to the starting point of the search. <p> 00070 * is considered to be the root of a subtree, and only 00071 * the subtree will be searched. 00072 * 00073 * Output: 00074 * A pointer to a node with a key that matches the key indicated by 00075 * FindMe, or NULL if no such node was found. 00076 * 00077 * Note: In a tree that allows duplicates, the pointer returned *might 00078 * not* point to the (sequentially) first occurance of the 00079 * desired key. 00080 * ------------------------------------------------------------------------ ** 00081 */ 00082 { 00083 int tmp; 00084 00085 while( (NULL != p) 00086 && ((tmp = ubi_trAbNormal( (*cmp)(FindMe, p) )) != ubi_trEQUAL) ) 00087 p = p->Link[tmp]; 00088 00089 return( p ); 00090 } /* qFind */ 00091 00092 static ubi_btNodePtr TreeFind( ubi_btItemPtr findme, 00093 ubi_btNodePtr p, 00094 ubi_btNodePtr *parentp, 00095 char *gender, 00096 ubi_btCompFunc CmpFunc ) 00097 /* ------------------------------------------------------------------------ ** 00098 * TreeFind() searches a tree for a given value (findme). It will return a 00099 * pointer to the target node, if found, or NULL if the target node was not 00100 * found. 00101 * 00102 * TreeFind() also returns, via parameters, a pointer to the parent of the 00103 * target node, and a LEFT or RIGHT value indicating which child of the 00104 * parent is the target node. *If the target is not found*, then these 00105 * values indicate the place at which the target *should be found*. This 00106 * is useful when inserting a new node into a tree or searching for nodes 00107 * "near" the target node. 00108 * 00109 * The parameters are: 00110 * 00111 * findme - is a pointer to the key information to be searched for. 00112 * p - points to the root of the tree to be searched. 00113 * parentp - will return a pointer to a pointer to the !parent! of the 00114 * target node, which can be especially usefull if the target 00115 * was not found. 00116 * gender - returns LEFT or RIGHT to indicate which child of *parentp 00117 * was last searched. 00118 * CmpFunc - points to the comparison function. 00119 * 00120 * This function is called by ubi_btLocate() and ubi_btInsert(). 00121 * ------------------------------------------------------------------------ ** 00122 */ 00123 { 00124 register ubi_btNodePtr tmp_p = p; 00125 ubi_btNodePtr tmp_pp = NULL; 00126 char tmp_gender = ubi_trEQUAL; 00127 int tmp_cmp; 00128 00129 while( (NULL != tmp_p) 00130 && (ubi_trEQUAL != (tmp_cmp = ubi_trAbNormal((*CmpFunc)(findme, tmp_p)))) ) 00131 { 00132 tmp_pp = tmp_p; /* Keep track of previous node. */ 00133 tmp_gender = (char)tmp_cmp; /* Keep track of sex of child. */ 00134 tmp_p = tmp_p->Link[tmp_cmp]; /* Go to child. */ 00135 } 00136 *parentp = tmp_pp; /* Return results. */ 00137 *gender = tmp_gender; 00138 return( tmp_p ); 00139 } /* TreeFind */ 00140 00141 static void ReplaceNode( ubi_btNodePtr *parent, 00142 ubi_btNodePtr oldnode, 00143 ubi_btNodePtr newnode ) 00144 /* ------------------------------------------------------------------------ ** 00145 * Remove node oldnode from the tree, replacing it with node newnode. 00146 * 00147 * Input: 00148 * parent - A pointer to he parent pointer of the node to be 00149 * replaced. <parent> may point to the Link[] field of 00150 * a parent node, or it may indicate the root pointer at 00151 * the top of the tree. 00152 * oldnode - A pointer to the node that is to be replaced. 00153 * newnode - A pointer to the node that is to be installed in the 00154 * place of <*oldnode>. 00155 * 00156 * Notes: Don't forget to free oldnode. 00157 * Also, this function used to have a really nasty typo 00158 * bug. "oldnode" and "newnode" were swapped in the line 00159 * that now reads: 00160 * ((unsigned char *)newnode)[i] = ((unsigned char *)oldnode)[i]; 00161 * Bleah! 00162 * ------------------------------------------------------------------------ ** 00163 */ 00164 { 00165 *newnode = *oldnode; /* Copy node internals to new node. */ 00166 00167 (*parent) = newnode; /* Old node's parent points to new child. */ 00168 /* Now tell the children about their new step-parent. */ 00169 if( oldnode->Link[ubi_trLEFT] ) 00170 (oldnode->Link[ubi_trLEFT])->Link[ubi_trPARENT] = newnode; 00171 if( oldnode->Link[ubi_trRIGHT] ) 00172 (oldnode->Link[ubi_trRIGHT])->Link[ubi_trPARENT] = newnode; 00173 } /* ReplaceNode */ 00174 00175 static void SwapNodes( ubi_btRootPtr RootPtr, 00176 ubi_btNodePtr Node1, 00177 ubi_btNodePtr Node2 ) 00178 /* ------------------------------------------------------------------------ ** 00179 * This function swaps two nodes in the tree. Node1 will take the place of 00180 * Node2, and Node2 will fill in the space left vacant by Node 1. 00181 * 00182 * Input: 00183 * RootPtr - pointer to the tree header structure for this tree. 00184 * Node1 - \ 00185 * > These are the two nodes which are to be swapped. 00186 * Node2 - / 00187 * 00188 * Notes: 00189 * This function does a three step swap, using a dummy node as a place 00190 * holder. This function is used by ubi_btRemove(). 00191 * ------------------------------------------------------------------------ ** 00192 */ 00193 { 00194 ubi_btNodePtr *Parent; 00195 ubi_btNode dummy; 00196 ubi_btNodePtr dummy_p = &dummy; 00197 00198 /* Replace Node 1 with the dummy, thus removing Node1 from the tree. */ 00199 if( NULL != Node1->Link[ubi_trPARENT] ) 00200 Parent = &((Node1->Link[ubi_trPARENT])->Link[(int)(Node1->gender)]); 00201 else 00202 Parent = &(RootPtr->root); 00203 ReplaceNode( Parent, Node1, dummy_p ); 00204 00205 /* Swap Node 1 with Node 2, placing Node 1 back into the tree. */ 00206 if( NULL != Node2->Link[ubi_trPARENT] ) 00207 Parent = &((Node2->Link[ubi_trPARENT])->Link[(int)(Node2->gender)]); 00208 else 00209 Parent = &(RootPtr->root); 00210 ReplaceNode( Parent, Node2, Node1 ); 00211 00212 /* Swap Node 2 and the dummy, thus placing Node 2 back into the tree. */ 00213 if( NULL != dummy_p->Link[ubi_trPARENT] ) 00214 Parent = &((dummy_p->Link[ubi_trPARENT])->Link[(int)(dummy_p->gender)]); 00215 else 00216 Parent = &(RootPtr->root); 00217 ReplaceNode( Parent, dummy_p, Node2 ); 00218 } /* SwapNodes */ 00219 00220 /* -------------------------------------------------------------------------- ** 00221 * These routines allow you to walk through the tree, forwards or backwards. 00222 */ 00223 00224 static ubi_btNodePtr SubSlide( register ubi_btNodePtr P, 00225 register int whichway ) 00226 /* ------------------------------------------------------------------------ ** 00227 * Slide down the side of a subtree. 00228 * 00229 * Given a starting node, this function returns a pointer to the LEFT-, or 00230 * RIGHT-most descendent, *or* (if whichway is PARENT) to the tree root. 00231 * 00232 * Input: P - a pointer to a starting place. 00233 * whichway - the direction (LEFT, RIGHT, or PARENT) in which to 00234 * travel. 00235 * Output: A pointer to a node that is either the root, or has no 00236 * whichway-th child but is within the subtree of P. Note that 00237 * the return value may be the same as P. The return value *will 00238 * be* NULL if P is NULL. 00239 * ------------------------------------------------------------------------ ** 00240 */ 00241 { 00242 00243 if( NULL != P ) 00244 while( NULL != P->Link[ whichway ] ) 00245 P = P->Link[ whichway ]; 00246 return( P ); 00247 } /* SubSlide */ 00248 00249 static ubi_btNodePtr Neighbor( register ubi_btNodePtr P, 00250 register int whichway ) 00251 /* ------------------------------------------------------------------------ ** 00252 * Given starting point p, return the (key order) next or preceeding node 00253 * in the tree. 00254 * 00255 * Input: P - Pointer to our starting place node. 00256 * whichway - the direction in which to travel to find the 00257 * neighbor, i.e., the RIGHT neighbor or the LEFT 00258 * neighbor. 00259 * 00260 * Output: A pointer to the neighboring node, or NULL if P was NULL. 00261 * 00262 * Notes: If whichway is PARENT, the results are unpredictable. 00263 * ------------------------------------------------------------------------ ** 00264 */ 00265 { 00266 if( P ) 00267 { 00268 if( NULL != P->Link[ whichway ] ) 00269 return( SubSlide( P->Link[ whichway ], (char)ubi_trRevWay(whichway) ) ); 00270 else 00271 while( NULL != P->Link[ ubi_trPARENT ] ) 00272 { 00273 if( whichway == P->gender ) 00274 P = P->Link[ ubi_trPARENT ]; 00275 else 00276 return( P->Link[ ubi_trPARENT ] ); 00277 } 00278 } 00279 return( NULL ); 00280 } /* Neighbor */ 00281 00282 static ubi_btNodePtr Border( ubi_btRootPtr RootPtr, 00283 ubi_btItemPtr FindMe, 00284 ubi_btNodePtr p, 00285 int whichway ) 00286 /* ------------------------------------------------------------------------ ** 00287 * Given starting point p, which has a key value equal to *FindMe, locate 00288 * the first (index order) node with the same key value. 00289 * 00290 * This function is useful in trees that have can have duplicate keys. 00291 * For example, consider the following tree: 00292 * Tree Traversal 00293 * 2 If <p> points to the root and <whichway> is RIGHT, 3 00294 * / \ then the return value will be a pointer to the / \ 00295 * 2 2 RIGHT child of the root node. The tree on 2 5 00296 * / / \ the right shows the order of traversal. / / \ 00297 * 1 2 3 1 4 6 00298 * 00299 * Input: RootPtr - Pointer to the tree root structure. 00300 * FindMe - Key value for comparisons. 00301 * p - Pointer to the starting-point node. 00302 * whichway - the direction in which to travel to find the 00303 * neighbor, i.e., the RIGHT neighbor or the LEFT 00304 * neighbor. 00305 * 00306 * Output: A pointer to the first (index, or "traversal", order) node with 00307 * a Key value that matches *FindMe. 00308 * 00309 * Notes: If whichway is PARENT, or if the tree does not allow duplicate 00310 * keys, this function will return <p>. 00311 * ------------------------------------------------------------------------ ** 00312 */ 00313 { 00314 register ubi_btNodePtr q; 00315 00316 /* Exit if there's nothing that can be done. */ 00317 if( !ubi_trDups_OK( RootPtr ) || (ubi_trPARENT == whichway) ) 00318 return( p ); 00319 00320 /* First, if needed, move up the tree. We need to get to the root of the 00321 * subtree that contains all of the matching nodes. 00322 */ 00323 q = p->Link[ubi_trPARENT]; 00324 while( (NULL != q) 00325 && (ubi_trEQUAL == ubi_trAbNormal( (*(RootPtr->cmp))(FindMe, q) )) ) 00326 { 00327 p = q; 00328 q = p->Link[ubi_trPARENT]; 00329 } 00330 00331 /* Next, move back down in the "whichway" direction. */ 00332 q = p->Link[whichway]; 00333 while( NULL != q ) 00334 { 00335 q = qFind( RootPtr->cmp, FindMe, q ); 00336 if( q ) 00337 { 00338 p = q; 00339 q = p->Link[whichway]; 00340 } 00341 } 00342 return( p ); 00343 } /* Border */ 00344 00345 00346 /* ========================================================================== ** 00347 * Exported utilities. 00348 */ 00349 00350 long ubi_btSgn( register long x ) 00351 /* ------------------------------------------------------------------------ ** 00352 * Return the sign of x; {negative,zero,positive} ==> {-1, 0, 1}. 00353 * 00354 * Input: x - a signed long integer value. 00355 * 00356 * Output: the "sign" of x, represented as follows: 00357 * -1 == negative 00358 * 0 == zero (no sign) 00359 * 1 == positive 00360 * 00361 * Note: This utility is provided in order to facilitate the conversion 00362 * of C comparison function return values into BinTree direction 00363 * values: {LEFT, PARENT, EQUAL}. It is INCORPORATED into the 00364 * ubi_trAbNormal() conversion macro! 00365 * 00366 * ------------------------------------------------------------------------ ** 00367 */ 00368 { 00369 return( (x)?((x>0)?(1):(-1)):(0) ); 00370 } /* ubi_btSgn */ 00371 00372 ubi_btNodePtr ubi_btInitNode( ubi_btNodePtr NodePtr ) 00373 /* ------------------------------------------------------------------------ ** 00374 * Initialize a tree node. 00375 * 00376 * Input: a pointer to a ubi_btNode structure to be initialized. 00377 * Output: a pointer to the initialized ubi_btNode structure (ie. the 00378 * same as the input pointer). 00379 * ------------------------------------------------------------------------ ** 00380 */ 00381 { 00382 NodePtr->Link[ ubi_trLEFT ] = NULL; 00383 NodePtr->Link[ ubi_trPARENT ] = NULL; 00384 NodePtr->Link[ ubi_trRIGHT ] = NULL; 00385 NodePtr->gender = ubi_trEQUAL; 00386 NodePtr->balance = ubi_trEQUAL; 00387 return( NodePtr ); 00388 } /* ubi_btInitNode */ 00389 00390 ubi_btRootPtr ubi_btInitTree( ubi_btRootPtr RootPtr, 00391 ubi_btCompFunc CompFunc, 00392 char Flags ) 00393 /* ------------------------------------------------------------------------ ** 00394 * Initialize the fields of a Tree Root header structure. 00395 * 00396 * Input: RootPtr - a pointer to an ubi_btRoot structure to be 00397 * initialized. 00398 * CompFunc - a pointer to a comparison function that will be used 00399 * whenever nodes in the tree must be compared against 00400 * outside values. 00401 * Flags - One bytes worth of flags. Flags include 00402 * ubi_trOVERWRITE and ubi_trDUPKEY. See the header 00403 * file for more info. 00404 * 00405 * Output: a pointer to the initialized ubi_btRoot structure (ie. the 00406 * same value as RootPtr). 00407 * 00408 * Note: The interface to this function has changed from that of 00409 * previous versions. The <Flags> parameter replaces two 00410 * boolean parameters that had the same basic effect. 00411 * 00412 * ------------------------------------------------------------------------ ** 00413 */ 00414 { 00415 if( RootPtr ) 00416 { 00417 RootPtr->root = NULL; 00418 RootPtr->count = 0L; 00419 RootPtr->cmp = CompFunc; 00420 RootPtr->flags = (Flags & ubi_trDUPKEY) ? ubi_trDUPKEY : Flags; 00421 } /* There are only two supported flags, and they are 00422 * mutually exclusive. ubi_trDUPKEY takes precedence 00423 * over ubi_trOVERWRITE. 00424 */ 00425 return( RootPtr ); 00426 } /* ubi_btInitTree */ 00427 00428 ubi_trBool ubi_btInsert( ubi_btRootPtr RootPtr, 00429 ubi_btNodePtr NewNode, 00430 ubi_btItemPtr ItemPtr, 00431 ubi_btNodePtr *OldNode ) 00432 /* ------------------------------------------------------------------------ ** 00433 * This function uses a non-recursive algorithm to add a new element to the 00434 * tree. 00435 * 00436 * Input: RootPtr - a pointer to the ubi_btRoot structure that indicates 00437 * the root of the tree to which NewNode is to be added. 00438 * NewNode - a pointer to an ubi_btNode structure that is NOT 00439 * part of any tree. 00440 * ItemPtr - A pointer to the sort key that is stored within 00441 * *NewNode. ItemPtr MUST point to information stored 00442 * in *NewNode or an EXACT DUPLICATE. The key data 00443 * indicated by ItemPtr is used to place the new node 00444 * into the tree. 00445 * OldNode - a pointer to an ubi_btNodePtr. When searching 00446 * the tree, a duplicate node may be found. If 00447 * duplicates are allowed, then the new node will 00448 * be simply placed into the tree. If duplicates 00449 * are not allowed, however, then one of two things 00450 * may happen. 00451 * 1) if overwritting *is not* allowed, this 00452 * function will return FALSE (indicating that 00453 * the new node could not be inserted), and 00454 * *OldNode will point to the duplicate that is 00455 * still in the tree. 00456 * 2) if overwritting *is* allowed, then this 00457 * function will swap **OldNode for *NewNode. 00458 * In this case, *OldNode will point to the node 00459 * that was removed (thus allowing you to free 00460 * the node). 00461 * ** If you are using overwrite mode, ALWAYS ** 00462 * ** check the return value of this parameter! ** 00463 * Note: You may pass NULL in this parameter, the 00464 * function knows how to cope. If you do this, 00465 * however, there will be no way to return a 00466 * pointer to an old (ie. replaced) node (which is 00467 * a problem if you are using overwrite mode). 00468 * 00469 * Output: a boolean value indicating success or failure. The function 00470 * will return FALSE if the node could not be added to the tree. 00471 * Such failure will only occur if duplicates are not allowed, 00472 * nodes cannot be overwritten, AND a duplicate key was found 00473 * within the tree. 00474 * ------------------------------------------------------------------------ ** 00475 */ 00476 { 00477 ubi_btNodePtr OtherP, 00478 parent = NULL; 00479 char tmp; 00480 00481 if( NULL == OldNode ) /* If they didn't give us a pointer, supply our own. */ 00482 OldNode = &OtherP; 00483 00484 (void)ubi_btInitNode( NewNode ); /* Init the new node's BinTree fields. */ 00485 00486 /* Find a place for the new node. */ 00487 *OldNode = TreeFind(ItemPtr, (RootPtr->root), &parent, &tmp, (RootPtr->cmp)); 00488 00489 /* Now add the node to the tree... */ 00490 if( NULL == (*OldNode) ) /* The easy one: we have a space for a new node! */ 00491 { 00492 if( NULL == parent ) 00493 RootPtr->root = NewNode; 00494 else 00495 { 00496 parent->Link[(int)tmp] = NewNode; 00497 NewNode->Link[ubi_trPARENT] = parent; 00498 NewNode->gender = tmp; 00499 } 00500 (RootPtr->count)++; 00501 return( ubi_trTRUE ); 00502 } 00503 00504 /* If we reach this point, we know that a duplicate node exists. This 00505 * section adds the node to the tree if duplicate keys are allowed. 00506 */ 00507 if( ubi_trDups_OK(RootPtr) ) /* Key exists, add duplicate */ 00508 { 00509 ubi_btNodePtr q; 00510 00511 tmp = ubi_trRIGHT; 00512 q = (*OldNode); 00513 *OldNode = NULL; 00514 while( NULL != q ) 00515 { 00516 parent = q; 00517 if( tmp == ubi_trEQUAL ) 00518 tmp = ubi_trRIGHT; 00519 q = q->Link[(int)tmp]; 00520 if ( q ) 00521 tmp = ubi_trAbNormal( (*(RootPtr->cmp))(ItemPtr, q) ); 00522 } 00523 parent->Link[(int)tmp] = NewNode; 00524 NewNode->Link[ubi_trPARENT] = parent; 00525 NewNode->gender = tmp; 00526 (RootPtr->count)++; 00527 return( ubi_trTRUE ); 00528 } 00529 00530 /* If we get to *this* point, we know that we are not allowed to have 00531 * duplicate nodes, but our node keys match, so... may we replace the 00532 * old one? 00533 */ 00534 if( ubi_trOvwt_OK(RootPtr) ) /* Key exists, we replace */ 00535 { 00536 if( NULL == parent ) 00537 ReplaceNode( &(RootPtr->root), *OldNode, NewNode ); 00538 else 00539 ReplaceNode( &(parent->Link[(int)((*OldNode)->gender)]), 00540 *OldNode, NewNode ); 00541 return( ubi_trTRUE ); 00542 } 00543 00544 return( ubi_trFALSE ); /* Failure: could not replace an existing node. */ 00545 } /* ubi_btInsert */ 00546 00547 ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr, 00548 ubi_btNodePtr DeadNode ) 00549 /* ------------------------------------------------------------------------ ** 00550 * This function removes the indicated node from the tree. 00551 * 00552 * Input: RootPtr - A pointer to the header of the tree that contains 00553 * the node to be removed. 00554 * DeadNode - A pointer to the node that will be removed. 00555 * 00556 * Output: This function returns a pointer to the node that was removed 00557 * from the tree (ie. the same as DeadNode). 00558 * 00559 * Note: The node MUST be in the tree indicated by RootPtr. If not, 00560 * strange and evil things will happen to your trees. 00561 * ------------------------------------------------------------------------ ** 00562 */ 00563 { 00564 ubi_btNodePtr p, 00565 *parentp; 00566 int tmp; 00567 00568 /* if the node has both left and right subtrees, then we have to swap 00569 * it with another node. The other node we choose will be the Prev()ious 00570 * node, which is garunteed to have no RIGHT child. 00571 */ 00572 if( (NULL != DeadNode->Link[ubi_trLEFT]) 00573 && (NULL != DeadNode->Link[ubi_trRIGHT]) ) 00574 SwapNodes( RootPtr, DeadNode, ubi_btPrev( DeadNode ) ); 00575 00576 /* The parent of the node to be deleted may be another node, or it may be 00577 * the root of the tree. Since we're not sure, it's best just to have 00578 * a pointer to the parent pointer, whatever it is. 00579 */ 00580 if( NULL == DeadNode->Link[ubi_trPARENT] ) 00581 parentp = &( RootPtr->root ); 00582 else 00583 parentp = &((DeadNode->Link[ubi_trPARENT])->Link[(int)(DeadNode->gender)]); 00584 00585 /* Now link the parent to the only grand-child and patch up the gender. */ 00586 tmp = ((DeadNode->Link[ubi_trLEFT])?ubi_trLEFT:ubi_trRIGHT); 00587 00588 p = (DeadNode->Link[tmp]); 00589 if( NULL != p ) 00590 { 00591 p->Link[ubi_trPARENT] = DeadNode->Link[ubi_trPARENT]; 00592 p->gender = DeadNode->gender; 00593 } 00594 (*parentp) = p; 00595 00596 /* Finished, reduce the node count and return. */ 00597 (RootPtr->count)--; 00598 return( DeadNode ); 00599 } /* ubi_btRemove */ 00600 00601 ubi_btNodePtr ubi_btLocate( ubi_btRootPtr RootPtr, 00602 ubi_btItemPtr FindMe, 00603 ubi_trCompOps CompOp ) 00604 /* ------------------------------------------------------------------------ ** 00605 * The purpose of ubi_btLocate() is to find a node or set of nodes given 00606 * a target value and a "comparison operator". The Locate() function is 00607 * more flexible and (in the case of trees that may contain dupicate keys) 00608 * more precise than the ubi_btFind() function. The latter is faster, 00609 * but it only searches for exact matches and, if the tree contains 00610 * duplicates, Find() may return a pointer to any one of the duplicate- 00611 * keyed records. 00612 * 00613 * Input: 00614 * RootPtr - A pointer to the header of the tree to be searched. 00615 * FindMe - An ubi_btItemPtr that indicates the key for which to 00616 * search. 00617 * CompOp - One of the following: 00618 * CompOp Return a pointer to the node with 00619 * ------ --------------------------------- 00620 * ubi_trLT - the last key value that is less 00621 * than FindMe. 00622 * ubi_trLE - the first key matching FindMe, or 00623 * the last key that is less than 00624 * FindMe. 00625 * ubi_trEQ - the first key matching FindMe. 00626 * ubi_trGE - the first key matching FindMe, or the 00627 * first key greater than FindMe. 00628 * ubi_trGT - the first key greater than FindMe. 00629 * Output: 00630 * A pointer to the node matching the criteria listed above under 00631 * CompOp, or NULL if no node matched the criteria. 00632 * 00633 * Notes: 00634 * In the case of trees with duplicate keys, Locate() will behave as 00635 * follows: 00636 * 00637 * Find: 3 Find: 3 00638 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6 00639 * ^ ^ ^ ^ ^ 00640 * LT EQ GT LE GE 00641 * 00642 * That is, when returning a pointer to a node with a key that is LESS 00643 * THAN the target key (FindMe), Locate() will return a pointer to the 00644 * LAST matching node. 00645 * When returning a pointer to a node with a key that is GREATER 00646 * THAN the target key (FindMe), Locate() will return a pointer to the 00647 * FIRST matching node. 00648 * 00649 * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf(). 00650 * ------------------------------------------------------------------------ ** 00651 */ 00652 { 00653 register ubi_btNodePtr p; 00654 ubi_btNodePtr parent; 00655 char whichkid; 00656 00657 /* Start by searching for a matching node. */ 00658 p = TreeFind( FindMe, 00659 RootPtr->root, 00660 &parent, 00661 &whichkid, 00662 RootPtr->cmp ); 00663 00664 if( NULL != p ) /* If we have found a match, we can resolve as follows: */ 00665 { 00666 switch( CompOp ) 00667 { 00668 case ubi_trLT: /* It's just a jump to the left... */ 00669 p = Border( RootPtr, FindMe, p, ubi_trLEFT ); 00670 return( Neighbor( p, ubi_trLEFT ) ); 00671 case ubi_trGT: /* ...and then a jump to the right. */ 00672 p = Border( RootPtr, FindMe, p, ubi_trRIGHT ); 00673 return( Neighbor( p, ubi_trRIGHT ) ); 00674 default: 00675 p = Border( RootPtr, FindMe, p, ubi_trLEFT ); 00676 return( p ); 00677 } 00678 } 00679 00680 /* Else, no match. */ 00681 if( ubi_trEQ == CompOp ) /* If we were looking for an exact match... */ 00682 return( NULL ); /* ...forget it. */ 00683 00684 /* We can still return a valid result for GT, GE, LE, and LT. 00685 * <parent> points to a node with a value that is either just before or 00686 * just after the target value. 00687 * Remaining possibilities are LT and GT (including LE & GE). 00688 */ 00689 if( (ubi_trLT == CompOp) || (ubi_trLE == CompOp) ) 00690 return( (ubi_trLEFT == whichkid) ? Neighbor( parent, whichkid ) : parent ); 00691 else 00692 return( (ubi_trRIGHT == whichkid) ? Neighbor( parent, whichkid ) : parent ); 00693 } /* ubi_btLocate */ 00694 00695 ubi_btNodePtr ubi_btFind( ubi_btRootPtr RootPtr, 00696 ubi_btItemPtr FindMe ) 00697 /* ------------------------------------------------------------------------ ** 00698 * This function performs a non-recursive search of a tree for any node 00699 * matching a specific key. 00700 * 00701 * Input: 00702 * RootPtr - a pointer to the header of the tree to be searched. 00703 * FindMe - a pointer to the key value for which to search. 00704 * 00705 * Output: 00706 * A pointer to a node with a key that matches the key indicated by 00707 * FindMe, or NULL if no such node was found. 00708 * 00709 * Note: In a tree that allows duplicates, the pointer returned *might 00710 * not* point to the (sequentially) first occurance of the 00711 * desired key. In such a tree, it may be more useful to use 00712 * ubi_btLocate(). 00713 * ------------------------------------------------------------------------ ** 00714 */ 00715 { 00716 return( qFind( RootPtr->cmp, FindMe, RootPtr->root ) ); 00717 } /* ubi_btFind */ 00718 00719 int ubi_btCheck( ubi_btRootPtr RootPtr, 00720 ubi_btCheckFunc func, 00721 void *UserData ) 00722 /* ------------------------------------------------------------------------ ** 00723 * This function performs a non-recursive search of a tree checking for any 00724 * node that matches the data supplied, by calling func. 00725 * 00726 * Input: 00727 * RootPtr - a pointer to the header of the tree to be searched. 00728 * func - a pointer function to perform the check 00729 * UserData - a pointer to the user data to pass to func 00730 * 00731 * Output: 00732 * Returns 1 when found, 0 if not found 00733 * 00734 * Note: 00735 * ------------------------------------------------------------------------ ** 00736 */ 00737 { 00738 ubi_btNodePtr p = ubi_btFirst( RootPtr->root ); 00739 00740 while( NULL != p ) 00741 { 00742 if ((*func)( p, UserData )) 00743 return ( 1 ); 00744 p = ubi_btNext( p ); 00745 } 00746 return( 0 ); 00747 } /* ubi_btCheck */ 00748 00749 ubi_btNodePtr ubi_btNext( ubi_btNodePtr P ) 00750 /* ------------------------------------------------------------------------ ** 00751 * Given the node indicated by P, find the (sorted order) Next node in the 00752 * tree. 00753 * Input: P - a pointer to a node that exists in a binary tree. 00754 * Output: A pointer to the "next" node in the tree, or NULL if P pointed 00755 * to the "last" node in the tree or was NULL. 00756 * ------------------------------------------------------------------------ ** 00757 */ 00758 { 00759 return( Neighbor( P, ubi_trRIGHT ) ); 00760 } /* ubi_btNext */ 00761 00762 ubi_btNodePtr ubi_btPrev( ubi_btNodePtr P ) 00763 /* ------------------------------------------------------------------------ ** 00764 * Given the node indicated by P, find the (sorted order) Previous node in 00765 * the tree. 00766 * Input: P - a pointer to a node that exists in a binary tree. 00767 * Output: A pointer to the "previous" node in the tree, or NULL if P 00768 * pointed to the "first" node in the tree or was NULL. 00769 * ------------------------------------------------------------------------ ** 00770 */ 00771 { 00772 return( Neighbor( P, ubi_trLEFT ) ); 00773 } /* ubi_btPrev */ 00774 00775 ubi_btNodePtr ubi_btFirst( ubi_btNodePtr P ) 00776 /* ------------------------------------------------------------------------ ** 00777 * Given the node indicated by P, find the (sorted order) First node in the 00778 * subtree of which *P is the root. 00779 * Input: P - a pointer to a node that exists in a binary tree. 00780 * Output: A pointer to the "first" node in a subtree that has *P as its 00781 * root. This function will return NULL only if P is NULL. 00782 * Note: In general, you will be passing in the value of the root field 00783 * of an ubi_btRoot structure. 00784 * ------------------------------------------------------------------------ ** 00785 */ 00786 { 00787 return( SubSlide( P, ubi_trLEFT ) ); 00788 } /* ubi_btFirst */ 00789 00790 ubi_btNodePtr ubi_btLast( ubi_btNodePtr P ) 00791 /* ------------------------------------------------------------------------ ** 00792 * Given the node indicated by P, find the (sorted order) Last node in the 00793 * subtree of which *P is the root. 00794 * Input: P - a pointer to a node that exists in a binary tree. 00795 * Output: A pointer to the "last" node in a subtree that has *P as its 00796 * root. This function will return NULL only if P is NULL. 00797 * Note: In general, you will be passing in the value of the root field 00798 * of an ubi_btRoot structure. 00799 * ------------------------------------------------------------------------ ** 00800 */ 00801 { 00802 return( SubSlide( P, ubi_trRIGHT ) ); 00803 } /* ubi_btLast */ 00804 00805 ubi_btNodePtr ubi_btFirstOf( ubi_btRootPtr RootPtr, 00806 ubi_btItemPtr MatchMe, 00807 ubi_btNodePtr p ) 00808 /* ------------------------------------------------------------------------ ** 00809 * Given a tree that a allows duplicate keys, and a pointer to a node in 00810 * the tree, this function will return a pointer to the first (traversal 00811 * order) node with the same key value. 00812 * 00813 * Input: RootPtr - A pointer to the root of the tree. 00814 * MatchMe - A pointer to the key value. This should probably 00815 * point to the key within node *p. 00816 * p - A pointer to a node in the tree. 00817 * Output: A pointer to the first node in the set of nodes with keys 00818 * matching <FindMe>. 00819 * Notes: Node *p MUST be in the set of nodes with keys matching 00820 * <FindMe>. If not, this function will return NULL. 00821 * 00822 * 4.7: Bug found & fixed by Massimo Campostrini, 00823 * Istituto Nazionale di Fisica Nucleare, Sezione di Pisa. 00824 * 00825 * ------------------------------------------------------------------------ ** 00826 */ 00827 { 00828 /* If our starting point is invalid, return NULL. */ 00829 if( (NULL == p) 00830 || (ubi_trEQUAL != ubi_trAbNormal( (*(RootPtr->cmp))( MatchMe, p ) )) ) 00831 return( NULL ); 00832 return( Border( RootPtr, MatchMe, p, ubi_trLEFT ) ); 00833 } /* ubi_btFirstOf */ 00834 00835 ubi_btNodePtr ubi_btLastOf( ubi_btRootPtr RootPtr, 00836 ubi_btItemPtr MatchMe, 00837 ubi_btNodePtr p ) 00838 /* ------------------------------------------------------------------------ ** 00839 * Given a tree that a allows duplicate keys, and a pointer to a node in 00840 * the tree, this function will return a pointer to the last (traversal 00841 * order) node with the same key value. 00842 * 00843 * Input: RootPtr - A pointer to the root of the tree. 00844 * MatchMe - A pointer to the key value. This should probably 00845 * point to the key within node *p. 00846 * p - A pointer to a node in the tree. 00847 * Output: A pointer to the last node in the set of nodes with keys 00848 * matching <FindMe>. 00849 * Notes: Node *p MUST be in the set of nodes with keys matching 00850 * <FindMe>. If not, this function will return NULL. 00851 * 00852 * 4.7: Bug found & fixed by Massimo Campostrini, 00853 * Istituto Nazionale di Fisica Nucleare, Sezione di Pisa. 00854 * 00855 * ------------------------------------------------------------------------ ** 00856 */ 00857 { 00858 /* If our starting point is invalid, return NULL. */ 00859 if( (NULL != p) 00860 || (ubi_trEQUAL != ubi_trAbNormal( (*(RootPtr->cmp))( MatchMe, p ) )) ) 00861 return( NULL ); 00862 return( Border( RootPtr, MatchMe, p, ubi_trRIGHT ) ); 00863 } /* ubi_btLastOf */ 00864 00865 unsigned long ubi_btTraverse( ubi_btRootPtr RootPtr, 00866 ubi_btActionRtn EachNode, 00867 void *UserData ) 00868 /* ------------------------------------------------------------------------ ** 00869 * Traverse a tree in sorted order (non-recursively). At each node, call 00870 * (*EachNode)(), passing a pointer to the current node, and UserData as the 00871 * second parameter. 00872 * 00873 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates 00874 * the tree to be traversed. 00875 * EachNode - a pointer to a function to be called at each node 00876 * as the node is visited. 00877 * UserData - a generic pointer that may point to anything that 00878 * you choose. 00879 * 00880 * Output: A count of the number of nodes visited. This will be zero 00881 * if the tree is empty. 00882 * 00883 * ------------------------------------------------------------------------ ** 00884 */ 00885 { 00886 ubi_btNodePtr p = ubi_btFirst( RootPtr->root ); 00887 unsigned long count = 0; 00888 00889 while( NULL != p ) 00890 { 00891 (*EachNode)( p, UserData ); 00892 count++; 00893 if(RootPtr->count != 0) 00894 p = ubi_btNext( p ); 00895 else 00896 return count; 00897 } 00898 return( count ); 00899 } /* ubi_btTraverse */ 00900 00901 unsigned long ubi_btTraverseReverse( ubi_btRootPtr RootPtr, 00902 ubi_btActionRtn EachNode, 00903 void *UserData ) 00904 /* ------------------------------------------------------------------------ ** 00905 * Traverse a tree in reverse sorted order (non-recursively). At each node, 00906 * call (*EachNode)(), passing a pointer to the current node, and UserData 00907 * as the second parameter. 00908 * 00909 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates 00910 * the tree to be traversed. 00911 * EachNode - a pointer to a function to be called at each node 00912 * as the node is visited. 00913 * UserData - a generic pointer that may point to anything that 00914 * you choose. 00915 * 00916 * Output: A count of the number of nodes visited. This will be zero 00917 * if the tree is empty. 00918 * 00919 * ------------------------------------------------------------------------ ** 00920 */ 00921 { 00922 ubi_btNodePtr p = ubi_btLast( RootPtr->root ); 00923 unsigned long count = 0; 00924 00925 while( NULL != p ) 00926 { 00927 (*EachNode)( p, UserData ); 00928 count++; 00929 if(RootPtr->count != 0) 00930 p = ubi_btPrev( p ); 00931 else 00932 return count; 00933 } 00934 return( count ); 00935 } /* ubi_btTraverse */ 00936 00937 unsigned long ubi_btKillTree( ubi_btRootPtr RootPtr, 00938 ubi_btKillNodeRtn FreeNode ) 00939 /* ------------------------------------------------------------------------ ** 00940 * Delete an entire tree (non-recursively) and reinitialize the ubi_btRoot 00941 * structure. Return a count of the number of nodes deleted. 00942 * 00943 * Input: RootPtr - a pointer to an ubi_btRoot structure that indicates 00944 * the root of the tree to delete. 00945 * FreeNode - a function that will be called for each node in the 00946 * tree to deallocate the memory used by the node. 00947 * 00948 * Output: The number of nodes removed from the tree. 00949 * A value of 0 will be returned if: 00950 * - The tree actually contains 0 entries. 00951 * - the value of <RootPtr> is NULL, in which case the tree is 00952 * assumed to be empty 00953 * - the value of <FreeNode> is NULL, in which case entries 00954 * cannot be removed, so 0 is returned. *Make sure that you 00955 * provide a valid value for <FreeNode>*. 00956 * In all other cases, you should get a positive value equal to 00957 * the value of RootPtr->count upon entry. 00958 * 00959 * ------------------------------------------------------------------------ ** 00960 */ 00961 { 00962 ubi_btNodePtr p, q; 00963 unsigned long count = 0; 00964 00965 if( (NULL == RootPtr) || (NULL == FreeNode) ) 00966 return( 0 ); 00967 00968 p = ubi_btFirst( RootPtr->root ); 00969 while( NULL != p ) 00970 { 00971 q = p; 00972 while( q->Link[ubi_trRIGHT] ) 00973 q = SubSlide( q->Link[ubi_trRIGHT], ubi_trLEFT ); 00974 p = q->Link[ubi_trPARENT]; 00975 if( NULL != p ) 00976 p->Link[ ((p->Link[ubi_trLEFT] == q)?ubi_trLEFT:ubi_trRIGHT) ] = NULL; 00977 (*FreeNode)((void *)q); 00978 count++; 00979 } 00980 00981 /* overkill... */ 00982 (void)ubi_btInitTree( RootPtr, 00983 RootPtr->cmp, 00984 RootPtr->flags ); 00985 return( count ); 00986 } /* ubi_btKillTree */ 00987 00988 ubi_btNodePtr ubi_btLeafNode( ubi_btNodePtr leader ) 00989 /* ------------------------------------------------------------------------ ** 00990 * Returns a pointer to a leaf node. 00991 * 00992 * Input: leader - Pointer to a node at which to start the descent. 00993 * 00994 * Output: A pointer to a leaf node selected in a somewhat arbitrary 00995 * manner. 00996 * 00997 * Notes: I wrote this function because I was using splay trees as a 00998 * database cache. The cache had a maximum size on it, and I 00999 * needed a way of choosing a node to sacrifice if the cache 01000 * became full. In a splay tree, less recently accessed nodes 01001 * tend toward the bottom of the tree, meaning that leaf nodes 01002 * are good candidates for removal. (I really can't think of 01003 * any other reason to use this function.) 01004 * + In a simple binary tree or an AVL tree, the most recently 01005 * added nodes tend to be nearer the bottom, making this a *bad* 01006 * way to choose which node to remove from the cache. 01007 * + Randomizing the traversal order is probably a good idea. You 01008 * can improve the randomization of leaf node selection by passing 01009 * in pointers to nodes other than the root node each time. A 01010 * pointer to any node in the tree will do. Of course, if you 01011 * pass a pointer to a leaf node you'll get the same thing back. 01012 * 01013 * ------------------------------------------------------------------------ ** 01014 */ 01015 { 01016 ubi_btNodePtr follower = NULL; 01017 int whichway = ubi_trLEFT; 01018 01019 while( NULL != leader ) 01020 { 01021 follower = leader; 01022 leader = follower->Link[ whichway ]; 01023 if( NULL == leader ) 01024 { 01025 whichway = ubi_trRevWay( whichway ); 01026 leader = follower->Link[ whichway ]; 01027 } 01028 } 01029 01030 return( follower ); 01031 } /* ubi_btLeafNode */ 01032 01033 int ubi_btModuleID( int size, char *list[] ) 01034 /* ------------------------------------------------------------------------ ** 01035 * Returns a set of strings that identify the module. 01036 * 01037 * Input: size - The number of elements in the array <list>. 01038 * list - An array of pointers of type (char *). This array 01039 * should, initially, be empty. This function will fill 01040 * in the array with pointers to strings. 01041 * Output: The number of elements of <list> that were used. If this value 01042 * is less than <size>, the values of the remaining elements are 01043 * not guaranteed. 01044 * 01045 * Notes: Please keep in mind that the pointers returned indicate strings 01046 * stored in static memory. Don't free() them, don't write over 01047 * them, etc. Just read them. 01048 * ------------------------------------------------------------------------ ** 01049 */ 01050 { 01051 if( size > 0 ) 01052 { 01053 list[0] = ModuleID; 01054 if( size > 1 ) 01055 list[1] = NULL; 01056 return( 1 ); 01057 } 01058 return( 0 ); 01059 } /* ubi_btModuleID */ 01060 01061 01062 /* ========================================================================== */