📄 ubi_bintree.h
字号:
#ifndef UBI_BINTREE_H#define UBI_BINTREE_H/* ========================================================================== ** * ubi_BinTree.h * * 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.h,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:15:07 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:13:47 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:16:04 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:30 crh * Added ubi_trCount() macro. * * Revision 2.5 1997/12/23 03:59:21 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:14 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:15:27 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:40 CRH * Ubisized! * * Revision 2.1 95/03/09 23:43:46 CRH * Added the ModuleID static string and function. These modules are now * self-identifying. * * Revision 2.0 95/02/27 22:00:33 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:55:04 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 "sys_include.h" /* Global include file, used to adapt the ubiqx * modules to the host environment and the project * with which the modules will be used. See * sys_include.h for more info. *//* -------------------------------------------------------------------------- ** * Macros and constants. * * General purpose: * ubi_trTRUE - Boolean TRUE. * ubi_trFALSE - Boolean FALSE. * * Flags used in the tree header: * ubi_trOVERWRITE - This flag indicates that an existing node may be * overwritten by a new node with a matching key. * ubi_trDUPKEY - This flag indicates that the tree allows duplicate * keys. If the tree does allow duplicates, the * overwrite flag is ignored. * * Node link array index constants: (Each node has an array of three * pointers. One to the left, one to the right, and one back to the * parent.) * ubi_trLEFT - Left child pointer. * ubi_trPARENT - Parent pointer. * ubi_trRIGHT - Right child pointer. * ubi_trEQUAL - Synonym for PARENT. * * ubi_trCompOps: These values are used in the ubi_trLocate() function. * ubi_trLT - request the first instance of the greatest key less than * the search key. * ubi_trLE - request the first instance of the greatest key that is less * than or equal to the search key. * ubi_trEQ - request the first instance of key that is equal to the * search key. * ubi_trGE - request the first instance of a key that is greater than * or equal to the search key. * ubi_trGT - request the first instance of the first key that is greater * than the search key. * -------------------------------------------------------------------------- ** */#define ubi_trTRUE 0xFF#define ubi_trFALSE 0x00#define ubi_trOVERWRITE 0x01 /* Turn on allow overwrite */#define ubi_trDUPKEY 0x02 /* Turn on allow duplicate keys *//* Pointer array index constants... */#define ubi_trLEFT 0x00#define ubi_trPARENT 0x01#define ubi_trRIGHT 0x02#define ubi_trEQUAL ubi_trPARENTtypedef enum { ubi_trLT = 1, ubi_trLE, ubi_trEQ, ubi_trGE, ubi_trGT } ubi_trCompOps;/* -------------------------------------------------------------------------- ** * These three macros allow simple manipulation of pointer index values (LEFT, * RIGHT, and PARENT). * * Normalize() - converts {LEFT, PARENT, RIGHT} into {-1, 0 ,1}. C * uses {negative, zero, positive} values to indicate * {less than, equal to, greater than}. * AbNormal() - converts {negative, zero, positive} to {LEFT, PARENT, * RIGHT} (opposite of Normalize()). Note: C comparison * functions, such as strcmp(), return {negative, zero, * positive} values, which are not necessarily {-1, 0, * 1}. This macro uses the the ubi_btSgn() function to * compensate. * RevWay() - converts LEFT to RIGHT and RIGHT to LEFT. PARENT (EQUAL) * is left as is. * -------------------------------------------------------------------------- ** */#define ubi_trNormalize(W) ((char)( (W) - ubi_trEQUAL ))#define ubi_trAbNormal(W) ((char)( ((char)ubi_btSgn( (long)(W) )) \ + ubi_trEQUAL ))#define ubi_trRevWay(W) ((char)( ubi_trEQUAL - ((W) - ubi_trEQUAL) ))/* -------------------------------------------------------------------------- ** * These macros allow us to quickly read the values of the OVERWRITE and * DUPlicate KEY bits of the tree root flags field. * -------------------------------------------------------------------------- ** */#define ubi_trDups_OK(A) \ ((ubi_trDUPKEY & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))#define ubi_trOvwt_OK(A) \ ((ubi_trOVERWRITE & ((A)->flags))?(ubi_trTRUE):(ubi_trFALSE))/* -------------------------------------------------------------------------- ** * Additional Macros... * * ubi_trCount() - Given a pointer to a tree root, this macro returns the * number of nodes currently in the tree. * * ubi_trNewTree() - This macro makes it easy to declare and initialize a * tree header in one step. The line
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -