//simple routines that work on a binary tree.... 
public class Tree {

public static int countnodes(MyBinaryNode t){
 if(t==null)return 0;
    else return(1+countnodes(t.left)+countnodes(t.right));
}

public static int ipl(MyBinaryNode t, int level){// Internal Path Length
    if(t==null) return 0;
    else  return(ipl(t.left, level+1) + level+ ipl(t.right,level+1));
}

public static int treeHeight(MyBinaryNode t){//calculates tree height
  if(t==null) return -1; 
     else return(1 + max(treeHeight(t.left),treeHeight(t.right)));
}

public static int max(int a,int b){
if(a>b) return a; else return b;
}
public static void printTree( MyBinaryNode t , int indent)
 {
            if( t != null )
            {
                printTree( t.right, indent + 3 );
                for(int i=0;i<indent;i++)
                   System.out.print(" ");
                System.out.println( t.element );
                printTree( t.left , indent + 3 );
            }
 }
public static void inOrder(MyBinaryNode t){

           if (t!=null){
	       inOrder(t.left);
               System.out.print(t.element + " ");
               inOrder(t.right);
	   }
}
public static void preOrder(MyBinaryNode t){

           if (t!=null){
               System.out.print(t.element + " ");
	       preOrder(t.left);
               preOrder(t.right);

	   }
}
public static void postOrder(MyBinaryNode t){

           if (t!=null){
	       postOrder(t.left);
               postOrder(t.right);
               System.out.print(t.element + " ");
	   }  }

 public static void main(String[] args)  { 
// create a Binary tree by hand...
      MyBinaryNode root= new MyBinaryNode(new Integer(5),null,null);
      root.left= new MyBinaryNode(new Integer(20),null,null); 
      root.right= new MyBinaryNode(new Integer(6),null,null); 
      root.left.right= new MyBinaryNode(new Integer(12),null,null); 
      root.right.left= new MyBinaryNode(new Integer(8),null,null); 
      root.left.left=new MyBinaryNode(new Integer(36),null,null); 
      printTree(root,0); 
      System.out.println("Number of nodes is " + countnodes(root));
      System.out.println("Tree height is " + (treeHeight(root)));
      System.out.println("Internal Path length is " + ipl(root,0));
      System.out.println("inorder traversal");
      inOrder(root);
      System.out.println();
      System.out.println("preorder traversal");
      preOrder(root);
      System.out.println();
      System.out.println("postorder traversal");
      postOrder(root);
      System.out.println();
  }
}
 
  class MyBinaryNode  {
      MyBinaryNode( Object theElement ,MyBinaryNode lt, MyBinaryNode rt ){
           element  = theElement;
            left     = lt;
            right    = rt;
        }
     Object element;            // The data in the node
     MyBinaryNode left;         // Left child
     MyBinaryNode right;        // Right child
   }
--------------------------------------------------------
$java Tree     
   6
      8
5
      12
   20
      36
Number of nodes is 6
Tree height is 2
Internal Path length is 8
inorder traversal
36 20 12 5 8 6 
preorder traversal
5 20 36 12 6 8 
postorder traversal
36 12 20 8 6 5 

