Source of BinaryTree.java


  1: package TreePackage;
  2: import java.util.Iterator;
  3: import java.util.EmptyStackException;
  4: import java.util.NoSuchElementException;
  5: import StackAndQueuePackage.*;
  6: 
  7: /**
  8:    A class that implements the ADT binary tree.
  9:    
 10:    @author Frank M. Carrano
 11:    @author Timothy M. Henry
 12:    @version 4.0
 13: */
 14: public class BinaryTree<T> implements BinaryTreeInterface<T>
 15: {
 16:    private BinaryNode<T> root;
 17: 
 18:    public BinaryTree()
 19:    {
 20:       root = null;
 21:    } // end default constructor
 22: 
 23:    public BinaryTree(T rootData)
 24:    {
 25:       root = new BinaryNode<>(rootData);
 26:    } // end constructor
 27: 
 28:    public BinaryTree(T rootData, BinaryTree<T> leftTree, 
 29:                                  BinaryTree<T> rightTree)
 30:    {
 31:       privateSetTree(rootData, leftTree, rightTree);
 32:    } // end constructor
 33: 
 34:    public void setTree(T rootData)
 35:    {
 36:       root = new BinaryNode<>(rootData);
 37:    } // end setTree
 38: 
 39:    public void setTree(T rootData, BinaryTreeInterface<T> leftTree,
 40:                                    BinaryTreeInterface<T> rightTree)
 41:    {
 42:       privateSetTree(rootData, (BinaryTree<T>)leftTree, 
 43:                                (BinaryTree<T>)rightTree);
 44:    } // end setTree
 45: 
 46:         private void privateSetTree(T rootData, BinaryTree<T> leftTree, 
 47:                                                 BinaryTree<T> rightTree)
 48:         {
 49:       // < FIRST DRAFT - See Segments 24.4 - 24.7 for improvements. >
 50:       root = new BinaryNode<T>(rootData);
 51:       
 52:       if (leftTree != null)
 53:          root.setLeftChild(leftTree.root);
 54:       
 55:       if (rightTree != null)
 56:          root.setRightChild(rightTree.root);
 57:         } // end privateSetTree
 58: 
 59: /*  < Implementations of getRootData, getHeight, getNumberOfNodes, isEmpty, clear,
 60:       and the methods specified in TreeIteratorInterface are here. >
 61:    . . . */
 62: 
 63: } // end BinaryTree