tpl_binTree.H

00001 
00002 /*
00003   This file is part of Aleph system
00004 
00005   Copyright (c) 2002, 2003, 2004, 2005, 2006
00006   UNIVERSITY LOS ANDES (ULA) Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA  
00007 
00008   - Center of Studies in Microelectronics & Distributed Systems (CEMISID) 
00009   - ULA Computer Science Department
00010   - FUNDACITE Mérida - Ministerio de Ciencia y Tecnología
00011 
00012   PERMISSION TO USE, COPY, MODIFY AND DISTRIBUTE THIS SOFTWARE AND ITS
00013   DOCUMENTATION IS HEREBY GRANTED, PROVIDED THAT BOTH THE COPYRIGHT
00014   NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES OF THE
00015   SOFTWARE, DERIVATIVE WORKS OR MODIFIED VERSIONS, AND ANY PORTIONS
00016   THEREOF, AND THAT BOTH NOTICES APPEAR IN SUPPORTING DOCUMENTATION.
00017 
00018   Aleph is distributed in the hope that it will be useful, but WITHOUT
00019   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020   or FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022   UNIVERSIDAD DE LOS ANDES requests users of this software to return to 
00023 
00024   Leandro Leon
00025   CEMISID 
00026   Ed La Hechicera 
00027   3er piso, ala sur
00028   Facultad de Ingenieria 
00029   Universidad de Los Andes 
00030   Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA    or
00031 
00032   lrleon@ula.ve
00033 
00034   any improvements or extensions that they make and grant Universidad 
00035   de Los Andes (ULA) the rights to redistribute these changes. 
00036 
00037   Aleph is (or was) granted by: 
00038   - Consejo de Desarrollo Cientifico, Humanistico, Tecnico de la ULA 
00039     (CDCHT)
00040   - Fundacite Mérida
00041 */
00042 
00043 
00044 # ifndef TPL_BINTREE_H
00045 # define TPL_BINTREE_H
00046 
00047 # include <tpl_binNodeUtils.H>
00048 
00049 using namespace Aleph;
00050 
00051 namespace Aleph {
00052 
00071     template <template <typename> class NodeType, typename Key, class Compare>
00072 class GenBinTree 
00073 {
00074   
00075   public:
00076 
00077     typedef NodeType<Key> Node;
00078 
00079   private:
00080 
00081 
00082 private:
00083 
00084   Node    headNode;
00085   Node  * head;
00086   Node *& root;
00087 
00088   GenBinTree(const GenBinTree & );
00089 
00090   GenBinTree & operator =(const GenBinTree &);
00091 public:
00092 
00093   GenBinTree() : head(&headNode), root(headNode.getR()) { /* Empty */ }
00103   Node * search(const Key & key)
00104   {
00105     return searchInBinTree <Node, Compare>(root, key);
00106   }
00107   virtual ~GenBinTree() { /* Empty */ }
00109   Node*& getRoot() { return root; }
00110   bool verifyBin() 
00111   { 
00112     return check_binary_search_tree<Node, Compare>(root); 
00113   } 
00114 
00115   void reset() { new (this) GenBinTree; }
00124   Node * insert(Node *p)
00125   {
00126 
00127     I(p != Node::NullPtr);
00128     I((LLINK(p) == Node::NullPtr) and(RLINK(p) == Node::NullPtr));
00129 
00130     return insert_in_binary_search_tree<Node, Compare>(root, p);
00131   }
00147   bool split(const Key & key, GenBinTree & l, GenBinTree & r)
00148   {
00149 
00150     I(l.root == Node::NullPtr and r.root == Node::NullPtr);
00151 
00152     return split_key_rec<Node, Compare>(root, key, l.root, r.root);
00153   }
00165   Node * remove(const Key & key)
00166   {
00167     return remove_from_search_binary_tree<Node, Compare>(root, key);
00168   }
00180   void join(GenBinTree & tree, GenBinTree & dup)
00181   {
00182     root = Aleph::join<Node, Compare> (root, tree.root, dup);
00183 
00184     tree.root = Node::NullPtr;
00185   }
00186 };
00187 
00204     template  <typename Key, class Compare = Aleph::less<Key> >
00205 class BinTree : public GenBinTree<BinNode, Key, Compare>
00206 { /* empty */ };
00207 
00224     template  <typename Key, class Compare = Aleph::less<Key> >
00225 class BinTreeVtl : public GenBinTree<BinNodeVtl, Key, Compare>
00226 { /* empty */ };
00227 
00228 } // end namespace Aleph
00229 # endif /* TPL_BINTREE_H */
00230 

Leandro R. León