00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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 , class > 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) { }
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 { };
00252
00253
00260 template <typename Key, class Compare = Aleph::less<Key> >
00261 class DynSetAvlTree : public DynSetTree<Avl_Tree, Key, Compare>
00262 { };
00263
00264
00271 template <typename Key, class Compare = Aleph::less<Key> >
00272 class DynSetSplayTree : public DynSetTree<Splay_Tree, Key, Compare>
00273 { };
00274
00275
00282 template <typename Key, class Compare = Aleph::less<Key> >
00283 class DynSetRandTree : public DynSetTree<Rand_Tree, Key, Compare>
00284 { };
00285
00286
00293 template <typename Key, class Compare = Aleph::less<Key> >
00294 class DynSetTreap : public DynSetTree<Treap, Key, Compare>
00295 { };
00296
00297
00304 template <typename Key, class Compare = Aleph::less<Key> >
00305 class DynSetRbTree : public DynSetTree<Rb_Tree, Key, Compare>
00306 { };
00307
00308
00309 }
00310
00311 # endif
00312