Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_binTree.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
50# ifndef TPL_BINTREE_H
51# define TPL_BINTREE_H
52
53# include <utility>
54
55# include <tpl_binTreeOps.H>
56
57using namespace Aleph;
58
59namespace Aleph {
60
134 template <template <typename> class NodeType, typename Key, class Compare>
136{
137public:
138
140
141private:
142
146 Compare cmp;
147
148 template <class Inserter>
149 static void move_all(Node *& src, Inserter inserter) noexcept
150 {
151 auto rec = [&] (auto && self, Node * n) noexcept -> void
152 {
153 if (n == Node::NullPtr)
154 return;
155
156 Node * l = LLINK(n);
157 Node * r = RLINK(n);
158 n->reset();
159 inserter(n);
160 self(self, l);
161 self(self, r);
162 };
163
164 Node * n = src;
165 src = Node::NullPtr;
166 rec(rec, n);
167 }
168
169public:
170
171 GenBinTree(const GenBinTree & ) = delete;
172
173 GenBinTree & operator = (const GenBinTree &) = delete;
174
176 void swap(GenBinTree & tree) noexcept
177 {
178 std::swap(root, tree.root);
179 std::swap(cmp, tree.cmp);
180 }
181
183 Compare & key_comp() noexcept { return cmp; }
184
186 Compare & get_compare() noexcept { return key_comp(); }
187
189 GenBinTree(Compare _cmp = Compare()) noexcept
190 : head(&headNode), root(headNode.getR()), cmp(_cmp)
191 {
192 // empty
193 }
194
201 Node * search(const Key & key) const noexcept
202 {
203 return BinTree_Operation<Node, Compare>(cmp).search(root, key);
204 }
205
206 virtual ~GenBinTree() = default;
207
209 bool verify() const { return check_bst<Node, Compare>(root, cmp); }
210
212 Node *& getRoot() noexcept { return root; }
213
216
218 bool verifyBin() const { return check_bst<Node, Compare>(root, cmp); }
219
227 Node * insert(Node *p) noexcept
228 {
230 }
231
239 Node * insert_dup(Node *p) noexcept
240 {
241 return BinTree_Operation<Node, Compare>(cmp).insert_dup(root, p);
242 }
243
257 {
258 return BinTree_Operation<Node, Compare>(cmp).search_or_insert(root, p);
259 }
260
274 bool split(const Key & key, GenBinTree & l, GenBinTree & r) noexcept
275 {
277 split_key_rec(root, key, l.root, r.root);
278 }
279
290 void split_dup(const Key & key, GenBinTree & l, GenBinTree & r) noexcept
291 {
293 }
294
301 Node * remove(const Key & key) noexcept
302 {
304 }
305
313 void join(GenBinTree & tree, GenBinTree & dup) noexcept
314 {
316 move_all(tree.root, [&] (Node * n) noexcept
317 {
318 if (op.insert(root, n) == Node::NullPtr)
319 op.insert(dup.root, n);
320 });
321 }
322
331 void join_dup(GenBinTree & t) noexcept
332 {
334 move_all(t.root, [&] (Node * n) noexcept { op.insert_dup(root, n); });
335 }
336
348 void join_exclusive(GenBinTree & t) noexcept
349 {
351 t.root = Node::NullPtr;
352 }
353
360 struct Iterator : public BinNodeInfixIterator<Node>
361 {
365 Iterator(const GenBinTree & tree)
366 : BinNodeInfixIterator<Node>(tree.getRoot()) {}
367 };
368};
369
376 template <typename Key, class Compare = Aleph::less<Key>>
377struct BinTree : public GenBinTree<BinNode, Key, Compare>
378{
380 using Base::Base;
381};
382
383
390 template <typename Key, class Compare = Aleph::less<Key>>
391struct BinTreeVtl : public GenBinTree<BinNodeVtl, Key, Compare>
392{
394 using Base::Base;
395};
396
397
398} // end namespace Aleph
399# endif /* TPL_BINTREE_H */
400
Inorder iterator on the nodes of a binary tree.
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T remove()
Remove the first item of the list.
Definition htlist.H:1611
Simple (unbalanced) binary search tree.
GenBinTree & operator=(const GenBinTree &)=delete
static void move_all(Node *&src, Inserter inserter) noexcept
void join_dup(GenBinTree &t) noexcept
Join this with t independently of the presence of duplicated keys.
Node headNode
The node.
GenBinTree(const GenBinTree &)=delete
Node * getRoot() const noexcept
Return the root of tree.
Node *& getRoot() noexcept
Return the root of tree.
bool split(const Key &key, GenBinTree &l, GenBinTree &r) noexcept
Split the tree according to a key.
bool verify() const
Return true if the tree is a consistent (correct) binary search tree.
Node * insert(Node *p) noexcept
Insert a node in the tree.
Compare & key_comp() noexcept
return the comparison criteria
void split_dup(const Key &key, GenBinTree &l, GenBinTree &r) noexcept
Split the tree according to a key that could be in the tree.
Node * search(const Key &key) const noexcept
Search a key.
GenBinTree(Compare _cmp=Compare()) noexcept
Initialize an empty tree with comparison criteria __cmp
void join(GenBinTree &tree, GenBinTree &dup) noexcept
Join tree with this.
Compare & get_compare() noexcept
Node * insert_dup(Node *p) noexcept
Insert a node in the tree.
Node * remove(const Key &key) noexcept
Remove a key from the tree.
NodeType< Key > Node
void join_exclusive(GenBinTree &t) noexcept
Join exclusive of this with t
void swap(GenBinTree &tree) noexcept
Swap this with tree in constant time.
virtual ~GenBinTree()=default
bool verifyBin() const
Node * search_or_insert(Node *p) noexcept
Search or insert a key.
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept
Split recursively according to a key.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Binary search tree with nodes with virtual destructors,.
Binary search tree with nodes without virtual destructors,.
Iterator on nodes of the tree.
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Iterator(const GenBinTree &tree)
Binary tree operations (split, join, rotate).
DynList< int > l