44#include <gtest/gtest.h>
49using namespace testing;
58 bool operator()(
int a,
int b)
const
60 return std::abs(a) < std::abs(b);
66 std::vector<Node *> allocated;
70 auto * p =
new Node(
k);
71 allocated.push_back(p);
77 for (
auto & q : allocated)
87 for (
auto * p : allocated)
94 if (
root == Node::NullPtr)
103 std::vector<int>
keys;
104 if (
root == Node::NullPtr)
108 keys.insert(
keys.end(), left.begin(), left.end());
111 keys.insert(
keys.end(), right.begin(), right.end());
117 ASSERT_TRUE(tree.verify()) <<
"Rank tree invariant violated";
118 auto *
root = tree.getRoot();
119 if (
root != Node::NullPtr)
134 EXPECT_EQ(tree.getRoot(), Node::NullPtr);
144 auto * p = pool.make(42);
145 auto * inserted = tree.
insert(p);
159 for (
int k : {5, 3, 7, 1, 4, 6, 8})
161 auto * p = pool.make(
k);
177 auto * p1 = pool.make(10);
180 auto * p2 = pool.make(10);
192 for (
int i = 0; i < 5; ++i)
193 ASSERT_NE(tree.insert_dup(pool.make(42)),
nullptr);
207 for (
int k : {1, 2, 3, 4, 5})
210 for (
int k : {1, 2, 3, 4, 5})
225 for (
int k : {1, 3, 5})
242 auto * p1 = pool.make(10);
243 auto *
ret1 = tree.search_or_insert(p1);
248 auto * p2 = pool.make(10);
249 auto *
ret2 = tree.search_or_insert(p2);
266 for (
int k : {1, 2, 3, 4, 5})
288 for (
int k : {1, 3, 5})
312 tree.
insert(pool.make(1));
313 tree.
insert(pool.make(2));
314 tree.
insert(pool.make(3));
332 std::vector<int>
keys = {5, 3, 7, 1, 4, 6, 8};
358 for (
int k : {5, 3, 7, 1, 4, 6, 8})
362 std::vector<int>
expected = {1, 3, 4, 5, 6, 7, 8};
364 for (
size_t i = 0; i <
expected.size(); ++i)
366 auto * node = tree.select(i);
367 ASSERT_NE(node, Node::NullPtr) <<
"select(" << i <<
") returned null";
377 for (
int k : {1, 2, 3})
389 for (
int k : {5, 3, 7, 1, 4, 6, 8})
393 std::vector<std::pair<int, long>>
expected = {
394 {1, 0}, {3, 1}, {4, 2}, {5, 3}, {6, 4}, {7, 5}, {8, 6}
399 auto result = tree.position(key);
400 EXPECT_EQ(result.first, pos) <<
"position(" << key <<
") wrong";
411 for (
int k : {2, 4, 6})
414 auto result = tree.position(3);
425 auto result = tree.position(42);
438 for (
int k : {1, 2, 3, 4, 5})
462 for (
int k : {5, 3, 7, 1, 4, 6, 8})
468 for (
int k : {1, 8, 4, 6, 3, 7, 5})
485 auto * p = pool.make(42);
504 for (
int k = 10;
k >= 1; --
k)
511 EXPECT_EQ(
keys, (std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
519 for (
int k = 1;
k <= 10; ++
k)
526 EXPECT_EQ(
keys, (std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
536 using NodeGt = TreeGt::Node;
539 std::vector<NodeGt *>
nodes;
541 for (
int k : {1, 2, 3, 4, 5})
552 std::vector<int>
keys;
554 if (
r == NodeGt::NullPtr)
return;
563 for (
auto * p :
nodes)
576 auto * found = tree.
search(-1);
594 std::mt19937
rng(42);
595 std::uniform_int_distribution<int> dist(0, 500);
598 for (
int i = 0; i < 200; ++i)
601 auto * p = pool.make(
k);
602 if (tree.
insert(p) !=
nullptr)
619 for (
int i = 0; i < 100; ++i)
635 for (
int i = 0; i < 150; ++i)
664 std::mt19937
rng(123);
665 std::uniform_int_distribution<int> dist(0, 1000);
668 for (
int i = 0; i < 100; ++i)
671 auto * p = pool.make(
k);
672 if (tree.
insert(p) !=
nullptr)
683 for (
size_t i = 0; i <
sorted.size(); ++i)
685 auto * node = tree.select(i);
704 for (
int k : {5, 3, 7, 1, 4, 6, 8})
727 for (
int k : {1, 2, 3})
730 for (
int k : {10, 20})
731 tree2.insert(pool.make(
k));
755 for (
int k = 0;
k <
N; ++
k)
762 EXPECT_EQ(tree.size(),
static_cast<size_t>(
N));
766 for (
int i = 0; i < 100; ++i)
768 int pos =
rand() %
N;
769 auto * node = tree.select(pos);
781 for (
int k =
N - 1;
k >= 0; --
k)
784 EXPECT_EQ(tree.size(),
static_cast<size_t>(
N));
794 for (
int i = 0; i <
N; ++i)
796 int k = (i % 2 == 0) ? i / 2 :
N - 1 - i / 2;
800 EXPECT_EQ(tree.size(),
static_cast<size_t>(
N));
810 std::mt19937
gen(98765);
811 std::uniform_int_distribution<>
key_dist(0, 20000);
812 std::uniform_int_distribution<>
op_dist(0, 2);
821 auto * p = pool.make(key);
822 if (tree.
insert(p) !=
nullptr)
830 else if (op == 1 && !
oracle.empty())
844 auto * found = tree.
search(key);
853 if (
iter % 2000 == 0)
868 for (
int k = 0;
k <
N; ++
k)
871 EXPECT_EQ(tree.size(),
static_cast<size_t>(
N));
877 std::mt19937
gen(11111);
899 for (
int k = 0;
k <
N; ++
k)
900 for (
int d = 0; d <
DUPS; ++d)
901 tree.insert_dup(pool.make(
k));
909 while (!tree.is_empty())
912 auto * node = tree.select(0);
937 std::mt19937
gen(22222);
938 std::uniform_int_distribution<>
key_dist(0, 5000);
941 for (
int i = 0; i < 2000; ++i)
944 auto * p = pool.make(key);
945 if (tree.
insert(p) !=
nullptr)
957 for (
size_t i = 0; i <
sorted.size(); ++i)
959 auto * node = tree.select(i);
976 for (
int k = 0;
k <
N; ++
k)
979 std::mt19937
gen(33333);
980 std::uniform_int_distribution<> dist(0,
N - 1);
984 for (
int i = 0; i < 5000; ++i)
987 auto * found = tree.
search(key);
WeightedDigraph::Node Node
Top-down splay tree with rank support.
QuadTree - Hierarchical spatial index for 2D points.
void remove(const Point &p)
Remove a point from the tree.
Point * search(const Point &p) noexcept
Search for a point in the tree.
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
__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)
void delete_tree(Tree_Node< T > *node)
DynArray< Graph::Node * > nodes
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
Main namespace for Aleph-w library functions.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::vector< int > inorder_keys(NodeT *root)
Top-down splay tree with rank support.