43#include <gtest/gtest.h>
48using namespace testing;
57 std::vector<Node *> allocated;
59 Node *
make(
const typename Node::key_type & key)
62 allocated.push_back(p);
68 for (
auto & q : allocated)
78 for (
Node * p : allocated)
86 std::vector<typename Node::key_type> keys;
97 const std::vector<int>
input{5, 3, 7, 2, 4, 6, 8};
122 auto * a = pool.make(5);
123 auto * b = pool.make(5);
128 auto * c = pool.make(5);
162 auto * dup = pool.make(25);
207 for (
int k : {50, 25, 75, 12, 37, 62, 87, 6, 18, 31, 43})
221 for (
int k : {31, 37, 25, 50})
247 for (
int k : {1, 2, 3})
258 for (
int k : {3, 1, 4, 2})
279 std::mt19937
rng(12345);
280 std::uniform_int_distribution<int> dist(0, 500);
284 for (
int i = 0; i < 300; ++i)
287 auto * p = pool.make(
k);
300 for (
int i = 0; i < 200; ++i)
362 std::mt19937
rng(123456);
363 std::uniform_int_distribution<int> dist(0, 2000);
364 std::bernoulli_distribution
do_insert(0.6);
368 for (
int i = 0; i < 1500; ++i)
373 auto * p = pool.make(
k);
405 bool operator()(
int a,
int b)
const noexcept {
return a > b; }
411 for (
int k : {1, 2, 3, 4, 5})
416 const std::vector<int>
expected{5, 4, 3, 2, 1};
439 for (
int i = 0; i < n; ++i)
441 ASSERT_NE(t.
insert(pool.make(i)),
nullptr) <<
"Insert failed at i=" << i;
446 for (
int i = 0; i < n; ++i)
458 for (
int i = n - 1; i >= 0; --i)
464 for (
int i = 0; i < n; ++i)
477 for (
int i = 0; i < n / 2; ++i)
485 for (
int i = 0; i < n; ++i)
495 std::mt19937
rng(99999);
496 std::uniform_int_distribution<int>
key_dist(0, 50000);
497 std::uniform_int_distribution<int>
op_dist(0, 2);
503 for (
int i = 0; i <
num_ops; ++i)
512 auto * p = pool.make(key);
572 std::vector<int> keys(n);
573 std::iota(keys.begin(), keys.end(), 0);
575 std::mt19937
rng(12345);
576 std::shuffle(keys.begin(), keys.end(),
rng);
586 std::shuffle(keys.begin(), keys.end(),
rng);
625 EXPECT_TRUE(std::is_sorted(keys.begin(), keys.end()));
635 std::mt19937
rng(54321);
636 std::uniform_int_distribution<int> dist(0, 10000);
640 for (
int i = 0; i < n; ++i)
646 auto * p = pool.make(
k);
647 if (t.
insert(p) ==
nullptr)
681 std::mt19937
rng(11111);
682 std::uniform_int_distribution<int>
len_dist(1, 30);
683 std::uniform_int_distribution<int>
char_dist(
'a',
'z');
689 for (
int i = 0; i < len; ++i)
694 std::set<std::string>
oracle;
698 for (
int i = 0; i < n; ++i)
701 auto * p = pool.make(s);
702 if (t.
insert(p) !=
nullptr)
714 for (
const auto & s :
oracle)
static string random_string(std::mt19937 &rng, size_t len)
bool is_avl(Node *p)
Validate that a tree satisfies AVL properties.
#define DIFF(p)
Access the balance factor of node p.
WeightedDigraph::Node Node
T & insert(const T &item)
Insert a new item by copy.
Node * search(const Key &key) const noexcept
Search a node containing key; if found, then a pointer to the node containing it is returned; otherwi...
Node * search_or_insert(Node *p) noexcept
Search or insert a key.
Node * insert_dup(Node *p) noexcept
Insert the node p without testing for key duplicity.
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
bool verify() const noexcept
Node * remove(const Key &key) noexcept
Remove from an AVL tree the node containing key key.
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)
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)
AVL binary search tree with nodes without virtual destructor.
AVL tree implementation (height-balanced BST).