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; } }
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 this case, we could create an N-ary tree as follows
Urgenthomework helped me with finance homework problems and taught math portion of my course as well. Initially, I used a tutor that taught me math course I felt that as if I was not getting the help I needed. With the help of Urgenthomework, I got precisely where I was weak:
Read More
Follow Us