Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Constructors

The NaryTree class declares two constructors. Implementations for the two constructors are given in Program gif. The first constructor takes a single argument of type unsigned int which specifies the degree of the tree. This constructor creates an empty tree. It does so by setting the key pointer to zero, and by setting the length of the subtree array to zero. The running time of this constructor is O(1).

   program16924
Program: NaryTree Class Constructor Definitions

The second constructor takes two arguments. The first specifies the degree of the tree, and the second is a reference to an Object instance. This constructor creates a non-empty tree in which the specified object occupies the root node. According to Definition gif, every internal node in an N-ary tree must have exactly N subtrees. Therefore, this constructor creates and attaches N empty subtrees to the root node. The running time of this constructor is O(N), since N empty subtrees are created and constructed and the constructor for an empty N-ary tree takes O(1) time.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.