Source of BinaryTree.java


  1: package TreePackage;
  2: import java.util.Iterator;
  3: import java.util.NoSuchElementException;
  4: import StackAndQueuePackage.*; // Needed by tree iterators

  6: /**
  7:    A class that implements the ADT binary tree.
  8:    
  9:    @author Frank M. Carrano
 10:    @author Timothy M. Henry
 11:    @version 5.0
 12: */
 13: public class BinaryTree<T> implements BinaryTreeInterface<T>
 14: {
 15:    private BinaryNode<T> root;

 17:    public BinaryTree()
 18:    {
 19:       root = null;
 20:    } // end default constructor

 22:    public BinaryTree(T rootData)
 23:    {
 24:       root = new BinaryNode<>(rootData);
 25:    } // end constructor

 27:    public BinaryTree(T rootData, BinaryTree<T> leftTree, BinaryTree<T> rightTree)
 28:    {
 29:       initializeTree(rootData, leftTree, rightTree);
 30:    } // end constructor

 32:     public void setTree(T rootData, BinaryTreeInterface<T> leftTree,
 33:                                    BinaryTreeInterface<T> rightTree)
 34:    {
 35:       initializeTree(rootData, (BinaryTree<T>)leftTree,
 36:                                (BinaryTree<T>)rightTree);
 37:    } // end setTree

 39:         private void initializeTree(T rootData, BinaryTree<T> leftTree,
 40:                                                 BinaryTree<T> rightTree)
 41:         {
 42:       // < FIRST DRAFT - See Segments 25.4 - 25.7 for improvements. >
 43:       root = new BinaryNode<T>(rootData);
 44:       
 45:       if (leftTree != null)
 46:          root.setLeftChild(leftTree.root);
 47:       
 48:       if (rightTree != null)
 49:          root.setRightChild(rightTree.root);
 50:         } // end initializeTree

 52: /* Implementations of setRootData, getRootData, getHeight, getNumberOfNodes,
 53:    isEmpty, clear, and the methods specified in TreeIteratorInterface are here.
 54:    . . . */

 56: } // end BinaryTree