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# include <ah-concepts.H>
57
58using namespace Aleph;
59
60namespace Aleph {
61
135 template <template <typename> class NodeType, typename Key, class Compare>
138{
139public:
140
142
143private:
144
148 Compare cmp;
149
150 template <class Inserter>
151 static void move_all(Node *& src, Inserter inserter) noexcept
152 {
153 auto rec = [&] (auto && self, Node * n) noexcept -> void
154 {
155 if (n == Node::NullPtr)
156 return;
157
158 Node * l = LLINK(n);
159 Node * r = RLINK(n);
160 n->reset();
161 inserter(n);
162 self(self, l);
163 self(self, r);
164 };
165
166 Node * n = src;
167 src = Node::NullPtr;
168 rec(rec, n);
169 }
170
171public:
172
173 GenBinTree(const GenBinTree & ) = delete;
174
175 GenBinTree & operator = (const GenBinTree &) = delete;
176
178 void swap(GenBinTree & tree) noexcept
179 {
180 std::swap(root, tree.root);
181 std::swap(cmp, tree.cmp);
182 }
183
185 Compare & key_comp() noexcept { return cmp; }
186
188 Compare & get_compare() noexcept { return key_comp(); }
189
191 GenBinTree(Compare _cmp = Compare()) noexcept
192 : head(&headNode), root(headNode.getR()), cmp(_cmp)
193 {
194 // empty
195 }
196
203 Node * search(const Key & key) const noexcept
204 {
205 return BinTree_Operation<Node, Compare>(cmp).search(root, key);
206 }
207
208 virtual ~GenBinTree() = default;
209
211 bool verify() const { return check_bst<Node, Compare>(root, cmp); }
212
214 Node *& getRoot() noexcept { return root; }
215
218
220 bool verifyBin() const { return check_bst<Node, Compare>(root, cmp); }
221
229 Node * insert(Node *p) noexcept
230 {
231 return BinTree_Operation<Node, Compare>(cmp).insert(root, p);
232 }
233
241 Node * insert_dup(Node *p) noexcept
242 {
243 return BinTree_Operation<Node, Compare>(cmp).insert_dup(root, p);
244 }
245
259 {
260 return BinTree_Operation<Node, Compare>(cmp).search_or_insert(root, p);
261 }
262
276 bool split(const Key & key, GenBinTree & l, GenBinTree & r) noexcept
277 {
279 split_key_rec(root, key, l.root, r.root);
280 }
281
292 void split_dup(const Key & key, GenBinTree & l, GenBinTree & r) noexcept
293 {
295 }
296
303 Node * remove(const Key & key) noexcept
304 {
305 return BinTree_Operation<Node, Compare>(cmp).remove(root, key);
306 }
307
315 void join(GenBinTree & tree, GenBinTree & dup) noexcept
316 {
318 move_all(tree.root, [&] (Node * n) noexcept
319 {
320 if (op.insert(root, n) == Node::NullPtr)
321 op.insert(dup.root, n);
322 });
323 }
324
333 void join_dup(GenBinTree & t) noexcept
334 {
336 move_all(t.root, [&] (Node * n) noexcept { op.insert_dup(root, n); });
337 }
338
350 void join_exclusive(GenBinTree & t) noexcept
351 {
353 t.root = Node::NullPtr;
354 }
355
362 struct Iterator : public BinNodeInfixIterator<Node>
363 {
367 Iterator(const GenBinTree & tree)
368 : BinNodeInfixIterator<Node>(tree.getRoot()) {}
369 };
370};
371
378 template <typename Key, class Compare = Aleph::less<Key>>
380struct BinTree : public GenBinTree<BinNode, Key, Compare>
381{
383 using Base::Base;
384};
385
386
393 template <typename Key, class Compare = Aleph::less<Key>>
395struct BinTreeVtl : public GenBinTree<BinNodeVtl, Key, Compare>
396{
398 using Base::Base;
399};
400
401
402} // end namespace Aleph
403# endif /* TPL_BINTREE_H */
404
C++20 concepts for constraining comparison functors.
Inorder iterator on the nodes of a binary tree.
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.
Strict weak ordering constraint for BST comparators.
Definition ah-concepts.H:84
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.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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)
#define RLINK(i, n)
#define LLINK(i, n)
gsl_rng * r
Binary tree operations (split, join, rotate).
DynList< int > l