Aleph-w  1.9
General library for algorithms and data structures
tpl_binTree.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 
28 # ifndef TPL_BINTREE_H
29 # define TPL_BINTREE_H
30 
31 # include <tpl_binTreeOps.H>
32 
33 using namespace Aleph;
34 
35 namespace Aleph {
36 
55  template <template <typename> class NodeType, typename Key, class Compare>
56 class GenBinTree
57 {
58 public:
59 
60  using Node = NodeType<Key>;
61 
62 private:
63 
64  Node headNode;
65  Node * head;
66  Node *& root;
67  Compare cmp;
68 
69 public:
70 
71  GenBinTree(const GenBinTree & ) = delete;
72 
73  GenBinTree & operator = (const GenBinTree &) = delete;
74 
76  void swap(GenBinTree & tree) noexcept
77  {
78  std::swap(root, tree.root);
79  std::swap(cmp, tree.cmp);
80  }
81 
83  Compare & key_comp() noexcept { return cmp; }
84 
86  Compare & get_compare() noexcept { return key_comp(); }
87 
89  GenBinTree(Compare __cmp = Compare()) noexcept
90  : head(&headNode), root(headNode.getR()), cmp(__cmp)
91  {
92  // empty
93  }
94 
101  Node * search(const Key & key) const noexcept
102  {
103  return BinTree_Operation<Node, Compare>(cmp).search(root, key);
104  }
105 
106  virtual ~GenBinTree() noexcept { /* Empty */ }
107 
109  bool verify() const { return check_bst<Node, Compare>(root); }
110 
112  Node *& getRoot() { return root; }
113 
115  bool verifyBin() const { return check_bst<Node, Compare>(root); }
116 
124  Node * insert(Node *p) noexcept
125  {
126  return BinTree_Operation<Node, Compare>(cmp).insert(root, p);
127  }
128 
136  Node * insert_dup(Node *p) noexcept
137  {
138  return BinTree_Operation<Node, Compare>(cmp).insert_dup(root, p);
139  }
140 
153  Node * search_or_insert(Node *p) noexcept
154  {
156  }
157 
171  bool split(const Key & key, GenBinTree & l, GenBinTree & r) noexcept
172  {
174  split_key_rec(root, key, l.root, r.root);
175  }
176 
187  void split_dup(const Key & key, GenBinTree & l, GenBinTree & r) noexcept
188  {
190  split_key_dup_rec(root, key, l.root, r.root);
191  }
192 
199  Node * remove(const Key & key) noexcept
200  {
201  return BinTree_Operation<Node, Compare>(cmp).remove(root, key);
202  }
203 
211  void join(GenBinTree & tree, GenBinTree & dup) noexcept
212  {
213  root = BinTree_Operation<Node, Compare>(cmp).join(root, tree.root, dup);
214  tree.root = Node::NullPtr;
215  }
216 
225  void join_dup(GenBinTree & t) noexcept
226  {
227  root = BinTree_Operation<Node, Compare>(cmp).join_dup(root, t.root);
228  t.tree_root = Node::NullPtr;
229  }
230 
242  void join_exclusive(GenBinTree & t) noexcept
243  {
244  root = join_exclusive(root, t.root);
245  t.root = Node::NullPtr;
246  }
247 
254  struct Iterator : public BinNodeInfixIterator<Node>
255  {
257  };
258 };
259 
266  template <typename Key, class Compare = Aleph::less<Key>>
267 struct BinTree : public GenBinTree<BinNode, Key, Compare>
268 {
270  using Base::Base;
271 };
272 
273 
280  template <typename Key, class Compare = Aleph::less<Key>>
281 struct BinTreeVtl : public GenBinTree<BinNodeVtl, Key, Compare>
282 {
284  using Base::Base;
285 };
286 
287 
288 } // end namespace Aleph
289 # endif /* TPL_BINTREE_H */
290 
Definition: tpl_binNodeUtils.H:2445
bool split(const Key &key, GenBinTree &l, GenBinTree &r) noexcept
Definition: tpl_binTree.H:171
Definition: tpl_binTree.H:254
void join_dup(GenBinTree &t) noexcept
Definition: tpl_binTree.H:225
Node *& getRoot()
Return the root of tree.
Definition: tpl_binTree.H:112
Node * search_or_insert(Node *p) noexcept
Definition: tpl_binTree.H:153
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, Compare cmp=Compare()) noexcept
Definition: tpl_binNodeUtils.H:1643
Compare & key_comp() noexcept
return the comparison criteria
Definition: tpl_binTree.H:83
Definition: tpl_binTree.H:267
Definition: tpl_binTree.H:56
GenBinTree(Compare __cmp=Compare()) noexcept
Initialize an empty tree with comparison criteria __cmp
Definition: tpl_binTree.H:89
Definition: ah-comb.H:35
void join_exclusive(GenBinTree &t) noexcept
Definition: tpl_binTree.H:242
Node * search(const Key &key) const noexcept
Definition: tpl_binTree.H:101
void join(GenBinTree &tree, GenBinTree &dup) noexcept
Definition: tpl_binTree.H:211
void split_dup(const Key &key, GenBinTree &l, GenBinTree &r) noexcept
Definition: tpl_binTree.H:187
bool verifyBin() const
Definition: tpl_binTree.H:115
void split_key_dup_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, Compare cmp=Compare()) noexcept
Definition: tpl_binNodeUtils.H:1686
bool verify() const
Return true if the tree is a consistent (correct) binary search tree.
Definition: tpl_binTree.H:109
Definition: tpl_binTreeOps.H:41
Definition: tpl_binTree.H:281
Node * insert(Node *p) noexcept
Definition: tpl_binTree.H:124
void swap(GenBinTree &tree) noexcept
Swap this with tree in constant time.
Definition: tpl_binTree.H:76
Node * insert_dup(Node *p) noexcept
Definition: tpl_binTree.H:136
Compare & get_compare() noexcept
Definition: tpl_binTree.H:86

Leandro Rabindranath León