Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bintree.cc
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
38#include <algorithm>
39#include <vector>
40
41#include <gtest/gtest.h>
42
43#include <tpl_binTree.H>
44
45using namespace Aleph;
46using namespace testing;
47
48namespace
49{
50 template <class Tree>
51 struct NodePool
52 {
53 using Node = typename Tree::Node;
54
55 std::vector<Node *> allocated;
56
57 Node * make(const typename Node::Key_Type & key)
58 {
59 Node * p = new Node(key);
60 allocated.push_back(p);
61 return p;
62 }
63
64 ~NodePool()
65 {
66 for (Node * p : allocated)
67 delete p;
68 }
69 };
70
71 template <class Node>
72 std::vector<typename Node::Key_Type> inorder_keys(Node * root)
73 {
74 std::vector<typename Node::Key_Type> keys;
75 Aleph::infix_for_each<Node>(root, [&] (Node * p) { keys.push_back(KEY(p)); });
76 return keys;
77 }
78}
79
80TEST(BinTree, InsertSearchVerifyAndIteratorOrder)
81{
83 NodePool<BinTree<int>> pool;
84
85 const std::vector<int> input{5, 3, 7, 2, 4, 6, 8};
86 for (int k : input)
87 ASSERT_NE(t.insert(pool.make(k)), nullptr);
88
89 EXPECT_TRUE(t.verify());
90
91 auto * found = t.search(4);
92 ASSERT_NE(found, nullptr);
93 EXPECT_EQ(KEY(found), 4);
94
95 std::vector<int> iterated;
96 for (BinTree<int>::Iterator it(t); it.has_curr(); it.next_ne())
97 iterated.push_back(KEY(it.get_curr_ne()));
98
99 std::vector<int> expected = input;
100 std::sort(expected.begin(), expected.end());
101 EXPECT_EQ(iterated, expected);
102}
103
104TEST(BinTree, RemoveReturnsNodeAndAllowsReinsert)
105{
106 BinTree<int> t;
107 NodePool<BinTree<int>> pool;
108 for (int k : {3, 1, 4, 2})
109 ASSERT_NE(t.insert(pool.make(k)), nullptr);
110
111 auto * removed = t.remove(1);
112 ASSERT_NE(removed, nullptr);
113 EXPECT_EQ(KEY(removed), 1);
114 EXPECT_EQ(LLINK(removed), BinNode<int>::NullPtr);
115 EXPECT_EQ(RLINK(removed), BinNode<int>::NullPtr);
116 EXPECT_TRUE(t.verify());
117
118 ASSERT_NE(t.insert(removed), nullptr);
119 EXPECT_TRUE(t.verify());
120}
121
122TEST(BinTree, InsertRejectsDuplicatesButInsertDupAllows)
123{
124 BinTree<int> t;
125 NodePool<BinTree<int>> pool;
126
127 auto * a = pool.make(5);
128 auto * b = pool.make(5);
129 ASSERT_NE(t.insert(a), nullptr);
130 EXPECT_EQ(t.insert(b), nullptr);
131
132 BinTree<int> t2;
133 NodePool<BinTree<int>> pool2;
134 ASSERT_NE(t2.insert_dup(pool2.make(5)), nullptr);
135 ASSERT_NE(t2.insert_dup(pool2.make(5)), nullptr);
136 EXPECT_TRUE(t2.verify());
137
138 auto keys = inorder_keys<BinNode<int>>(t2.getRoot());
139 EXPECT_EQ(keys, (std::vector<int>{5, 5}));
140}
141
142TEST(BinTree, SplitKeyNotPresentEmptiesSource)
143{
144 BinTree<int> t;
145 NodePool<BinTree<int>> pool;
146 for (int k : {1, 2, 3, 4, 5})
147 ASSERT_NE(t.insert(pool.make(k)), nullptr);
148
151 ASSERT_TRUE(t.split(0, l, r));
152
153 EXPECT_EQ(t.getRoot(), BinNode<int>::NullPtr);
154 EXPECT_TRUE(l.verify());
155 EXPECT_TRUE(r.verify());
156
157 auto lkeys = inorder_keys<BinNode<int>>(l.getRoot());
158 auto rkeys = inorder_keys<BinNode<int>>(r.getRoot());
159 EXPECT_TRUE(lkeys.empty());
160 EXPECT_EQ(rkeys, (std::vector<int>{1, 2, 3, 4, 5}));
161}
162
163TEST(BinTree, SplitKeyPresentReturnsFalseAndKeepsTree)
164{
165 BinTree<int> t;
166 NodePool<BinTree<int>> pool;
167 for (int k : {1, 2, 3, 4, 5})
168 ASSERT_NE(t.insert(pool.make(k)), nullptr);
169
172 ASSERT_FALSE(t.split(3, l, r));
173
174 EXPECT_TRUE(t.verify());
175 EXPECT_EQ(inorder_keys<BinNode<int>>(t.getRoot()), (std::vector<int>{1, 2, 3, 4, 5}));
176}
177
178TEST(BinTree, SplitDupAlwaysSplits)
179{
180 BinTree<int> t;
181 NodePool<BinTree<int>> pool;
182 for (int k : {1, 2, 3, 4, 5})
183 ASSERT_NE(t.insert(pool.make(k)), nullptr);
184
187 t.split_dup(3, l, r);
188 EXPECT_EQ(t.getRoot(), BinNode<int>::NullPtr);
189 EXPECT_TRUE(l.verify());
190 EXPECT_TRUE(r.verify());
191 EXPECT_EQ(inorder_keys<BinNode<int>>(l.getRoot()), (std::vector<int>{1, 2}));
192 EXPECT_EQ(inorder_keys<BinNode<int>>(r.getRoot()), (std::vector<int>{3, 4, 5}));
193}
194
195TEST(BinTree, JoinMovesNodesAndCollectsDuplicates)
196{
197 BinTree<int> a;
198 BinTree<int> b;
199 BinTree<int> dup;
200 NodePool<BinTree<int>> pool;
201
202 for (int k : {1, 3, 5})
203 ASSERT_NE(a.insert(pool.make(k)), nullptr);
204 for (int k : {2, 3, 4})
205 ASSERT_NE(b.insert(pool.make(k)), nullptr);
206
207 a.join(b, dup);
208
209 EXPECT_EQ(b.getRoot(), BinNode<int>::NullPtr);
210 EXPECT_TRUE(a.verify());
211 EXPECT_TRUE(dup.verify());
212
213 EXPECT_EQ(inorder_keys<BinNode<int>>(a.getRoot()), (std::vector<int>{1, 2, 3, 4, 5}));
214 EXPECT_EQ(inorder_keys<BinNode<int>>(dup.getRoot()), (std::vector<int>{3}));
215}
216
217TEST(BinTree, JoinDupAllowsDuplicates)
218{
219 BinTree<int> a;
220 BinTree<int> b;
221 NodePool<BinTree<int>> pool;
222
223 for (int k : {1, 3})
224 ASSERT_NE(a.insert(pool.make(k)), nullptr);
225 for (int k : {3, 4})
226 ASSERT_NE(b.insert(pool.make(k)), nullptr);
227
228 a.join_dup(b);
229 EXPECT_EQ(b.getRoot(), BinNode<int>::NullPtr);
230 EXPECT_TRUE(a.verify());
231
232 EXPECT_EQ(inorder_keys<BinNode<int>>(a.getRoot()), (std::vector<int>{1, 3, 3, 4}));
233}
234
235TEST(BinTree, RemoveMissingReturnsNull)
236{
237 BinTree<int> t;
238 NodePool<BinTree<int>> pool;
239 for (int k : {1, 2, 3})
240 ASSERT_NE(t.insert(pool.make(k)), nullptr);
241
242 EXPECT_EQ(t.remove(42), nullptr);
243 EXPECT_TRUE(t.verify());
244}
245
246TEST(BinTree, SearchOrInsertReturnsExistingOrInserted)
247{
248 BinTree<int> t;
249 NodePool<BinTree<int>> pool;
250
251 auto * p = pool.make(2);
252 auto * ret1 = t.search_or_insert(p);
253 ASSERT_EQ(ret1, p);
254 EXPECT_TRUE(t.verify());
255
256 auto * other = pool.make(2);
257 auto * ret2 = t.search_or_insert(other);
258 ASSERT_NE(ret2, nullptr);
259 EXPECT_NE(ret2, other);
260 EXPECT_EQ(KEY(ret2), 2);
261 EXPECT_TRUE(t.verify());
262}
263
264TEST(BinTree, SwapExchangesRoots)
265{
266 BinTree<int> a;
267 BinTree<int> b;
268 NodePool<BinTree<int>> pool;
269
270 ASSERT_NE(a.insert(pool.make(1)), nullptr);
271 ASSERT_NE(b.insert(pool.make(2)), nullptr);
272
273 a.swap(b);
274 EXPECT_TRUE(a.verify());
275 EXPECT_TRUE(b.verify());
276
277 EXPECT_EQ(inorder_keys<BinNode<int>>(a.getRoot()), (std::vector<int>{2}));
278 EXPECT_EQ(inorder_keys<BinNode<int>>(b.getRoot()), (std::vector<int>{1}));
279}
280
281TEST(BinTree, JoinExclusiveConcatenatesWhenRangesAreDisjoint)
282{
283 BinTree<int> a;
284 BinTree<int> b;
285 NodePool<BinTree<int>> pool;
286
287 for (int k : {1, 2, 3})
288 ASSERT_NE(a.insert(pool.make(k)), nullptr);
289 for (int k : {4, 5, 6})
290 ASSERT_NE(b.insert(pool.make(k)), nullptr);
291
292 a.join_exclusive(b);
293 EXPECT_TRUE(a.verify());
294 EXPECT_EQ(b.getRoot(), BinNode<int>::NullPtr);
295 EXPECT_EQ(inorder_keys<BinNode<int>>(a.getRoot()), (std::vector<int>{1, 2, 3, 4, 5, 6}));
296}
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
Node for binary search tree.
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.
bool verify() const
Return true if the tree is a consistent (correct) binary search 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.
void join(GenBinTree &tree, GenBinTree &dup) noexcept
Join tree with this.
void join_exclusive(GenBinTree &t) noexcept
Join exclusive of this with t
Node * search(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Search a key.
Minimal std::expected-style result type for C++20.
Node * make(int key)
Definition rand-tree.cc:88
QuadNode Node
Definition quadtree.H:128
#define TEST(name)
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
Main namespace for Aleph-w library functions.
std::vector< int > inorder_keys(NodeT *root)
Definition rand-tree.cc:61
Binary search tree with nodes without virtual destructors,.
#define RLINK(i, n)
int keys[]
#define LLINK(i, n)
static int * k
Gsl_Compatible_Rng r
Generic unbalanced binary search tree.
DynList< int > l