38#include <gtest/gtest.h>
58template <
typename NodeT>
61 std::vector<int> keys;
62 if (
root == NodeT::NullPtr)
66 keys.insert(keys.end(), left.begin(), left.end());
69 keys.insert(keys.end(), right.begin(), right.end());
88 auto *p =
new Node(key);
139 auto *p = pool.make(10);
154 for (
int k : {5, 3, 7, 1, 4, 6, 8})
161 EXPECT_EQ(keys, (std::vector<int>{1, 3, 4, 5, 6, 7, 8}));
169 auto *p1 = pool.make(10);
170 auto *p2 = pool.make(10);
185 for (
int i = 0; i < 5; ++i)
197 for (
int k = 1;
k <= 100; ++
k)
214 for (
int k = 100;
k >= 1; --
k)
235 for (
int k : {1, 2, 3, 4, 5})
238 for (
int k : {1, 2, 3, 4, 5})
251 for (
int k : {1, 3, 5})
265 auto *p1 = pool.make(10);
268 auto *p2 = pool.make(10);
283 tree.
insert(pool.make(5));
285 auto *p = pool.make(10);
302 for (
int k : {1, 2, 3, 4, 5})
317 EXPECT_EQ(keys, (std::vector<int>{1, 2, 4, 5}));
325 tree.
insert(pool.make(1));
326 tree.
insert(pool.make(3));
337 auto *
root = pool.make(5);
339 tree.
insert(pool.make(3));
340 tree.
insert(pool.make(7));
357 for (
int k : {5, 3, 7, 1, 4, 6, 8})
360 for (
int k : {5, 3, 7, 1, 4, 6, 8})
377 for (
int k = 1;
k <= 10; ++
k)
380 for (
int k = 1;
k <= 10; ++
k)
397 for (
int k = 1;
k <= 10; ++
k)
400 for (
int k = 10;
k >= 1; --
k)
421 for (
int k : {5, 3, 7, 1, 4, 6, 8})
439 tree.
insert(pool.make(1));
440 tree.
insert(pool.make(2));
451 for (
int k : {5, 3, 7, 1, 4, 6, 8})
473 for (
int k : {2, 4, 6})
476 auto [pos, node] = tree.
position(3);
485 for (
int k : {2, 4, 6, 8})
498 for (
int k : {2, 4, 6, 8})
513 for (
int k : {2, 4, 6})
526 for (
int k : {2, 4, 6})
543 for (
int k : {5, 3, 7, 1, 4, 6, 8})
563 for (
int k : {5, 3, 7})
581 for (
int k : {5, 3, 7})
609 for (
int k : {2, 4, 6, 8, 10})
631 for (
int k : {2, 4, 6, 8, 10})
645 for (
int k : {2, 4, 6, 8, 10})
665 for (
int k : {1, 2, 3, 4, 5})
690 for (
int k : {1, 3, 5})
692 for (
int k : {2, 4, 6})
703 EXPECT_EQ(keys, (std::vector<int>{1, 2, 3, 4, 5, 6}));
713 for (
int k : {1, 3, 5})
715 for (
int k : {3, 4, 5})
725 EXPECT_EQ(keys, (std::vector<int>{1, 3, 4, 5}));
738 for (
int k : {1, 3, 5})
740 for (
int k : {3, 4, 5})
757 for (
int k : {1, 2, 3})
760 for (
int k : {10, 11, 12})
770 EXPECT_EQ(keys, (std::vector<int>{1, 2, 3, 10, 11, 12}));
782 for (
int k : {5, 3, 7, 1, 4, 6, 8})
785 std::vector<int> result;
787 result.push_back(
KEY(it.get_curr()));
789 EXPECT_EQ(result, (std::vector<int>{1, 3, 4, 5, 6, 7, 8}));
805 for (
int k : {1, 2, 3, 4, 5})
812 std::vector<int> result;
814 result.push_back(
KEY(it.get_curr()));
816 EXPECT_EQ(result, (std::vector<int>{1, 2, 4, 5}));
829 for (
int k : {1, 2, 3})
831 for (
int k : {10, 20})
854 for (
int k : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
878 using NodeGt = TreeGt::Node;
882 std::vector<NodeGt *>
nodes;
883 for (
int k : {1, 2, 3, 4, 5})
894 std::vector<int> result;
895 for (TreeGt::Iterator it(tree); it.has_curr(); it.next())
896 result.push_back(
KEY(it.get_curr()));
898 EXPECT_EQ(result, (std::vector<int>{5, 4, 3, 2, 1}));
901 for (
int k : {1, 2, 3, 4, 5})
902 delete tree.remove(
k);
914 for (
int k : {-5, -3, -1, 0, 1, 3, 5})
921 EXPECT_EQ(keys, (std::vector<int>{-5, -3, -1, 0, 1, 3, 5}));
934 auto *p = pool.make(42);
940 auto [pos, node] = tree.
position(42);
962 std::mt19937
rng(12345);
963 std::uniform_int_distribution<int> dist(0, 999);
966 for (
int i = 0; i < 500; ++i)
980 for (
int i = 0; i < 200; ++i)
991 for (
int i = 0; i < 200; ++i)
1024 for (
int k = 0;
k <
N; ++
k)
1031 for (
int i = 0; i < 10; ++i)
1035 for (
int k = 0;
k <
N;
k += 2)
1047 for (
int k = 1;
k <
N;
k += 2)
1058 using NodeVtl = TreeVtl::Node;
1062 for (
int k : {1, 2, 3, 4, 5})
1069 for (
int k : {1, 2, 3, 4, 5})
1070 delete tree.remove(
k);
1084 for (
int k : {5, 3, 7, 1, 4, 6, 8})
1106 tree.
insert(pool.make(10));
1140 tree1.set_seed(999);
1141 tree2.set_seed(999);
1143 for (
int k : {1, 2, 3, 4, 5})
1158static_assert(!std::is_copy_assignable_v<Tree>,
1159 "Rand_Tree should not be copy assignable");
1161static_assert(!std::is_copy_constructible_v<Tree>,
1162 "Rand_Tree should not be copy constructible");
WeightedDigraph::Node Node
bool has_curr() const noexcept
Return true the iterator has current node.
T & insert(const T &item)
Insert a new item by copy.
DynList & swap(DynList &l) noexcept
Swap this with l.
bool is_empty() const noexcept
Return true if the tree is empty.
bool verify() const
Return true if this is a consistent randomized tree.
Node * remove(const Key &key) noexcept
Remove a key from the tree.
Node * search_or_insert(Node *p) noexcept
Search or insert a key.
Compare & key_comp() noexcept
return the comparison criteria
Node * insert_dup(Node *p) noexcept
Insert a node in the tree.
void split_pos(size_t pos, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2) noexcept
Split a extended binary tree according to a position.
std::pair< long, Node * > find_position(const Key &key) const noexcept
Find the inorder position of a key in the tree.
Compare & get_compare() noexcept
bool split_key(const Key &key, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2) noexcept
Split the tree according to a key.
Node * insert(Node *p) noexcept
Insert a node in the tree.
void split_key_dup(const Key &key, Gen_Rand_Tree &t1, Gen_Rand_Tree &t2) noexcept
Split the tree according to a key that could be in the tree.
size_t size() const noexcept
Return the number of nodes that have the tree.
Node * select(const size_t i) const
Return the i-th node in inorder sense.
gsl_rng * gsl_rng_object() noexcept
Get a pointer to gsl random number generator.
Node *& getRoot() noexcept
Return the tree's root.
Node * search(const Key &key) const noexcept
Search a key.
Node * remove_pos(Node *&root, const size_t pos) noexcept
size_t size() const noexcept
Count the number of elements of the list.
std::vector< Node * > nodes
Generator for uniformly random trees.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
__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)
DynArray< Graph::Node * > nodes
std::pair< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
std::vector< int > inorder_keys(NodeT *root)
Iterator on nodes of the tree.
Randomized binary search tree.
Utility functions for binary tree operations.
Randomized binary search tree.