00001 /* 00002 00003 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00004 00005 This program is free software; you can redistribute it and/or modify it 00006 under the terms of version 2 of the GNU General Public License as 00007 published by the Free Software Foundation. 00008 00009 This program is distributed in the hope that it would be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 Further, this software is distributed without any warranty that it is 00014 free of the rightful claim of any third person regarding infringement 00015 or the like. Any license provided herein, whether implied or 00016 otherwise, applies only to this software file. Patent licenses, if 00017 any, provided herein do not apply to combinations of this program with 00018 other software, or any other product whatsoever. 00019 00020 You should have received a copy of the GNU General Public License along 00021 with this program; if not, write the Free Software Foundation, Inc., 59 00022 Temple Place - Suite 330, Boston MA 02111-1307, USA. 00023 00024 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00025 Mountain View, CA 94043, or: 00026 00027 http://www.sgi.com 00028 00029 For further information regarding this notice, see: 00030 00031 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00032 00033 */ 00034 00035 00036 // -*-C++-*- 00037 00062 #ifndef bin_tree_INCLUDED 00063 #define bin_tree_INCLUDED 00064 00065 static const char *bin_tree_rcs_id = "$Source: /scratch/mee/2.4-65/kpro64-pending/be/com/SCCS/s.btree.h $ $Revision: 1.2 $"; 00066 00067 00068 #ifndef defs_INCLUDED 00069 #include "defs.h" 00070 #endif 00071 #ifndef ERRORS_INCLUDED 00072 #include "errors.h" 00073 #endif 00074 #ifndef CXX_MEMORY_INCLUDED 00075 #include "cxx_memory.h" 00076 #endif 00077 00078 00079 template <class BINARY_NODE> 00080 class BINARY_TREE_NODE { 00081 BINARY_TREE_NODE<BINARY_NODE> *_left; 00082 BINARY_TREE_NODE<BINARY_NODE> *_right; 00083 BINARY_NODE _data; 00084 public: 00085 // create a new element 00086 BINARY_TREE_NODE(BINARY_NODE data) { 00087 _data = data; 00088 _left = _right = NULL; 00089 }; 00090 BINARY_TREE_NODE<BINARY_NODE>* Enter(BINARY_NODE node, MEM_POOL *pool); 00091 BINARY_TREE_NODE<BINARY_NODE>* Find(BINARY_NODE node) const; 00092 ~BINARY_TREE_NODE() { 00093 if (_left) CXX_DELETE(_left,Default_Mem_Pool); 00094 if (_right) CXX_DELETE(_right,Default_Mem_Pool); 00095 }; 00096 BINARY_NODE* Get_Data() { return &_data; } 00097 const BINARY_NODE* Get_Data() const { return &_data; } 00098 }; 00099 00100 template <class BINARY_NODE> 00101 class BINARY_TREE { 00102 BINARY_TREE_NODE<BINARY_NODE> *_binary_tree_node; 00103 MEM_POOL *_pool; 00104 public: 00105 BINARY_TREE(MEM_POOL *pool) { _pool = pool; _binary_tree_node = NULL; } 00106 BINARY_TREE_NODE<BINARY_NODE>* Enter(BINARY_NODE node) { 00107 typedef BINARY_TREE_NODE<BINARY_NODE> THIS_BINARY_TREE_NODE; 00108 if (!_binary_tree_node) { 00109 _binary_tree_node = CXX_NEW(THIS_BINARY_TREE_NODE(node),_pool); 00110 return(_binary_tree_node); 00111 } else { 00112 return(_binary_tree_node->Enter(node,_pool)); 00113 } 00114 } 00115 BINARY_TREE_NODE<BINARY_NODE>* Find(BINARY_NODE element) const { 00116 if (!_binary_tree_node) { 00117 return(FALSE); 00118 } else { 00119 return(_binary_tree_node->Find(element)); 00120 } 00121 } 00122 ~BINARY_TREE() { 00123 MEM_POOL_Set_Default(_pool); 00124 if (_binary_tree_node) CXX_DELETE(_binary_tree_node,Default_Mem_Pool); 00125 } 00126 }; 00127 00128 00129 #ifdef __GNUC__ 00130 // Implementation stuff included here because g++ 00131 // (rightly) doesn't do implicit .cxx file inclusion. 00132 00133 template <class BINARY_NODE> 00134 BINARY_TREE_NODE<BINARY_NODE>* BINARY_TREE_NODE<BINARY_NODE>:: 00135 Enter(BINARY_NODE node, MEM_POOL *pool) 00136 { 00137 //typedef BINARY_TREE_NODE<BINARY_NODE> THIS_NODE; 00138 //typedef BINARY_TREE_NODE<BINARY_NODE> *THIS_NODEP; 00139 //THIS_NODEP nodep = this; 00140 BINARY_TREE_NODE<BINARY_NODE> *nodep = this; 00141 BOOL found = FALSE; 00142 while (!found) { 00143 if (nodep->_data == node) { 00144 found = TRUE; 00145 } else if (node < nodep->_data) { 00146 if (!nodep->_left) { 00147 nodep->_left = CXX_NEW(BINARY_TREE_NODE<BINARY_NODE>(node),pool); 00148 found = TRUE; 00149 } 00150 nodep = nodep->_left; 00151 } else { 00152 if (!nodep->_right) { 00153 nodep->_right = CXX_NEW(BINARY_TREE_NODE<BINARY_NODE>(node),pool); 00154 found = TRUE; 00155 } 00156 nodep = nodep->_right; 00157 } 00158 } 00159 return nodep; 00160 } 00161 00162 00163 template <class BINARY_NODE> 00164 BINARY_TREE_NODE<BINARY_NODE>* BINARY_TREE_NODE<BINARY_NODE>:: 00165 Find(BINARY_NODE node) const 00166 { 00167 //typedef BINARY_TREE_NODE<BINARY_NODE> *THIS_NODEP; 00168 BINARY_TREE_NODE<BINARY_NODE> *nodep = (BINARY_TREE_NODE<BINARY_NODE> *)this; 00169 while (1) { 00170 if (nodep->_data == node) { 00171 return nodep; 00172 } else if (node < nodep->_data) { 00173 if (nodep->_left) { 00174 nodep = nodep->_left; 00175 } else { 00176 return NULL; 00177 } 00178 } else { 00179 if (nodep->_right) { 00180 nodep = nodep->_right; 00181 } else { 00182 return NULL; 00183 } 00184 } 00185 } 00186 } 00187 00188 #endif 00189 00190 #endif
1.5.6