Rev 28 | Blame | Last modification | View Log | RSS feed
// Written by Ira Snyder// Due Date: 11-15-2004// Project #3import java.io.*;import java.util.*;class BinaryTree {private Object root;private BinaryTree left, right;//constructors ----------------------------------------------------------// constructor to create a singleton tree// Precondition: root is a non-null Object// Postcondition: returns a BinaryTree with the Object given as the root// data, and null left and right subtreespublic BinaryTree( Object root ) {this.root = root;this.left = null;this.right = null;}// constructor to create a BinaryTree with the given Object as the root// data, and the given BinaryTrees as the left and right subtrees// Precondition: root is a non-null Object (right and left CAN be null// Postcondition: returns a BinaryTree with the given root data and// the given left and right subtreespublic BinaryTree( Object root, BinaryTree left, BinaryTree right ) {this.root = root;this.left = left;this.right = right;}// copy constructor, creates a tree which has the same structure and// whose nodes reference the same objects as the _that_ treepublic BinaryTree( BinaryTree that ) {System.out.println("Don't use me yet");}//getter methods --------------------------------------------------------// method which returns the root data// Precondition: none// Postcondition: return the root datapublic Object getRoot() { return root; }// method which will return a reference to the left subtree// Precondition: none// Postcondition: returns a reference to the left subtreepublic BinaryTree getLeft() { return left; }// method which will return a reference to the right subtree// Precondition: none// Postcondition: returns a reference to the right subtreepublic BinaryTree getRight() { return right; }//setter methods --------------------------------------------------------// method which updates the root data// Precondition: root is non-null// Postcondition: sets this.root to the new data, returns the old datapublic Object setRoot( Object root ) {Object temp = this.root;this.root = root;return temp;}// method which updates the left subtree// Precondition: none ( left CAN be null )// Postcondition: sets this.left to the new subtree,// returns a reference to the old left subtreepublic BinaryTree setLeft( BinaryTree left ) {BinaryTree temp = this.left;this.left = left;return temp;}// method which update the right subtree// Precondition: none ( right CAN be null )// Postcondition: sets this.right to the new subtree,// returns a reference to the old right subtreepublic BinaryTree setRight( BinaryTree right ) {BinaryTree temp = this.right;this.right = right;return temp;}//toString method -------------------------------------------------------// returns a String representation of the BinaryTreepublic String toString() {String sLeft = "";String sRight = "";String answer = new String();//check left treeif( !(left == null) ) { sLeft = left.toString(); }//check right treeif( !(right == null) ) { sRight = right.toString(); }//answer = "(" + sLeft + "," + root.toString() + "," + sRight + ")";answer = "(";if( !sLeft.equals("") ) { answer += sLeft + ","; }answer += root.toString();if( !sRight.equals("") ) { answer += "," + sRight; }answer += ")";return answer;}/*//misc methods ----------------------------------------------------------public boolean isLeaf() { }public int size() { }public int height() { }public boolean contains( Object object ) { }public int numLeaves() { }public int count( Object x ) { }public boolean isFull() { }public boolean isBalanced() { }public int pathLength() { }public BinaryTree reverse() { }public int level( Object x ) { }public boolean isDisjointFrom( BinaryTree that ) { }public boolean isValid() { }public boolean equals( Object object ) { }//printing methods ------------------------------------------------------public static void preOrderPrint( BinaryTree tree ) { }public static void postOrderPrint( BinaryTree tree ) { }public static void levelOrderPrint( BinaryTree tree ) { }public static void inOrderPrint( BinaryTree tree ) { }*/}/*BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));PrintStream ps = new PrintStream(new FileOutputStream(new File(filename)));*/