This page provides an example of how to use the weight balanced tree. For information on what a weight balanced tree is and how it behaves, please see Overview over the Data Structures.
The code can also be found in the examples directory.
#include "../src/ygg.hpp"
using MyTreeOptions =
TreeOptions<TreeFlags::MULTIPLE, TreeFlags::WBT_DELTA_NUMERATOR<3>,
TreeFlags::WBT_DELTA_DENOMINATOR<1>,
TreeFlags::WBT_GAMMA_NUMERATOR<2>,
TreeFlags::WBT_GAMMA_DENOMINATOR<1>>;
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;
}