mlpack
greedy_single_tree_traverser_impl.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_CORE_TREE_GREEDY_SINGLE_TREE_TRAVERSER_IMPL_HPP
15 #define MLPACK_CORE_TREE_GREEDY_SINGLE_TREE_TRAVERSER_IMPL_HPP
16 
17 // In case it hasn't been included yet.
19 
20 namespace mlpack {
21 namespace tree {
22 
23 template<typename TreeType, typename RuleType>
25  RuleType& rule) :
26  rule(rule),
27  numPrunes(0)
28 { /* Nothing to do. */ }
29 
30 template<typename TreeType, typename RuleType>
32  const size_t queryIndex,
33  TreeType& referenceNode)
34 {
35  // Run the base case as necessary for all the points in the reference node.
36  for (size_t i = 0; i < referenceNode.NumPoints(); ++i)
37  rule.BaseCase(queryIndex, referenceNode.Point(i));
38 
39  size_t bestChild = rule.GetBestChild(queryIndex, referenceNode);
40  size_t numDescendants;
41 
42  // Check that referencenode is not a leaf node while calculating number of
43  // descendants of it's best child.
44  if (!referenceNode.IsLeaf())
45  numDescendants = referenceNode.Child(bestChild).NumDescendants();
46  else
47  numDescendants = referenceNode.NumPoints();
48 
49  // If number of descendants are more than rule.MinimumBaseCases() than we can
50  // go along with best child otherwise we need to traverse for each descendant
51  // to ensure that we calculate at least rule.MinimumBaseCases() number of base
52  // cases.
53  if (!referenceNode.IsLeaf())
54  {
55  if (numDescendants > rule.MinimumBaseCases())
56  {
57  // We are pruning all but one child.
58  numPrunes += referenceNode.NumChildren() - 1;
59  // Recurse the best child.
60  Traverse(queryIndex, referenceNode.Child(bestChild));
61  }
62  else
63  {
64  // Run the base case over first minBaseCases number of descendants.
65  for (size_t i = 0; i <= rule.MinimumBaseCases(); ++i)
66  rule.BaseCase(queryIndex, referenceNode.Descendant(i));
67  }
68  }
69 }
70 
71 } // namespace tree
72 } // namespace mlpack
73 
74 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void Traverse(const size_t queryIndex, TreeType &referenceNode)
Traverse the tree with the given point.
Definition: greedy_single_tree_traverser_impl.hpp:31
GreedySingleTreeTraverser(RuleType &rule)
Instantiate the greedy single tree traverser with the given rule set.
Definition: greedy_single_tree_traverser_impl.hpp:24