Libftpp
A modern C++ library
n_ary_tree.hpp
Go to the documentation of this file.
1 #ifndef N_ARY_TREE_HPP
2 #define N_ARY_TREE_HPP
3 
4 #include <functional>
5 
14 template <typename TType>
15 class Node
16 {
17 public:
18  TType data;
19  std::vector<std::unique_ptr<Node<TType>>> _children;
20  std::function<TType(std::vector<TType>)> parentFunct;
21  Node(TType value) : data(value) {}
22 
23  void addChild(const TType& child)
24  {
25  _children.push_back(std::make_unique<Node<TType>>(child));
26  }
27 
28  Node<TType>* setParentFunct(std::function<TType(std::vector<TType>)> func)
29  {
30  parentFunct = func;
31  return this;
32  }
33 };
34 
89 template <typename TType>
90 class NAryTree
91 {
92 private:
93  std::unique_ptr<Node<TType>> _root;
94 
95 public:
96  NAryTree() : _root(nullptr) {}
97  Node<TType>* setRoot(TType value)
98  {
99  if (_root == nullptr)
100  _root = std::make_unique<Node<TType>>(value);
101  return _root.get();
102  }
103 
105  {
106  return _root.get();
107  }
108 
109  Node<TType>* addChild(Node<TType>* parent, const TType& value)
110  {
111  if (!parent)
112  return nullptr;
113  parent->_children.push_back(std::make_unique<Node<TType>>(value));
114  return parent->_children.back().get();
115  }
116 
117  Node<TType>* addChildToRoot(const TType& value)
118  {
119  return addChild(_root.get(), value);
120  }
121 
126  void preorder(std::function<void(TType)>& funct)
127  {
128  if (_root != nullptr)
129  preorder(_root.get(), funct);
130  }
131 
132  void preorder(Node<TType>* node, std::function<void(TType)>& funct)
133  {
134  if (node != nullptr)
135  {
136  funct(node->data);
137  for (const auto& child : node->_children)
138  {
139  preorder(child.get(), funct);
140  }
141  }
142  }
143 
144  std::vector<TType> preorderValues()
145  {
146  std::vector<TType> result;
147  preorder(_root.get(), result);
148  return result;
149  }
150 
151  void preorder(Node<TType>* node, std::vector<TType>& result)
152  {
153  if (!node)
154  return;
155  result.push_back(node->data);
156  for (const auto& child : node->_children)
157  preorder(child.get(), result);
158  }
159 
164  void postorder(std::function<void(TType)>& funct)
165  {
166  if (_root != nullptr)
167  postorder(_root, funct);
168  }
169 
170  void postorder(Node<TType>* node, std::function<void(TType)>& funct)
171  {
172  if (node != nullptr)
173  {
174  for (const auto& child : node->_children)
175  {
176  postorder(child.get(), funct);
177  }
178  funct(node->data);
179  }
180  }
181 
182  std::vector<TType> postorderValues()
183  {
184  std::vector<TType> result;
185  postorder(_root.get(), result);
186  return result;
187  }
188 
189  void postorder(Node<TType>* node, std::vector<TType>& result)
190  {
191  if (!node)
192  return;
193 
194  for (const auto& child : node->_children)
195  postorder(child.get(), result);
196 
197  result.push_back(node->data);
198  }
199 
200  template <typename TResult>
202  {
203  if (!node)
204  return TResult();
205 
206  std::vector<TResult> childResults;
207 
208  if (node->parentFunct)
209  {
210  for (const auto& child : node->_children)
211  childResults.push_back(postorderCompute<TResult>(child.get()));
212  }
213  return node->parentFunct ? node->parentFunct(childResults) : TResult(node->data);
214  }
215 };
216 #endif
N ary Tree class.
Definition: n_ary_tree.hpp:91
void postorder(Node< TType > *node, std::vector< TType > &result)
Definition: n_ary_tree.hpp:189
void preorder(Node< TType > *node, std::function< void(TType)> &funct)
Definition: n_ary_tree.hpp:132
std::vector< TType > preorderValues()
Definition: n_ary_tree.hpp:144
Node< TType > * getRoot() const
Definition: n_ary_tree.hpp:104
void postorder(std::function< void(TType)> &funct)
Traverse the tree in postorder (children → parent).
Definition: n_ary_tree.hpp:164
Node< TType > * setRoot(TType value)
Definition: n_ary_tree.hpp:97
TResult postorderCompute(Node< TType > *node)
Definition: n_ary_tree.hpp:201
void postorder(Node< TType > *node, std::function< void(TType)> &funct)
Definition: n_ary_tree.hpp:170
Node< TType > * addChild(Node< TType > *parent, const TType &value)
Definition: n_ary_tree.hpp:109
std::vector< TType > postorderValues()
Definition: n_ary_tree.hpp:182
void preorder(Node< TType > *node, std::vector< TType > &result)
Definition: n_ary_tree.hpp:151
Node< TType > * addChildToRoot(const TType &value)
Definition: n_ary_tree.hpp:117
void preorder(std::function< void(TType)> &funct)
Traverse the tree in preorder (parent → children).
Definition: n_ary_tree.hpp:126
Node structure for an n-ary tree.
Definition: n_ary_tree.hpp:16
Node(TType value)
Definition: n_ary_tree.hpp:21
std::function< TType(std::vector< TType >)> parentFunct
Definition: n_ary_tree.hpp:20
void addChild(const TType &child)
Definition: n_ary_tree.hpp:23
Node< TType > * setParentFunct(std::function< TType(std::vector< TType >)> func)
Definition: n_ary_tree.hpp:28
std::vector< std::unique_ptr< Node< TType > > > _children
Definition: n_ary_tree.hpp:19
TType data
Definition: n_ary_tree.hpp:18