class BinTree {
  // The basic class used for many of the data structures in this program.
  // The nodes contain an object.  This can be cast to more specific types
  // as the situation demands.  There is nothing in this that forces this
  // to be a binary search tree or an expression tree.  So please write
  // this generically.
    
  // Instance Variables

  private Object node;
  private BinTree left, right;

  // Constructors

  public BinTree(Object root){
    // Makes a tree with one node and empty subtrees 
    this.left = null;
    this.right = null;
    this.node = root;
  }
  
  public BinTree(Object root, BinTree l, BinTree r){
    //Makes a tree with a new root and given subtrees
    this.left = l;
    this.right = r;
    this.node = root;
  }

  // Instance Methods

  public Object root(){
      return this.node;
  }

  public BinTree left(){
      return this.left;
  }

  public BinTree right(){
      return this.right;
  }

  public void SetRoot(Object x){
      this.node = x;
  }

  public void SetLeft(BinTree t){
      this.left = t;
  }

  public void SetRight(BinTree t){
      this.right = t;
  }

  public boolean IsLeaf(){
      return (this.left==null && this.right==null);
  }
  
  public int length() {
      if (this.left==null && this.right==null) return 1;
      else return 1+java.lang.Math.max(this.left.length(), this.right.length());
  }

  //The next three methods are required to visit the nodes in the indicated
  //orders and print the object at the nodes.  It is somewhat silly to make
  //you do all three since if you do one you can do all three.  However I
  //think that even silly repetitive code will help some of this material
  //become automatic. 
  public void preorder(){
  //Returns in root, left, right order--copied of out notes
      System.out.print(" " + this.node);
      if (this.left != null) this.left.preorder();
      if (this.right != null) this.right.preorder();
  }

  public void inorder(){
  //Returns in left, root, right order--copied of out notes
      if (this.left != null) this.left.inorder();
      System.out.print(" " + this.node);
      if (this.right != null) this.right.inorder();
  }

  public void postorder(){
  //Returns in left, right, root order--copied of out notes
      if (this.left != null) this.left.postorder();
      if (this.right != null) this.right.postorder();
      System.out.print(" " + this.node);
  }

}//end BinTree
