⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ubi_bintree.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* ========================================================================== ** *                              ubi_BinTree.c * *  Copyright (C) 1991-1998 by Christopher R. Hertel * *  Email:  crh@ubiqx.mn.org * -------------------------------------------------------------------------- ** * *  This module implements a simple binary tree. * * -------------------------------------------------------------------------- ** * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Library General Public *  License as published by the Free Software Foundation; either *  version 2 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Library General Public License for more details. * *  You should have received a copy of the GNU Library General Public *  License along with this library; if not, write to the Free *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * -------------------------------------------------------------------------- ** * * Log: ubi_BinTree.c,v * Revision 4.12  2004/06/06 04:51:56  crh * Fixed a small typo in ubi_BinTree.c (leftover testing cruft). * Did a small amount of formatting touchup to ubi_BinTree.h. * * Revision 4.11  2004/06/06 03:14:09  crh * Rewrote the ubi_btLeafNode() function.  It now takes several paths in an * effort to find a deeper leaf node.  There is a small amount of extra * overhead, but it is limited. * * Revision 4.10  2000/06/06 20:38:40  crh * In the ReplaceNode() function, the old node header was being copied * to the new node header using a byte-by-byte copy.  This was causing * the 'insure' software testing program to report a memory leak.  The * fix was to do a simple assignement: *newnode = *oldnode; * This quieted the (errant) memory leak reports and is probably a bit * faster than the bytewise copy. * * Revision 4.9  2000/01/08 23:24:30  crh * Clarified a variety of if( pointer ) lines, replacing them with * if( NULL != pointer ).  This is more correct, and I have heard * of at least one (obscure?) system out there that uses a non-zero * value for NULL. * Also, speed improvement in Neighbor().  It was comparing pointers * when it could have compared two gender values.  The pointer * comparison was somewhat indirect (does pointer equal the pointer * of the parent of the node pointed to by pointer).  Urq. * * Revision 4.8  1999/09/22 03:40:30  crh * Modified ubi_btTraverse() and ubi_btKillTree().  They now return an * unsigned long indicating the number of nodes processed.  The change * is subtle.  An empty tree formerly returned False, and now returns * zero. * * Revision 4.7  1998/10/21 06:14:42  crh * Fixed bugs in FirstOf() and LastOf() reported by Massimo Campostrini. * See function comments. * * Revision 4.6  1998/07/25 17:02:10  crh * Added the ubi_trNewTree() macro. * * Revision 4.5  1998/06/04 21:29:27  crh * Upper-cased defined constants (eg UBI_BINTREE_H) in some header files. * This is more "standard", and is what people expect.  Weird, eh? * * Revision 4.4  1998/06/03 17:42:46  crh * Further fiddling with sys_include.h.  It's now in ubi_BinTree.h which is * included by all of the binary tree files. * * Reminder: Some of the ubi_tr* macros in ubi_BinTree.h are redefined in *           ubi_AVLtree.h and ubi_SplayTree.h.  This allows easy swapping *           of tree types by simply changing a header.  Unfortunately, the *           macro redefinitions in ubi_AVLtree.h and ubi_SplayTree.h will *           conflict if used together.  You must either choose a single tree *           type, or use the underlying function calls directly.  Compare *           the two header files for more information. * * Revision 4.3  1998/06/02 01:28:43  crh * Changed ubi_null.h to sys_include.h to make it more generic. * * Revision 4.2  1998/05/20 04:32:36  crh * The C file now includes ubi_null.h.  See ubi_null.h for more info. * Also, the balance and gender fields of the node were declared as * signed char.  As I understand it, at least one SunOS or Solaris * compiler doesn't like "signed char".  The declarations were * wrong anyway, so I changed them to simple "char". * * Revision 4.1  1998/03/31 06:11:57  crh * Thomas Aglassinger sent E'mail pointing out errors in the * dereferencing of function pointers, and a missing typecast. * Thanks, Thomas! * * Revision 4.0  1998/03/10 03:19:22  crh * Added the AVL field 'balance' to the ubi_btNode structure.  This means * that all BinTree modules now use the same basic node structure, which * greatly simplifies the AVL module. * Decided that this was a big enough change to justify a new major revision * number.  3.0 was an error, so we're at 4.0. * * Revision 2.6  1998/01/24 06:27:46  crh * Added ubi_trCount() macro. * * Revision 2.5  1997/12/23 03:56:29  crh * In this version, all constants & macros defined in the header file have * the ubi_tr prefix.  Also cleaned up anything that gcc complained about * when run with '-pedantic -fsyntax-only -Wall'. * * Revision 2.4  1997/07/26 04:11:10  crh * + Just to be annoying I changed ubi_TRUE and ubi_FALSE to ubi_trTRUE *   and ubi_trFALSE. * + There is now a type ubi_trBool to go with ubi_trTRUE and ubi_trFALSE. * + There used to be something called "ubi_TypeDefs.h".  I got rid of it. * + Added function ubi_btLeafNode(). * * Revision 2.3  1997/06/03 05:16:17  crh * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid conflicts. * Also changed the interface to function InitTree().  See the comments * for this function for more information. * * Revision 2.2  1995/10/03 22:00:07  CRH * Ubisized! *  * Revision 2.1  95/03/09  23:37:10  CRH * Added the ModuleID static string and function.  These modules are now * self-identifying. *  * Revision 2.0  95/02/27  22:00:17  CRH * Revision 2.0 of this program includes the following changes: * *     1)  A fix to a major typo in the RepaceNode() function. *     2)  The addition of the static function Border(). *     3)  The addition of the public functions FirstOf() and LastOf(), which *         use Border(). These functions are used with trees that allow *         duplicate keys. *     4)  A complete rewrite of the Locate() function.  Locate() now accepts *         a "comparison" operator. *     5)  Overall enhancements to both code and comments. * * I decided to give this a new major rev number because the interface has * changed.  In particular, there are two new functions, and changes to the * Locate() function. * * Revision 1.0  93/10/15  22:44:59  CRH * With this revision, I have added a set of #define's that provide a single, * standard API to all existing tree modules.  Until now, each of the three * existing modules had a different function and typedef prefix, as follows: * *       Module        Prefix *     ubi_BinTree     ubi_bt *     ubi_AVLtree     ubi_avl *     ubi_SplayTree   ubi_spt * * To further complicate matters, only those portions of the base module * (ubi_BinTree) that were superceeded in the new module had the new names. * For example, if you were using ubi_SplayTree, the locate function was * called "ubi_sptLocate", but the next and previous functions remained * "ubi_btNext" and "ubi_btPrev". * * This was not too terrible if you were familiar with the modules and knew * exactly which tree model you wanted to use.  If you wanted to be able to * change modules (for speed comparisons, etc), things could get messy very * quickly. * * So, I have added a set of defined names that get redefined in any of the * descendant modules.  To use this standardized interface in your code, * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with * "ubi_tr".  The "ubi_tr" names will resolve to the correct function or * datatype names for the module that you are using.  Just remember to * include the header for that module in your program file.  Because these * names are handled by the preprocessor, there is no added run-time * overhead. * * Note that the original names do still exist, and can be used if you wish * to write code directly to a specific module.  This should probably only be * done if you are planning to implement a new descendant type, such as * red/black trees.  CRH * *  V0.0 - June, 1991   -  Written by Christopher R. Hertel (CRH). * * ========================================================================== ** */#include "ubi_BinTree.h"  /* Header for this module.   *//* ========================================================================== ** * Static data. */static char ModuleID[] = "ubi_BinTree\n\\tRevision: 4.12\n\\tDate: 2004/06/06 04:51:56\n\\tAuthor: crh\n";/* ========================================================================== ** * Internal (private) functions. */static ubi_btNodePtr qFind( ubi_btCompFunc cmp,                            ubi_btItemPtr  FindMe,                   register ubi_btNodePtr  p )  /* ------------------------------------------------------------------------ **   * This function performs a non-recursive search of a tree for a node   * matching a specific key.  It is called "qFind()" because it is   * faster that TreeFind (below).   *   *  Input:   *     cmp      -  a pointer to the tree's comparison function.   *     FindMe   -  a pointer to the key value for which to search.   *     p        -  a pointer to the starting point of the search.  <p>   *                 is considered to be the root of a subtree, and only   *                 the subtree will be searched.   *   *  Output:   *     A pointer to a node with a key that matches the key indicated by   *     FindMe, or NULL if no such node was found.   *   *  Note:   In a tree that allows duplicates, the pointer returned *might   *          not* point to the (sequentially) first occurance of the   *          desired key.   * ------------------------------------------------------------------------ **   */  {  int tmp;  while( (NULL != p)      && ((tmp = ubi_trAbNormal( (*cmp)(FindMe, p) )) != ubi_trEQUAL) )    p = p->Link[tmp];  return( p );  } /* qFind */static ubi_btNodePtr TreeFind( ubi_btItemPtr  findme,                               ubi_btNodePtr  p,                               ubi_btNodePtr *parentp,                               char          *gender,                               ubi_btCompFunc CmpFunc )  /* ------------------------------------------------------------------------ **   * TreeFind() searches a tree for a given value (findme).  It will return a   * pointer to the target node, if found, or NULL if the target node was not   * found.   *   * TreeFind() also returns, via parameters, a pointer to the parent of the   * target node, and a LEFT or RIGHT value indicating which child of the   * parent is the target node.  *If the target is not found*, then these   * values indicate the place at which the target *should be found*.  This   * is useful when inserting a new node into a tree or searching for nodes   * "near" the target node.   *   * The parameters are:   *   *  findme   -  is a pointer to the key information to be searched for.   *  p        -  points to the root of the tree to be searched.   *  parentp  -  will return a pointer to a pointer to the !parent! of the   *              target node, which can be especially usefull if the target   *              was not found.   *  gender   -  returns LEFT or RIGHT to indicate which child of *parentp   *              was last searched.   *  CmpFunc  -  points to the comparison function.   *   * This function is called by ubi_btLocate() and ubi_btInsert().   * ------------------------------------------------------------------------ **   */  {  register ubi_btNodePtr tmp_p      = p;  ubi_btNodePtr          tmp_pp     = NULL;  char                   tmp_gender = ubi_trEQUAL;  int                    tmp_cmp;  while( (NULL != tmp_p)     && (ubi_trEQUAL != (tmp_cmp = ubi_trAbNormal((*CmpFunc)(findme, tmp_p)))) )    {    tmp_pp     = tmp_p;                 /* Keep track of previous node. */    tmp_gender = (char)tmp_cmp;         /* Keep track of sex of child.  */    tmp_p      = tmp_p->Link[tmp_cmp];  /* Go to child. */    }  *parentp = tmp_pp;                /* Return results. */  *gender  = tmp_gender;  return( tmp_p );  } /* TreeFind */static void ReplaceNode( ubi_btNodePtr *parent,                         ubi_btNodePtr  oldnode,                         ubi_btNodePtr  newnode )  /* ------------------------------------------------------------------------ **   * Remove node oldnode from the tree, replacing it with node newnode.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -