40# include <gtest/gtest.h>
54template <
typename Tree>
61 static constexpr size_t N = 1000;
107 for (
size_t i = 0; i <
N; ++i)
109 auto p = tree.select(i);
111 EXPECT_EQ(p->get_key(),
static_cast<int>(i));
120 for (
size_t i = 0; i <
N; ++i)
122 auto [pos, node] = tree.position(
static_cast<int>(i));
125 EXPECT_EQ(node->get_key(),
static_cast<int>(i));
134 auto [pos, node] = tree.position(
static_cast<int>(
N + 100));
144 for (
size_t i = 0; i <
N / 2; ++i)
146 auto p = tree.remove(keys[i]);
162 auto p = tree.remove(
k);
169 for (
size_t i = 0; i <
N / 2; ++i)
171 auto p = tree.select(i);
173 EXPECT_EQ(p->get_key(),
static_cast<int>(2 * i + 1));
192 for (
size_t i = 0; i <
N; ++i)
194 auto p = tree.select(i);
196 EXPECT_EQ(p->get_key(),
static_cast<int>(i));
205 for (
size_t i = 0; i <
N; ++i)
207 auto [pos, node] = tree.position(
static_cast<int>(i));
210 EXPECT_EQ(node->get_key(),
static_cast<int>(i));
219 auto [pos, node] = tree.position(
static_cast<int>(
N + 100));
229 for (
size_t i = 0; i <
N / 2; ++i)
231 auto p = tree.remove(keys[i]);
247 auto p = tree.remove(
k);
254 for (
size_t i = 0; i <
N / 2; ++i)
256 auto p = tree.select(i);
258 EXPECT_EQ(p->get_key(),
static_cast<int>(2 * i + 1));
268 const size_t N = 5000;
271 for (
size_t i = 0; i <
N; ++i)
273 auto p =
new Node(
static_cast<int>(i));
282 for (
int i = 0; i < 100; ++i)
284 size_t pos = dist(
rng);
285 auto p = tree.
select(pos);
286 EXPECT_EQ(p->get_key(),
static_cast<int>(pos));
290 for (
int i = 0; i < 100; ++i)
292 int key =
static_cast<int>(dist(
rng));
293 auto [pos, node] = tree.
position(key);
313 const size_t N = 5000;
316 for (
size_t i = 0; i <
N; ++i)
318 auto p =
new Node(
static_cast<int>(i));
327 for (
int i = 0; i < 100; ++i)
329 size_t pos = dist(
rng);
330 auto p = tree.
select(pos);
331 EXPECT_EQ(p->get_key(),
static_cast<int>(pos));
335 for (
int i = 0; i < 100; ++i)
337 int key =
static_cast<int>(dist(
rng));
338 auto [pos, node] = tree.
position(key);
365 auto [pos, node] = tree.
position(42);
378 auto [pos, node] = tree.
position(42);
389 auto p =
new Node(42);
395 auto selected = tree.
select(0);
398 auto [pos, node] = tree.
position(42);
411 auto p =
new Node(42);
417 auto selected = tree.
select(0);
420 auto [pos, node] = tree.
position(42);
435 auto p1 =
new Node(42);
440 auto p2 =
new Node(42);
454 auto p1 =
new Node(42);
459 auto p2 =
new Node(42);
475 for (
int i = 0; i < 10; ++i)
477 auto p =
new Node(42);
485 for (
size_t i = 0; i < 10; ++i)
497 for (
int i = 0; i < 10; ++i)
499 auto p =
new Node(42);
507 for (
size_t i = 0; i < 10; ++i)
519template <
typename Tree>
534 for (
int i = 0; i < 50; ++i)
536 for (
int i = 50; i < 100; ++i)
544 t1.join_exclusive(
t2);
551 for (
int i = 0; i < 100; ++i)
553 auto p =
t1.select(i);
565 for (
int i = 0; i < 50; ++i)
568 t1.join_exclusive(
t2);
582 for (
int i = 0; i < 50; ++i)
585 t1.join_exclusive(
t2);
599 for (
int i = 0; i < 100; ++i)
614 for (
int i = 0; i < 50; ++i)
618 for (
int i = 0; i < 49; ++i)
632 for (
int i = 0; i < 100; i += 2)
657 for (
int i = 0; i < 100; ++i)
670 for (
int i = 0; i < 30; ++i)
674 for (
int i = 0; i < 70; ++i)
686 for (
int i = 0; i < 50; ++i)
704 for (
int i = 0; i < 50; ++i)
722 for (
int i = 0; i < 50; ++i)
724 for (
int i = 50; i < 100; ++i)
727 t1.join_exclusive(
t2);
750 for (
int i = 0; i < 50; ++i)
752 for (
int i = 50; i < 100; ++i)
760 t1.join_exclusive(
t2);
766 for (
int i = 0; i < 100; ++i)
768 auto p =
t1.select(i);
780 for (
int i = 0; i < 50; ++i)
783 t1.join_exclusive(
t2);
797 for (
int i = 0; i < 50; ++i)
800 t1.join_exclusive(
t2);
814 for (
int i = 0; i < 100; ++i)
828 for (
int i = 0; i < 50; ++i)
831 for (
int i = 0; i < 49; ++i)
844 for (
int i = 0; i < 100; i += 2)
866 for (
int i = 0; i < 100; ++i)
878 for (
int i = 0; i < 30; ++i)
881 for (
int i = 0; i < 70; ++i)
893 for (
int i = 0; i < 50; ++i)
911 for (
int i = 0; i < 50; ++i)
929 for (
int i = 0; i < 50; ++i)
931 for (
int i = 50; i < 100; ++i)
934 t1.join_exclusive(
t2);
958 for (
int i = 0; i <
N / 2; ++i)
960 for (
int i =
N / 2; i <
N; ++i)
963 t1.join_exclusive(
t2);
973 t3.join_exclusive(
t4);
986 for (
int i = 0; i <
N / 2; ++i)
988 for (
int i =
N / 2; i <
N; ++i)
991 t1.join_exclusive(
t2);
1001 t3.join_exclusive(
t4);
1016 for (
int i = 0; i < 50; ++i)
1018 for (
int i = 0; i < 10; ++i)
1020 for (
int i = 51; i < 100; ++i)
1045 for (
int i = 0; i < 50; ++i)
1047 for (
int i = 0; i < 10; ++i)
1049 for (
int i = 51; i < 100; ++i)
1070 ::testing::InitGoogleTest(&
argc,
argv);
TEST_F(AvlRkTest, InsertAndVerify)
void destroy_tree(Tree &tree)
WeightedDigraph::Node Node
T & insert(const T &item)
Insert a new item by copy.
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Node * select(const size_t i) const
Return the i-th node in order sense.
Node * search_or_insert(Node *p) noexcept
Search or insert a key.
Node * remove(const Key &key) noexcept
Remove from tree the node containing key.
size_t size() const noexcept
Return the number of nodes in the tree.
constexpr bool is_empty() const noexcept
Return true if tree is empty.
Node * insert_dup(Node *p) noexcept
Insert the node p without testing for key duplicity.
void split_key_dup(const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by key including duplicates.
Node * split_key(const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by key.
void split_pos(const size_t pos, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by inorder position.
bool verify() const noexcept
Return true if the tree is a valid AVL tree with correct counters.
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...
std::pair< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
bool is_empty() const noexcept
Return true if the tree is empty.
Node * remove(const Key &key) noexcept
Remove a key from the tree.
Node * insert(Node *p) noexcept
Insert a node in the tree.
Node *& getRoot() noexcept
Return the tree's root.
Node * split_key(const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by key.
size_t size() const noexcept
Node * select(const size_t i) const
Return the i-th node in order sense.
Node * insert_dup(Node *p)
bool is_empty() const noexcept
Node * search(const Key &key)
void split_pos(size_t pos, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by inorder position.
Node * search_or_insert(Node *p)
void split_key_dup(const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by key including duplicates.
Node *& getRoot() noexcept
Node * remove(const Key &key)
std::pair< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
static constexpr size_t N
Main namespace for Aleph-w library functions.
auto shuffle(const C< T > &c)
Randomly shuffle a sequence.
DynList< T > maps(const C &c, Op op)
Classic map operation.
AVL binary search tree with nodes without virtual destructor and with subtree counters for select/pos...
Red-Black binary search tree with nodes without virtual destructor and with subtree counters for sele...
AVL tree with rank (order statistics).
Red-Black tree with rank (order statistics).