
/**
 * Write a description of class BTNode here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BTNode<E>
{
    private E data;
    private BTNode<E> left;
    private BTNode<E> right;
    
    public BTNode() {
        data = null;
        left = null;
        right = null;        
    }
    
    public BTNode(E initialData) {
        data = initialData;
        left = null;
        right = null;        
    }
    
    public BTNode(E initialData, BTNode<E> initialLeft, BTNode<E> initialRight) {
        data = initialData;
        left = initialLeft;
        right = initialRight;        
    }
    
    public E getData() {
        return data;
    }
    
    public void setData(E newData) {
        data = newData;
    }

    public BTNode<E> getLeft() {
        return left;
    }

    public BTNode<E> getRight() {
        return right;
    }

    public void setLeft(BTNode<E> newLeft) {
        left = newLeft;
    }
    
    public void setRight(BTNode<E> newRight) {
        right = newRight;
    } 
    
    
    public boolean isLeaf() {
        return ((left == null) && (right == null));         
    }
    
    
    public static <E> int countNodes(BTNode<E> node) {
        
        if (node == null) 
            return 0;
            
        int leftCount = countNodes(node.left);
        int rightCount = countNodes(node.right);
        
        return 1 + leftCount + rightCount;
    }
    
    public static <E> int treeHeight(BTNode<E> node) {
        if (node == null) 
            return -1;
            
        int leftHeight = treeHeight(node.left);
        int rightHeight = treeHeight(node.right);
        
        if (leftHeight > rightHeight) 
            return leftHeight + 1;
        else
            return rightHeight + 1;
            
            
        
    }
    
    public void preorderPrint() {
        System.out.println(data);
        
        if (left != null) 
            left.preorderPrint();
        if (right != null)
            right.preorderPrint();
        
    }
    
    public void inorderPrint() {
        
        
        if (left != null) 
            left.inorderPrint();
            
        System.out.println(data);            
            
        if (right != null)
            right.inorderPrint();
        
    }
    
    public void postorderPrint() {
        
        
        if (left != null) 
            left.postorderPrint();
        if (right != null)
            right.postorderPrint();
        System.out.println(data);
    }    

    
    
}
