tpl_dynSetTree.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_DYNSETTREE_H
00045 # define TPL_DYNSETTREE_H
00046 
00047 # include <tpl_binNodeUtils.H>
00048 # include <tpl_binTree.H>
00049 # include <tpl_treap.H>
00050 # include <tpl_avl.H>
00051 # include <tpl_rand_tree.H>
00052 # include <tpl_rb_tree.H>
00053 # include <tpl_splay_tree.H>
00054 
00055 using namespace Aleph;
00056 
00057 namespace Aleph { 
00058 
00068     template <
00069       template <class /* Key */, class /* Compare */> class Tree, 
00070       typename Key, 
00071       class Compare = Aleph::less<Key> 
00072     >
00073 class DynSetTree
00074 {
00075 private:
00076 
00077   Tree<Key, Compare> tree; 
00078 
00079   size_t num_nodes; 
00080 
00081 public:
00082 
00085   typedef typename Tree<Key, Compare>::Node Node;
00086 
00088   DynSetTree() : num_nodes(0) { /* empty */ }
00089 
00091   DynSetTree(DynSetTree & srcTree) : num_nodes(srcTree.num_nodes) 
00092   { 
00093     Node * srcRoot = static_cast<Node*>(srcTree.tree.getRoot()); 
00094 
00095     try 
00096       {
00097      tree.getRoot() = copyStack(srcRoot);
00098       }
00099     catch (...)
00100       {
00101      num_nodes = 0;
00102      throw;
00103       }
00104   }
00105 
00107   DynSetTree & operator = (DynSetTree & srcTree)
00108   {
00109     if (this == &srcTree)
00110       return *this;
00111 
00112     Node *srcRoot = static_cast<Node*>(srcTree.tree.getRoot());
00113     Node *oldRoot = static_cast<Node*>(tree.getRoot());
00114 
00115     tree.getRoot() = copyStack(srcRoot);
00116     num_nodes       = srcTree.num_nodes;
00117     destroyStack(oldRoot);
00118 
00119     return *this;
00120   }
00121 
00123   virtual ~DynSetTree() 
00124   {
00125     destroyRec(tree.getRoot());
00126   }
00127 
00136   size_t insert(const Key & key)
00137   {
00138     Node * node = new Node (key);
00139 
00140     if (tree.insert(node) == NULL)
00141       {
00142      delete node;
00143      return num_nodes;
00144       }
00145 
00146     return ++num_nodes;
00147   }
00148 
00156   size_t remove(const Key & key)
00157   {
00158     Node *node = static_cast<Node*>(tree.remove(key));
00159 
00160     if (node == NULL)
00161       return num_nodes;
00162 
00163     delete node;
00164 
00165     return --num_nodes;
00166   }
00167 
00169   bool exist(const Key & key)
00170   {
00171     Node * node = static_cast<Node*>(tree.search(key));
00172 
00173     return node not_eq NULL;
00174   }
00175 
00190   Key & find(const Key & key) throw(std::exception, std::domain_error)
00191   {
00192     Node *node = static_cast<Node*>(tree.search(key));
00193 
00194     if (node == NULL)
00195       throw std::domain_error("key not found");;
00196 
00197     return node->get_data();
00198   }
00199 
00201   size_t size() const { return num_nodes; }
00202 
00204   bool is_empty() const { return num_nodes == 0; }
00205 
00208   size_t internal_path_length() const 
00209   {
00210     return internal_path_length(tree.getRoot()); 
00211   }
00212 
00214   size_t height() const { return computeHeightRec(tree.getRoot()); }
00215 
00217 void for_each_in_preorder(void (*visitFct)(Node*, int, int))
00218 {
00219   Node * root = static_cast<Node*>(tree.getRoot());
00220 
00221   preOrderRec(root, visitFct); 
00222 }
00223 
00224 private:
00225 
00226       template <class Operation> static
00227   void visit(Node * p, int, int pos)
00228   {
00229     Operation()(KEY(p), pos);
00230   }
00231 
00232 public:
00233 
00235   template <class Operation>
00236 void for_each()
00237 {
00238   inOrderRec(tree.getRoot(), visit<Operation>);
00239 }
00240 };
00241 
00242 
00249     template  <typename Key, class Compare = Aleph::less<Key> >
00250 class DynSetBinTree : public DynSetTree<BinTree, Key, Compare>
00251     { /* empty */ };
00252 
00253 
00260     template  <typename Key, class Compare = Aleph::less<Key> >
00261 class DynSetAvlTree : public DynSetTree<Avl_Tree, Key, Compare>
00262     { /* empty */ };
00263 
00264 
00271     template  <typename Key, class Compare = Aleph::less<Key> >
00272 class DynSetSplayTree : public DynSetTree<Splay_Tree, Key, Compare>
00273     { /* empty */ };
00274 
00275 
00282     template  <typename Key, class Compare = Aleph::less<Key> >
00283 class DynSetRandTree : public DynSetTree<Rand_Tree, Key, Compare>
00284     { /* empty */ };
00285 
00286 
00293     template  <typename Key, class Compare = Aleph::less<Key> >
00294 class DynSetTreap : public DynSetTree<Treap, Key, Compare>
00295     { /* empty */ };
00296 
00297 
00304     template  <typename Key, class Compare = Aleph::less<Key> >
00305 class DynSetRbTree : public DynSetTree<Rb_Tree, Key, Compare>
00306     { /* empty */ }; 
00307 
00308 
00309 } // end namespace Aleph
00310 
00311 # endif /* TPL_DYNSETTREE_H */
00312 

Leandro R. León