41#include <gtest/gtest.h>
46using namespace testing;
55 std::vector<Node *> allocated;
57 Node *
make(
const typename Node::Key_Type & key)
60 allocated.push_back(p);
66 for (
Node * p : allocated)
74 std::vector<typename Node::Key_Type>
keys;
75 Aleph::infix_for_each<Node>(
root, [&] (
Node * p) {
keys.push_back(
KEY(p)); });
83 NodePool<BinTree<int>> pool;
85 const std::vector<int> input{5, 3, 7, 2, 4, 6, 8};
87 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
91 auto * found = t.
search(4);
92 ASSERT_NE(found,
nullptr);
93 EXPECT_EQ(
KEY(found), 4);
95 std::vector<int> iterated;
97 iterated.push_back(
KEY(it.get_curr_ne()));
107 NodePool<BinTree<int>> pool;
108 for (
int k : {3, 1, 4, 2})
109 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
111 auto * removed = t.
remove(1);
112 ASSERT_NE(removed,
nullptr);
113 EXPECT_EQ(
KEY(removed), 1);
118 ASSERT_NE(t.
insert(removed),
nullptr);
125 NodePool<BinTree<int>> pool;
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);
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);
138 auto keys = inorder_keys<BinNode<int>>(t2.
getRoot());
139 EXPECT_EQ(
keys, (std::vector<int>{5, 5}));
145 NodePool<BinTree<int>> pool;
146 for (
int k : {1, 2, 3, 4, 5})
147 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
154 EXPECT_TRUE(
l.verify());
155 EXPECT_TRUE(
r.verify());
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}));
166 NodePool<BinTree<int>> pool;
167 for (
int k : {1, 2, 3, 4, 5})
168 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
172 ASSERT_FALSE(t.
split(3,
l,
r));
181 NodePool<BinTree<int>> pool;
182 for (
int k : {1, 2, 3, 4, 5})
183 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
189 EXPECT_TRUE(
l.verify());
190 EXPECT_TRUE(
r.verify());
200 NodePool<BinTree<int>> pool;
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);
211 EXPECT_TRUE(dup.
verify());
221 NodePool<BinTree<int>> pool;
224 ASSERT_NE(a.
insert(pool.make(
k)),
nullptr);
226 ASSERT_NE(b.
insert(pool.make(
k)),
nullptr);
238 NodePool<BinTree<int>> pool;
239 for (
int k : {1, 2, 3})
240 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
242 EXPECT_EQ(t.
remove(42),
nullptr);
249 NodePool<BinTree<int>> pool;
251 auto * p = pool.make(2);
256 auto * other = pool.make(2);
258 ASSERT_NE(ret2,
nullptr);
259 EXPECT_NE(ret2, other);
260 EXPECT_EQ(
KEY(ret2), 2);
268 NodePool<BinTree<int>> pool;
270 ASSERT_NE(a.
insert(pool.make(1)),
nullptr);
271 ASSERT_NE(b.
insert(pool.make(2)),
nullptr);
285 NodePool<BinTree<int>> pool;
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);
WeightedDigraph::Node Node
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.
__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)
Main namespace for Aleph-w library functions.
std::vector< int > inorder_keys(NodeT *root)
Binary search tree with nodes without virtual destructors,.
Generic unbalanced binary search tree.