27 #ifndef MPBLOCKS_BTPS_TREE_H_
28 #define MPBLOCKS_BTPS_TREE_H_
43 template <
class Traits>
63 return ops_.CumulativeWeight(N);
67 return ops_.Weight(N);
156 if (
count(p) == 1)
return p;
241 template <
typename T>
246 auto wMiddle = wLeft +
weight(x);
254 else if (val < wMiddle ||
right(x) ==
nil_)
268 if (x ==
nil_)
return 0;
332 depthMap[0].push_back(
root_);
333 for (
int i = 1;
true; i++) {
334 if (depthMap[i - 1].
size() < 1)
break;
335 for (
NodeRef node : depthMap[i - 1]) {
336 if (
left(node) !=
nil_) depthMap[i].push_back(
left(node));
344 template <
typename T>
NodeRef findInterval(NodeRef x, T val)
given $ val [0,1] $, sample a node from the weighted distribution over the subtree rooted at x ...
NodeRef root()
return the root node in the tree
void insert(NodeRef p, NodeRef x)
recursively insert x in the subtree rooted at p
void removeLeaf(NodeRef x)
remove a leaf node from the tree
auto sum() -> decltype(ops_.CumulativeWeight(root_))
return the total weight of all nodes in the tree
void swap(NodeRef a, NodeRef b)
swaps everything but the weight and cumulative weight
auto weight(NodeRef N) -> decltype(ops_.Weight(N))
double validateTree()
return the largest difference between cum(x) and the computed value over all nodes in the tree ...
size_t size()
return the count of nodes in the tree
implements a binary tree of partial sums for sampling from discrete distributions with arbitrary weig...
auto count(NodeRef N) -> decltype(ops_.Count(N))
bool isLeaf(NodeRef x)
true if x is a leaf node (i.e. both children are Nil)
void update(NodeRef x)
update a node's count and cumulative weight by summing the count/weight of itself with the cumulative...
NodeRef findInterval(T val)
given $ val [0,1] $, sample a node from the weighted distribution
NodeRef deepestLeaf(NodeRef p)
find the deepest leaf in a subtree
NodeRef & right(NodeRef N)
void updateAncestry(NodeRef p)
walk the tree along the parent path from p to the root and update all nodes on that path ...
void generateDepthProfile(std::map< int, std::list< NodeRef > > &depthMap)
fill depthMap with a list of nodes at each depth
void insert(NodeRef x)
insert a single node into the tree, weight(x) must be set, all other fields are initialized by the tr...
Tree(NodeRef Nil)
initialize a tree using Nil as the sentinal object
NodeRef & left(NodeRef N)
NodeRef & parent(NodeRef N)
void clear()
clear out the tree (does not free nodes)
auto cum(NodeRef N) -> decltype(ops_.CumulativeWeight(N))
double validateNode(NodeRef x)
return the larger of the error of cum(x) or the error of it's children