Libftpp
A modern C++ library
NAryTree< TType > Class Template Reference

N ary Tree class. More...

#include <n_ary_tree.hpp>

Public Member Functions

 NAryTree ()
 
Node< TType > * setRoot (TType value)
 
Node< TType > * getRoot () const
 
Node< TType > * addChild (Node< TType > *parent, const TType &value)
 
Node< TType > * addChildToRoot (const TType &value)
 
void preorder (std::function< void(TType)> &funct)
 Traverse the tree in preorder (parent → children). More...
 
void preorder (Node< TType > *node, std::function< void(TType)> &funct)
 
std::vector< TType > preorderValues ()
 
void preorder (Node< TType > *node, std::vector< TType > &result)
 
void postorder (std::function< void(TType)> &funct)
 Traverse the tree in postorder (children → parent). More...
 
void postorder (Node< TType > *node, std::function< void(TType)> &funct)
 
std::vector< TType > postorderValues ()
 
void postorder (Node< TType > *node, std::vector< TType > &result)
 
template<typename TResult >
TResult postorderCompute (Node< TType > *node)
 

Detailed Description

template<typename TType>
class NAryTree< TType >

N ary Tree class.

Note
Can execute preorder and postorder traversals to execute all children of the tree.
Simple Usage:
auto root = tree.setRoot(1);
auto child1 = tree.addChild(root, 2);
auto child2 = tree.addChild(root, 3);
tree.addChild(child1, 4);
tree.addChild(child1, 5);
tree.addChild(child2, 6);
Preorder Traversal:
std::vector<int> values;
tree.preorder(root, values);
std::vector<int> expected = {1, 2, 4, 5, 3, 6};
Postorder Traversal:
std::vector<int> postorderValues = tree.postorderValues();
std::vector<int> postorderExpected = {4, 5, 2, 6, 3, 1};
Postorder Compute with custom function:
auto root = tree.setRoot(1);
auto c1 = tree.addChild(root, 2);
auto c2 = tree.addChild(root, 3);
tree.addChild(c1, 4);
tree.addChild(c1, 5);
tree.addChild(c2, 6);
// Définir une fonction qui additionne les résultats des enfants + la valeur du noeud
auto sumFunc = [](const std::vector<int>& children)
{ return std::accumulate(children.begin(), children.end(), 0); };
// On définit sumFunc pour chaque noeud interne
root->setParentFunct(sumFunc);
c1->setParentFunct(sumFunc);
c2->setParentFunct(sumFunc);
int result = tree.postorderCompute<int>(root);
// Calcul attendu :
// c1: 4+5=9
// c2: 6
// root: 9+6=15
EXPECT_EQ(result, 15);
N ary Tree class.
Definition: n_ary_tree.hpp:91
Node< TType > * setRoot(TType value)
Definition: n_ary_tree.hpp:97
TResult postorderCompute(Node< TType > *node)
Definition: n_ary_tree.hpp:201
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(std::function< void(TType)> &funct)
Traverse the tree in preorder (parent → children).
Definition: n_ary_tree.hpp:126

Definition at line 90 of file n_ary_tree.hpp.

Constructor & Destructor Documentation

◆ NAryTree()

template<typename TType >
NAryTree< TType >::NAryTree ( )
inline

Definition at line 96 of file n_ary_tree.hpp.

Member Function Documentation

◆ addChild()

template<typename TType >
Node<TType>* NAryTree< TType >::addChild ( Node< TType > *  parent,
const TType &  value 
)
inline

Definition at line 109 of file n_ary_tree.hpp.

References Node< TType >::_children.

Referenced by NAryTree< TType >::addChildToRoot().

◆ addChildToRoot()

template<typename TType >
Node<TType>* NAryTree< TType >::addChildToRoot ( const TType &  value)
inline

Definition at line 117 of file n_ary_tree.hpp.

References NAryTree< TType >::addChild().

◆ getRoot()

template<typename TType >
Node<TType>* NAryTree< TType >::getRoot ( ) const
inline

Definition at line 104 of file n_ary_tree.hpp.

◆ postorder() [1/3]

template<typename TType >
void NAryTree< TType >::postorder ( Node< TType > *  node,
std::function< void(TType)> &  funct 
)
inline

◆ postorder() [2/3]

template<typename TType >
void NAryTree< TType >::postorder ( Node< TType > *  node,
std::vector< TType > &  result 
)
inline

◆ postorder() [3/3]

template<typename TType >
void NAryTree< TType >::postorder ( std::function< void(TType)> &  funct)
inline

Traverse the tree in postorder (children → parent).

Parameters
functFunction applied to each node's value

Definition at line 164 of file n_ary_tree.hpp.

Referenced by NAryTree< TType >::postorder(), and NAryTree< TType >::postorderValues().

◆ postorderCompute()

template<typename TType >
template<typename TResult >
TResult NAryTree< TType >::postorderCompute ( Node< TType > *  node)
inline

◆ postorderValues()

template<typename TType >
std::vector<TType> NAryTree< TType >::postorderValues ( )
inline

Definition at line 182 of file n_ary_tree.hpp.

References NAryTree< TType >::postorder().

◆ preorder() [1/3]

template<typename TType >
void NAryTree< TType >::preorder ( Node< TType > *  node,
std::function< void(TType)> &  funct 
)
inline

◆ preorder() [2/3]

template<typename TType >
void NAryTree< TType >::preorder ( Node< TType > *  node,
std::vector< TType > &  result 
)
inline

◆ preorder() [3/3]

template<typename TType >
void NAryTree< TType >::preorder ( std::function< void(TType)> &  funct)
inline

Traverse the tree in preorder (parent → children).

Parameters
functFunction applied to each node's value

Definition at line 126 of file n_ary_tree.hpp.

Referenced by NAryTree< TType >::preorder(), and NAryTree< TType >::preorderValues().

◆ preorderValues()

template<typename TType >
std::vector<TType> NAryTree< TType >::preorderValues ( )
inline

Definition at line 144 of file n_ary_tree.hpp.

References NAryTree< TType >::preorder().

◆ setRoot()

template<typename TType >
Node<TType>* NAryTree< TType >::setRoot ( TType  value)
inline

Definition at line 97 of file n_ary_tree.hpp.