enumerate_tree.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_TREE_ENUMERATE_TREE_HPP
14 #define MLPACK_CORE_TREE_ENUMERATE_TREE_HPP
15 
16 namespace mlpack {
17 namespace tree {
18 namespace enumerate {
19 
20 // Actual implementation of the enumeration. The problem is the unified
21 // detection if we're on the root, because Enter and Leave expect the
22 // parent being passed.
23 template <class TreeType, class Walker>
24 void EnumerateTreeImpl(TreeType* tree, Walker& walker, bool root)
25 {
26  if (root)
27  walker.Enter(tree, (const TreeType*)nullptr);
28 
29  const size_t numChildren = tree->NumChildren();
30  for (size_t i = 0; i < numChildren; ++i)
31  {
32  TreeType* child = tree->ChildPtr(i);
33  walker.Enter(child, tree);
34  EnumerateTreeImpl(child, walker, false);
35  walker.Leave(child, tree);
36  }
37 
38  if (root)
39  walker.Leave(tree, (const TreeType*)nullptr);
40 }
41 
42 } // namespace enumerate
43 
44 
55 template <class TreeType, class Walker>
56 inline void EnumerateTree(TreeType* tree, Walker& walker)
57 {
58  enumerate::EnumerateTreeImpl(tree, walker, true);
59 }
60 
61 } // namespace tree
62 } // namespace mlpack
63 
64 
65 #endif // MLPACK_CORE_TREE_ENUMERATE_TREE_HPP
Linear algebra utility functions, generally performed on matrices or vectors.
void EnumerateTreeImpl(TreeType *tree, Walker &walker, bool root)
void EnumerateTree(TreeType *tree, Walker &walker)
Traverses all nodes of the tree, including the inner ones.