mlpack
r_plus_tree_descent_heuristic_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_TREE_RECTANGLE_TREE_R_PLUS_TREE_DESCENT_HEURISTIC_IMPL_HPP
14 #define MLPACK_CORE_TREE_RECTANGLE_TREE_R_PLUS_TREE_DESCENT_HEURISTIC_IMPL_HPP
15 
17 #include "../hrectbound.hpp"
18 
19 namespace mlpack {
20 namespace tree {
21 
22 template<typename TreeType>
24  const size_t point)
25 {
26  typedef typename TreeType::ElemType ElemType;
27  size_t bestIndex = 0;
28  bool success = true;
29 
30  // Try to find a node that contains the point.
31  for (bestIndex = 0; bestIndex < node->NumChildren(); bestIndex++)
32  {
33  if (node->Child(bestIndex).Bound().Contains(
34  node->Dataset().col(point)))
35  return bestIndex;
36  }
37 
38  // No one node contains the point. Try to enlarge a node in such a way, that
39  // the resulting node do not overlap other nodes.
40  for (bestIndex = 0; bestIndex < node->NumChildren(); bestIndex++)
41  {
43  node->Child(bestIndex).Bound();
44  bound |= node->Dataset().col(point);
45 
46  success = true;
47 
48  for (size_t j = 0; j < node->NumChildren(); ++j)
49  {
50  if (j == bestIndex)
51  continue;
52  success = false;
53  // Two nodes overlap if and only if there are no dimension in which
54  // they do not overlap each other.
55  for (size_t k = 0; k < node->Bound().Dim(); ++k)
56  {
57  if (bound[k].Lo() >= node->Child(j).Bound()[k].Hi() ||
58  node->Child(j).Bound()[k].Lo() >= bound[k].Hi())
59  {
60  // We found the dimension in which these nodes do not overlap
61  // each other.
62  success = true;
63  break;
64  }
65  }
66  if (!success) // These two nodes overlap each other.
67  break;
68  }
69  if (success) // We found two nodes that do no overlap each other.
70  break;
71  }
72 
73  if (!success) // We could not find two nodes that do no overlap each other.
74  {
75  size_t depth = node->TreeDepth();
76 
77  // Create a new node into which we will insert the point.
78  TreeType* tree = node;
79  while (depth > 1)
80  {
81  TreeType* child = new TreeType(tree);
82 
83  tree->children[tree->NumChildren()++] = child;
84  tree = child;
85  depth--;
86  }
87  return node->NumChildren() - 1;
88  }
89 
90  assert(bestIndex < node->NumChildren());
91 
92  return bestIndex;
93 }
94 
95 template<typename TreeType>
97  const TreeType* /* node */, const TreeType* /*insertedNode */)
98 {
99  // Should never be used.
100  assert(false);
101 
102  return 0;
103 }
104 
105 } // namespace tree
106 } // namespace mlpack
107 
108 #endif // MLPACK_CORE_TREE_RECTANGLE_TREE_R_PLUS_TREE_DESCENT_HEURISTIC_IMPL_HPP
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
static size_t ChooseDescentNode(TreeType *node, const size_t point)
Evaluate the node using a heuristic.
Definition: r_plus_tree_descent_heuristic_impl.hpp:23