51#include <gtest/gtest.h>
85 auto *node =
new Node(key);
90 std::vector<Node*>
make_nodes(
const std::vector<int>& keys)
92 std::vector<Node*>
nodes;
122 auto *node =
new Node(key);
129 std::vector<Node*>
nodes;
151 Node *node = make_node(42);
162 std::vector<int> keys = {50, 25, 75, 10, 30, 60, 90};
163 auto nodes = make_nodes(keys);
165 for (
auto *node :
nodes)
171 EXPECT_NE(tree.search(key),
nullptr) <<
"Key " << key <<
" not found";
190 auto nodes = make_nodes({10, 20, 30});
191 for (
auto *node :
nodes)
201 Node *node = make_node(42);
213 auto nodes = make_nodes({50, 25, 75});
214 for (
auto *node :
nodes)
230 auto nodes = make_nodes({50, 25, 75, 10, 30, 60, 90});
231 for (
auto *node :
nodes)
241 for (
int key : {50, 75, 10, 30, 60, 90})
242 EXPECT_NE(tree.search(key),
nullptr) <<
"Key " << key <<
" missing";
249 auto nodes = make_nodes({50, 25, 75});
250 for (
auto *node :
nodes)
264 auto nodes = make_nodes({10, 20, 30});
265 for (
auto *node :
nodes)
275 std::vector<int> keys = {50, 25, 75, 10, 30, 60, 90};
276 auto nodes = make_nodes(keys);
278 for (
auto *node :
nodes)
282 std::vector<int>
removal_order = {30, 10, 90, 50, 25, 75, 60};
285 EXPECT_NE(tree.remove(key),
nullptr) <<
"Failed to remove " << key;
286 EXPECT_TRUE(tree.verifyRedBlack()) <<
"RB violation after removing " << key;
299 auto nodes = make_nodes({10, 20, 30});
300 for (
auto *node :
nodes)
314 auto node1 = make_node(10);
315 auto node2 = make_node(20);
316 auto node3 = make_node(30);
340 auto nodes = make_nodes({10, 20, 30, 40, 50});
342 for (
auto *node :
nodes)
348 for (
int key : {10, 20, 30, 40, 50})
359 bool operator()(
int a,
int b)
const {
return std::abs(a) < std::abs(b); }
364 std::vector<RbNode<int>*>
nodes;
365 auto make = [&
nodes](
int k) {
385 for (
auto *n :
nodes)
396 std::vector<RbNode<std::string>*>
nodes;
398 auto make = [&
nodes](
const std::string& s) {
404 tree.
insert(make(
"banana"));
405 tree.
insert(make(
"apple"));
406 tree.
insert(make(
"cherry"));
407 tree.
insert(make(
"date"));
420 for (
auto *n :
nodes)
430 for (
int i = 1; i <= 100; ++i)
431 tree.insert(make_node(i));
436 for (
int i = 1; i <= 100; ++i)
442 for (
int i = 100; i >= 1; --i)
443 tree.
insert(make_node(i));
448 for (
int i = 1; i <= 100; ++i)
480 std::set<int> reference;
481 std::vector<RbNode<int>*> all_nodes;
483 std::mt19937
rng(12345);
484 std::uniform_int_distribution<int>
key_dist(1, 10000);
485 std::uniform_int_distribution<int>
op_dist(0, 2);
487 auto make = [&all_nodes](
int k) {
489 all_nodes.push_back(n);
495 for (
int i = 0; i <
NUM_OPS; ++i)
502 auto *node = make(key);
506 <<
"Insert mismatch at op " << i <<
" key " << key;
513 <<
"Remove mismatch at op " << i <<
" key " << key;
518 bool ref_found = (reference.count(key) > 0);
520 <<
"Search mismatch at op " << i <<
" key " << key;
531 for (
auto *n : all_nodes)
538 std::vector<RbNode<int>*>
nodes;
543 for (
int i = 0; i <
N; ++i)
546 nodes.push_back(node);
554 for (
int i = 0; i <
N; ++i)
558 for (
int i = 0; i <
N; i += 2)
567 for (
int i = 0; i <
N; ++i)
575 for (
auto *n :
nodes)
589 std::vector<Rb_Tree<int>::Node*>
bu_nodes;
591 std::mt19937
rng(42);
592 std::uniform_int_distribution<int> dist(1, 1000);
595 std::vector<int> keys;
622 std::shuffle(keys.begin(), keys.end(),
rng);
652 Node *node = make_node(42);
669 auto nodes = make_nodes({-50, -25, 0, 25, 50});
671 for (
auto *node :
nodes)
676 for (
int key : {-50, -25, 0, 25, 50})
684 auto nodes = make_nodes({
685 std::numeric_limits<int>::min(),
686 std::numeric_limits<int>::max(),
690 for (
auto *node :
nodes)
693 EXPECT_NE(tree.search(std::numeric_limits<int>::min()),
nullptr);
694 EXPECT_NE(tree.search(std::numeric_limits<int>::max()),
nullptr);
706 std::vector<RbNodeVtl<int>*>
nodes;
708 auto make = [&
nodes](
int k) {
731 for (
auto *n :
nodes)
742 std::vector<RbNode<int>*>
nodes;
744 auto make = [&
nodes](
int k) {
767 for (
auto *n :
nodes)
774 std::vector<RbNode<int>*>
nodes;
776 auto make = [&
nodes](
int k) {
797 for (
auto *n :
nodes)
808 std::vector<RbNode<int>*>
nodes;
810 auto make = [&
nodes](
int k) {
826 for (
auto *n :
nodes)
833 std::vector<RbNode<int>*>
nodes;
835 auto make = [&
nodes](
int k) {
851 for (
auto *n :
nodes)
862 std::vector<RbNode<int>*>
nodes;
864 auto make = [&
nodes](
int k) {
870 auto *
node1 = make(42);
876 for (
auto *n :
nodes)
883 std::vector<RbNode<int>*>
nodes;
885 auto make = [&
nodes](
int k) {
891 auto *
node1 = make(42);
892 auto *
node2 = make(42);
900 for (
auto *n :
nodes)
911 std::vector<RbNode<int>*>
nodes;
913 auto make = [&
nodes](
int k) {
933 std::vector<int>
expected = {10, 25, 30, 50, 60, 75, 90};
936 for (
auto *n :
nodes)
969 ::testing::InitGoogleTest(&
argc,
argv);
High-level sorting functions for Aleph containers.
WeightedDigraph::Node Node
T & insert(const T &item)
Insert a new item by copy.
T remove()
Remove the first item of the list.
DynList & swap(DynList &l) noexcept
Swap this with l.
Node * insert_dup(Node *p) noexcept
Insert a node, allowing duplicates.
size_t size() const noexcept
Get number of nodes in tree (O(1))
Node * remove(const Key &key) noexcept
Remove and return node with given key.
Node * search_or_insert(Node *p) noexcept
Search for key or insert if not found.
Node * search(const Key &key) const noexcept
Search for a key in the tree.
bool verifyRedBlack() const noexcept
Verify that tree satisfies all red-black properties.
Node * insert(Node *p) noexcept
Insert a node into the tree.
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.
Top-down red-black tree with virtual destructor.
Declare RbNode type with 128-byte pool allocation.
std::vector< Node * > allocated_nodes
Node * make_node(int key)
std::vector< Node * > make_nodes(const std::vector< int > &keys)
Node * make_node(int key)
std::vector< Node * > make_nodes(const std::vector< int > &keys)
std::vector< Node * > allocated_nodes
DynArray< Graph::Node * > nodes
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.
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
DynList< T > maps(const C &c, Op op)
Classic map operation.
#define RED
Red color constant (newly inserted nodes are red)
Red-black tree with nodes without virtual destructor.
bool operator()(int a, int b) const
TEST_F(TdRbTreeTest, EmptyTree)
Red-Black tree implementation (bottom-up balancing).
Top-down Red-Black tree implementation.