+1-617-874-1011 (US)

  +44-117-230-1145 (UK)



General N-ary Trees Implementation

template < typename object>
class Nary_tree{
private:
object element;
int N;
Nary_tree **children;
public:
Nary_tree(object const & = object(), int = 2);
//...
}
template < typename object>
Nary_tree< object>::Nary_tree(object const &e, int n):
element (e),
N( std::max( 2, n )),
children ( new *Nary_tree[N] ) {
for (int i = 0; i < N; ++i ) {
children[i] = 0;
}
}
N-ary Trees Homework Help

However, this requires the allocation of dynamic memory in all cases. An alternate design is to use templates:

template < typename object, int N>
class Nary_tree{
private:
object element;
Nary_tree * children [std::max(N,2)|];//an array of N Children
public:
Nary_tree (object const &e );
element ( e) {
for (int i = 0; i < N; ++i) {
children[i] = 0;
}
}

N-ary Trees Implementation Assignment Help

n this case, we could create an N-ary tree as follows

N-ary Trees Online Tutoring


N-ary Trees Implementation Assignment Help