Yggdrasill
0.2
|
This page will guide you through the minimum steps necessary to use ygg::RBTree and ygg::IntervalTree.
Setting up your own RBTree basically consists of three to five steps:
This example (which uses the defaults for steps 3 and 4 above) demonstrates it, assuming that you want to store a simple integer-to-string map in the tree:
In fact, we could have skipped defining our own TreeOptions here, since TreeFlags::MULTIPLE is the default.
Inserting values into the tree is then a three-step process:
Again, a simple example (based on the example above), where we just add a couple of nodes. Note that in this example, we already use the feature that we can query for anything that is comparable to a Node (in this case, we make ints comparable to Nodes first). See TODO for details.
Using the IntervalTree is similar to using the RBTree. You need to implement your own node. However, Intervals can not just be compared to each other using operator<(…)
, but the IntervalTree has to have access to the actual upper and lower bounds of the intervals. This is achieved by implementing the get_upper()
and get_lower()
methods, and setting a key_type
typedef (or using) in the NodeTraits. You have to implement NodeTraits yourself for the IntervalTree (as opposed to the Red-Black-Tree). However, these are the only three things you have to implement. See TODO for details on implementing NodeTraits.
Again, an example demonstrates setting up an IntervalTree, this time mapping intervals to strings . Note that this time we do not specify our own TreeOptions, but use the default value.
Setting up a tree and inserting intervals is very similar to an RBTree:
Querying is a bit different from the RBTree. Since an IntervalTree is just an RBTree, you can of course again use find() to find an interval that you have previously inserted, but the really interesting queries are overlap queries.
Before explaining them, let me show you how the concept of types 'Comparable' to the nodes works with IntervalTrees. For a type T to be comparable to nodes, in RBTrees, you needed to implement operator<
. However, that's not used in IntervalTrees, so what to do? Easy: Your NodeTraits class must provide get_upper()
and get_lower()
methods for the comparable type T, too.
Let's say we want to represent intervals by a std::pair<int, int>
, and we want to make this comparable to our Nodes. We would change the NodeTraits like so:
Now you can query for nodes overlapping a certain interval: