Aleph-w 7.10.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>
136 requires StrictWeakOrder<Compare, Key>
138{
139public:
140
141 using Node = NodeType<Key>;
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
184 void swap(GenBinTree & tree) noexcept(std::is_nothrow_swappable_v<Compare>)
185 {
186 // The comparison first: if its swap throws, the nodes stay where
187 // they were
188 std::swap(cmp, tree.cmp);
189 std::swap(root, tree.root);
190 }
191
193 Compare & key_comp() noexcept { return cmp; }
194
196 Compare & get_compare() noexcept { return key_comp(); }
197
212 noexcept(std::is_nothrow_default_constructible_v<Node> and
213 std::is_nothrow_default_constructible_v<Compare> and
214 std::is_nothrow_constructible_v<Compare, Compare &>)
215 : GenBinTree(Compare())
216 {
217 // empty
218 }
219
228 GenBinTree(Compare _cmp)
229 noexcept(std::is_nothrow_default_constructible_v<Node> and
230 std::is_nothrow_constructible_v<Compare, Compare &>)
231 : head(&headNode), root(headNode.getR()), cmp(_cmp)
232 {
233 // empty
234 }
235
254 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
255 bin_node_detail::nothrow_compare_taken_v<Compare>)
256 : headNode(bin_node_detail::header_from(tree.headNode)),
257 head(&headNode), root(headNode.getR()),
258 cmp(bin_node_detail::compare_to_take(tree.cmp))
259 {
260 root = tree.root;
261 tree.root = Node::NullPtr;
262 }
263
274 Node * search(const Key & key) const
275 noexcept(nothrow_compare_copy_v<Compare, Key>)
276 {
278 }
279
280 virtual ~GenBinTree() = default;
281
283 bool verify() const { return check_bst<Node, Compare>(root, cmp); }
284
286 Node *& getRoot() noexcept { return root; }
287
289 Node * getRoot() const noexcept { return root; }
290
292 bool verifyBin() const { return check_bst<Node, Compare>(root, cmp); }
293
306 Node * insert(Node *p) noexcept(nothrow_compare_copy_v<Compare, Key>)
307 {
309 }
310
323 Node * insert_dup(Node *p) noexcept(nothrow_compare_copy_v<Compare, Key>)
324 {
326 }
327
346 noexcept(nothrow_compare_copy_v<Compare, Key>)
347 {
349 }
350
369 bool split(const Key & key, GenBinTree & l, GenBinTree & r)
370 noexcept(nothrow_compare_copy_v<Compare, Key>)
371 {
373 split_key_rec(root, key, l.root, r.root);
374 }
375
390 void split_dup(const Key & key, GenBinTree & l, GenBinTree & r)
391 noexcept(nothrow_compare_v<Compare, Key>)
392 {
393 Aleph::split_key_dup_rec<Node, Compare>(root, key, l.root, r.root, cmp);
394 }
395
407 Node * remove(const Key & key) noexcept(nothrow_compare_copy_v<Compare, Key>)
408 {
410 }
411
424 void join(GenBinTree & tree, GenBinTree & dup) noexcept
425 {
427 move_all(tree.root, [&] (Node * n) noexcept
428 {
429 if (op.insert(root, n) == Node::NullPtr)
430 op.insert(dup.root, n);
431 });
432 }
433
448 void join_dup(GenBinTree & t) noexcept
449 {
451 move_all(t.root, [&] (Node * n) noexcept { op.insert_dup(root, n); });
452 }
453
465 void join_exclusive(GenBinTree & t) noexcept
466 {
467 root = Aleph::join_exclusive<Node>(root, t.root);
468 t.root = Node::NullPtr;
469 }
470
477 struct Iterator : public BinNodeInfixIterator<Node>
478 {
480 Iterator() noexcept = default;
482 Iterator(const GenBinTree & tree)
483 : BinNodeInfixIterator<Node>(tree.getRoot()) {}
484 };
485};
486
493 template <typename Key, class Compare = Aleph::less<Key>>
494 requires StrictWeakOrder<Compare, Key>
495struct BinTree : public GenBinTree<BinNode, Key, Compare>
496{
498 using Base::Base;
499};
500
501
508 template <typename Key, class Compare = Aleph::less<Key>>
510struct BinTreeVtl : public GenBinTree<BinNodeVtl, Key, Compare>
511{
513 using Base::Base;
514};
515
516
517} // end namespace Aleph
518# endif /* TPL_BINTREE_H */
519
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Inorder iterator on the nodes of a binary tree.
Functor encompassing basic operation for binary search trees.
Node * insert(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert a node p in a binary search tree.
Node * search_or_insert(Node *&r, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Search or insert a node in a binary search tree.
Node * insert_dup(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert a node p in a binary search tree.
Node * search(Node *root, const Key &key) noexcept(nothrow_compare_v< Cmp, Key >)
Search a node with a specific key.
Node * remove(Node *&root, const Key &key) noexcept(nothrow_compare_v< Cmp, Key >)
Remove a key from a binary search tree.
Simple (unbalanced) binary search tree.
GenBinTree & operator=(const GenBinTree &)=delete
static void move_all(Node *&src, Inserter inserter) noexcept
void split_dup(const Key &key, GenBinTree &l, GenBinTree &r) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key that could be in the tree.
Node * insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
void join_dup(GenBinTree &t) noexcept
Join this with t independently of the presence of duplicated keys.
Node headNode
The node.
bool verify() const
Return true if the tree is a consistent (correct) binary search tree.
GenBinTree(const GenBinTree &)=delete
GenBinTree(Compare _cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria _cmp.
Node * getRoot() const noexcept
Return the root of tree.
Node *& getRoot() noexcept
Return the root of tree.
Node * insert_dup(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
void swap(GenBinTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
bool split(const Key &key, GenBinTree &l, GenBinTree &r) noexcept(nothrow_compare_copy_v< Compare, Key >)
Split the tree according to a key.
Node * remove(const Key &key) noexcept(nothrow_compare_copy_v< Compare, Key >)
Remove a key from the tree.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Search or insert a key.
GenBinTree() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with a comparison criteria built by default.
void join(GenBinTree &tree, GenBinTree &dup) noexcept
Join tree with this.
GenBinTree(GenBinTree &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
Compare & get_compare() noexcept
See key_comp().
NodeType< Key > Node
void join_exclusive(GenBinTree &t) noexcept
Join exclusive of this with t
Compare & key_comp() noexcept
return the comparison criteria
Node * search(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Search a key.
virtual ~GenBinTree()=default
bool verifyBin() const
See verify().
Strict weak ordering constraint for BST comparators.
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split recursively according to a key.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
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_Compatible_Rng r
Binary tree operations (split, join, rotate).
DynList< int > l