public class Tree {
    Node root;
    boolean empty;

    Tree() {
        root = new Node(0);
        empty = true;
    }

    public void add(Node root, int data) {
        if (empty) {
            root.data = data;
            empty = false;
        } else {
            int d = root.data;
            if (data < d) {
                if (root.left == null) {
                    Node n = new Node(data);
                    root.left = n;
                } else {
                    add(root.left, data);
                }
            } else {
                if (root.right == null) {
                    Node n = new Node(data);
                    root.right = n;
                } else {
                    add(root.right, data);
                }
            }
        }
    }// End of add()

    public void preorder(Node root) {
        if (root != null) {
            System.out.print(root.data + " ");
            preorder(root.left);
            preorder(root.right);
        }
    }// End of preorder

    public void inorder(Node root) {
        if (root == null) return;
        if (root.left == null) {
            System.out.print(root.data + " ");
            inorder(root.right);
        } else {
            inorder(root.left);
            System.out.print(root.data + " ");
            inorder(root.right);
        }
    }// End of inorder()
    public void postorder(Node root) {
        if (root == null) return;
        if (root.right == null) {
            System.out.print(root.data + " ");
            postorder(root.left);
        } else {
            postorder(root.right);
            System.out.print(root.data + " ");
            postorder(root.right);
        }
    }// End of inorder()


    public static void main(String[] args) {
        Tree t = new Tree();
        System.out.print("Adding : ");
        for (int i = 0; i < 5; i++) {
            int random = (int) (Math.random() * 15 + 1);
            System.out.print(random + " ");
            t.add(t.root, random);
        }
        System.out.println();
        System.out.println("Preorder");
        t.preorder(t.root);
        System.out.println();
        System.out.println("Inorder");
        t.inorder(t.root);
    }// Ends of main()
}// End of class Tree()
