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 
54 template <class TreeType, class Walker>
55 inline void EnumerateTree(TreeType* tree, Walker& walker)
56 {
57  enumerate::EnumerateTreeImpl(tree, walker, true);
58 }
59 
60 } // namespace tree
61 } // namespace mlpack
62 
63 
64 #endif // MLPACK_CORE_TREE_ENUMERATE_TREE_HPP
.hpp
Definition: add_to_po.hpp:21
void EnumerateTreeImpl(TreeType *tree, Walker &walker, bool root)
void EnumerateTree(TreeType *tree, Walker &walker)
Traverses all nodes of the tree, including the inner ones.