mlpack
single_tree_traverser_impl.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_CORE_TREE_BINARY_SPACE_TREE_SINGLE_TREE_TRAVERSER_IMPL_HPP
15 #define MLPACK_CORE_TREE_BINARY_SPACE_TREE_SINGLE_TREE_TRAVERSER_IMPL_HPP
16 
17 // In case it hasn't been included yet.
19 
20 #include <stack>
21 
22 namespace mlpack {
23 namespace tree {
24 
25 template<typename MetricType,
26  typename StatisticType,
27  typename MatType,
28  template<typename BoundMetricType, typename...> class BoundType,
29  template<typename SplitBoundType, typename SplitMatType>
30  class SplitType>
31 template<typename RuleType>
34  rule(rule),
35  numPrunes(0)
36 { /* Nothing to do. */ }
37 
38 template<typename MetricType,
39  typename StatisticType,
40  typename MatType,
41  template<typename BoundMetricType, typename...> class BoundType,
42  template<typename SplitBoundType, typename SplitMatType>
43  class SplitType>
44 template<typename RuleType>
47  const size_t queryIndex,
49  referenceNode)
50 {
51  // If we are a leaf, run the base case as necessary.
52  if (referenceNode.IsLeaf())
53  {
54  const size_t refEnd = referenceNode.Begin() + referenceNode.Count();
55  for (size_t i = referenceNode.Begin(); i < refEnd; ++i)
56  rule.BaseCase(queryIndex, i);
57  }
58  else
59  {
60  // If it's the root node, just score it.
61  if (referenceNode.Parent() == NULL)
62  {
63  const double rootScore = rule.Score(queryIndex, referenceNode);
64  // If root score is DBL_MAX, don't recurse into that node.
65  if (rootScore == DBL_MAX)
66  {
67  ++numPrunes;
68  return;
69  }
70  }
71 
72  // If either score is DBL_MAX, we do not recurse into that node.
73  double leftScore = rule.Score(queryIndex, *referenceNode.Left());
74  double rightScore = rule.Score(queryIndex, *referenceNode.Right());
75 
76  if (leftScore < rightScore)
77  {
78  // Recurse to the left.
79  Traverse(queryIndex, *referenceNode.Left());
80 
81  // Is it still valid to recurse to the right?
82  rightScore = rule.Rescore(queryIndex, *referenceNode.Right(), rightScore);
83 
84  if (rightScore != DBL_MAX)
85  Traverse(queryIndex, *referenceNode.Right()); // Recurse to the right.
86  else
87  ++numPrunes;
88  }
89  else if (rightScore < leftScore)
90  {
91  // Recurse to the right.
92  Traverse(queryIndex, *referenceNode.Right());
93 
94  // Is it still valid to recurse to the left?
95  leftScore = rule.Rescore(queryIndex, *referenceNode.Left(), leftScore);
96 
97  if (leftScore != DBL_MAX)
98  Traverse(queryIndex, *referenceNode.Left()); // Recurse to the left.
99  else
100  ++numPrunes;
101  }
102  else // leftScore is equal to rightScore.
103  {
104  if (leftScore == DBL_MAX)
105  {
106  numPrunes += 2; // Pruned both left and right.
107  }
108  else
109  {
110  // Choose the left first.
111  Traverse(queryIndex, *referenceNode.Left());
112 
113  // Is it still valid to recurse to the right?
114  rightScore = rule.Rescore(queryIndex, *referenceNode.Right(),
115  rightScore);
116 
117  if (rightScore != DBL_MAX)
118  Traverse(queryIndex, *referenceNode.Right());
119  else
120  ++numPrunes;
121  }
122  }
123  }
124 }
125 
126 } // namespace tree
127 } // namespace mlpack
128 
129 #endif
BinarySpaceTree * Parent() const
Gets the parent of this node.
Definition: binary_space_tree.hpp:342
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
size_t Count() const
Return the number of points in this subset.
Definition: binary_space_tree.hpp:503
BinarySpaceTree * Right() const
Gets the right child of this node.
Definition: binary_space_tree.hpp:337
A binary space partitioning tree, such as a KD-tree or a ball tree.
Definition: binary_space_tree.hpp:54
BinarySpaceTree * Left() const
Gets the left child of this node.
Definition: binary_space_tree.hpp:332
bool IsLeaf() const
Return whether or not this node is a leaf (true if it has no children).
Definition: binary_space_tree_impl.hpp:594
size_t Begin() const
Return the index of the beginning point of this subset.
Definition: binary_space_tree.hpp:498
void Traverse(const size_t queryIndex, BinarySpaceTree &referenceNode)
Traverse the tree with the given point.
Definition: single_tree_traverser_impl.hpp:46
SingleTreeTraverser(RuleType &rule)
Instantiate the single tree traverser with the given rule set.
Definition: single_tree_traverser_impl.hpp:33