Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Constructors

Program gif defines constructors for the BinaryTree class. The first one takes three arguments and assigns each of them to the corresponding field. Clearly the running time of this constructor is O(1).

   program16399
Program: BinaryTree constructors.

The second constructor, the no-arg constructor, creates an empty binary tree. It simply sets all three fields to null.

The third constructor takes as its argument any Object. The purpose of this constructor is to create a binary tree with the specified object as its root. Since every binary tree has exactly two subtrees, this constructor creates two empty subtrees and assigns them to the left and right fields.


next up previous contents index

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