/**
 * A generic Binary Tree class.
 * @author Dave Reed
 * @version 10/19/11
 */
public class BinaryTree<E> {
    protected TreeNode root;

    /**
     * Constructs an empty Binary Tree.
     */
    public BinaryTree() {
        this.root = null;
    }

    
    /**
     * Adds a value to the Binary Tree (attempting to maintain balance).
     * @param value the value to be added
     */
    public void add(E value) {
        this.root = this.add(this.root, value);
    }
    private TreeNode<E> add(TreeNode<E> current, E value) {
        if (current == null) {
            current = new TreeNode<E>(value, null, null);
        }
        else if (this.size(current.getLeft()) <= this.size(current.getRight())) {
            current.setLeft(this.add(current.getLeft(), value));
        }
        else {
            current.setRight(this.add(current.getRight(), value));
        }
        return current;
    }

    
    /**
     * Determines whether the Binary Tree contains a particular value.
     * @param value the value to be searched for
     * @return true if value is in the Binary Tree; else false
     */
    public boolean contains(E value) {
        return this.contains(this.root, value);
    }
    private  boolean contains(TreeNode<E> current, E value) {
        if (current == null) {
            return false;
        }
        else {
            return value.equals(current.getData()) ||
                   this.contains(current.getLeft(), value) ||
                   this.contains(current.getRight(), value);
        }
    }


    /**
     * Removes the specified value from the Binary Tree.
     * @param value the value to be removed
     * @return true if found and removed; else false
     */
    public boolean remove(E value) {
        if (!this.contains(value)) {
            return false;
        }
        else {
            this.root = this.remove(this.root, value);
            return true;
        }
    }
    private TreeNode<E> remove(TreeNode<E> current, E value) {
        if (value.equals(current.getData())) {
            if (current.getLeft() == null) {
                current = current.getRight();
            }
            else {
                TreeNode<E> righty = current.getLeft();
                while (righty.getRight() != null) {
                    righty = righty.getRight();
                }
                current.setData(righty.getData());
                current.setLeft(this.remove(current.getLeft(), current.getData()));
            }
        }
        else if (this.contains(current.getLeft(), value)) {
            current.setLeft(this.remove(current.getLeft(), value));
        }
        else {
            current.setRight(this.remove(current.getRight(), value));
        }
        return current;
    }

    
    /**
     * Determines the size of the Binary Tree.
     * @return the number of values stored (i.e., the number of nodes)
     */
    public int size() {
        return this.size(this.root);
    }
    private int size(TreeNode<E> current) {
        if (current == null) {
            return 0;
        }
        else {
            return 1 + this.size(current.getLeft()) +
                       this.size(current.getRight());
        }
    }
  

    /**
     * Converts the Binary Tree into a string using an inorder traversal.
     * @return the values appended into a single string
     */
    public String toString() {
        if (this.root == null) {
            return "[]";
        }

        String recStr = this.toString(this.root);
        return "[" + recStr.substring(0,recStr.length()-1) + "]";
    }
    private String toString(TreeNode<E> current) {
        if (current ==  null) {
            return "";
        }

        return this.toString(current.getLeft()) +
               current.getData().toString() + "," +
               this.toString(current.getRight());
    }
    
    
    /**
     * Clears the Binary Tree.
     */
    public void clear() {
        this.root = null;
    }
    
    //////////////////////////////////////////
    
    protected class TreeNode<E> {
        private E data;
        private TreeNode<E> left;
        private TreeNode<E> right;

        public TreeNode(E d, TreeNode<E> l, TreeNode<E> r) {
            this.data = d;
            this.left = l;
            this.right = r;
        }

        public E getData() {
            return this.data;
        }

        public TreeNode<E> getLeft() {
            return this.left;
        }

        public TreeNode<E> getRight() {
            return this.right;
        }

        public void setData(E newData) {
            this.data = newData;
        }

        public void setLeft(TreeNode<E> newLeft) {
            this.left = newLeft;
        }

        public void setRight(TreeNode<E> newRight) {
            this.right = newRight;
        }
    }
    
    //////////////////////////////////////////

    public static void main(String[] args) {
        BinaryTree<Integer> tree = new BinaryTree<Integer>();
        tree.add(1);
        tree.add(2);
        tree.add(3);
        tree.add(4);
        tree.add(5);
        tree.add(6);
        tree.add(7);
        tree.add(8);

        System.out.println(tree);
        System.out.println("size = " + tree.size());
        System.out.println(tree.contains(2) + " " + tree.contains(5)
                                            + " " + tree.contains(9));
        
        tree.remove(2);
        System.out.println(tree);
        System.out.println("size = " + tree.size());
        System.out.println(tree.contains(2) + " " + tree.contains(5)
                                            + " " + tree.contains(9));
        
        tree.clear();
        System.out.println(tree);
        System.out.println("size = " + tree.size());
        System.out.println(tree.contains(2) + " " + tree.contains(5)
                                            + " " + tree.contains(9));        
    }
}
