This page provides an example of how to use the red-black tree. For information on what a red-black tree is and how it behaves, please see Overview over the Data Structures.
The code can also be found in the examples directory.
#include "ygg.hpp"
using MyTreeOptions = TreeOptions<TreeFlags::MULTIPLE>;
public:
int key;
std::string value;
bool operator<(const Node & other) const {
return this->key < other.key;
}
};
bool operator<(const Node & lhs, const int rhs) {
return lhs.key < rhs;
}
bool operator<(const int lhs, const Node & rhs) {
return lhs < rhs.key;
}
int main(int argc, char **argv) {
(void)argc;
(void)argv;
MyTree t;
Node nodes[5];
for (int i = 0 ; i < 5 ; ++i) {
nodes[i].key = i;
nodes[i].value = std::string("The key is ") + std::to_string(i);
}
for (size_t i = 0 ; i < 5 ; ++i) {
t.insert(nodes[i]);
}
auto it = t.find(3);
assert(it != t.end());
std::string retrieved_value = it->value;
t.remove(*it);
return 0;
}