Aleph-w 3.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
81{
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
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());
102}
103
105{
106 BinTree<int> t;
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);
116 EXPECT_TRUE(t.verify());
117
118 ASSERT_NE(t.insert(removed), nullptr);
119 EXPECT_TRUE(t.verify());
120}
121
123{
124 BinTree<int> t;
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
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
143{
144 BinTree<int> t;
146 for (int k : {1, 2, 3, 4, 5})
147 ASSERT_NE(t.insert(pool.make(k)), nullptr);
148
150 BinTree<int> r;
151 ASSERT_TRUE(t.split(0, l, r));
152
154 EXPECT_TRUE(l.verify());
155 EXPECT_TRUE(r.verify());
156
157 auto lkeys = inorder_keys<BinNode<int>>(l.getRoot());
160 EXPECT_EQ(rkeys, (std::vector<int>{1, 2, 3, 4, 5}));
161}
162
164{
165 BinTree<int> t;
167 for (int k : {1, 2, 3, 4, 5})
168 ASSERT_NE(t.insert(pool.make(k)), nullptr);
169
171 BinTree<int> r;
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
179{
180 BinTree<int> t;
182 for (int k : {1, 2, 3, 4, 5})
183 ASSERT_NE(t.insert(pool.make(k)), nullptr);
184
186 BinTree<int> r;
187 t.split_dup(3, l, r);
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
196{
197 BinTree<int> a;
198 BinTree<int> b;
199 BinTree<int> dup;
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
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
218{
219 BinTree<int> a;
220 BinTree<int> b;
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);
230 EXPECT_TRUE(a.verify());
231
232 EXPECT_EQ(inorder_keys<BinNode<int>>(a.getRoot()), (std::vector<int>{1, 3, 3, 4}));
233}
234
236{
237 BinTree<int> t;
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
247{
248 BinTree<int> t;
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);
260 EXPECT_EQ(KEY(ret2), 2);
261 EXPECT_TRUE(t.verify());
262}
263
265{
266 BinTree<int> a;
267 BinTree<int> b;
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
282{
283 BinTree<int> a;
284 BinTree<int> b;
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());
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 empty() noexcept
empty the list
Definition htlist.H:1689
void join_dup(GenBinTree &t) noexcept
Join this with t independently of the presence of duplicated keys.
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.
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.
void join(GenBinTree &tree, GenBinTree &dup) noexcept
Join tree with this.
Node * remove(const Key &key) noexcept
Remove a key from the tree.
void join_exclusive(GenBinTree &t) noexcept
Join exclusive of this with t
void swap(GenBinTree &tree) noexcept
Swap this with tree in constant time.
Node * search_or_insert(Node *p) noexcept
Search or insert a key.
NodeType< Key > Node
Node * make(int key)
Definition rand-tree.cc:86
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#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:4060
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.
std::vector< int > inorder_keys(NodeT *root)
Definition rand-tree.cc:59
Binary search tree with nodes without virtual destructors,.
Generic unbalanced binary search tree.
DynList< int > l