public class IntTree {

    private static class TreeNode {
        private int data;
        private TreeNode left; 
        private TreeNode right;
        
        public TreeNode( int d, TreeNode l, TreeNode r ) {
            this.data = d;
            this.left = l;
            this.right = r;
        }
        
        public int totalSum() {
            return this.data + this.left.totalSum() + this.right.totalSum();
        }
        
        public void printPreOrder() {
            System.out.print( this.data + " " );
            this.left.printPreOrder();
            this.right.printPreOrder();
        }

        public void printInOrder() {
            this.left.printInOrder();
            System.out.print( this.data + " " );
            this.right.printInOrder();
        }

        public void printPostOrder() {
            this.left.printPostOrder();
            this.right.printPostOrder();
            System.out.print( this.data + " " );
        }
        
    } // end TreeNode class
    
    private static class NullTreeNode extends TreeNode {
        public NullTreeNode() { super( 0, null, null ); }
        
        @Override
        public int totalSum() { return 0; }
        
        @Override
        public void printPreOrder() { ; }

        @Override
        public void printInOrder() { ; }

        @Override
        public void printPostOrder() { ; }
    } // end NullTreeNode class
    
    private TreeNode root;
    private static final NullTreeNode NULLNODE = new NullTreeNode();
    
    public IntTree() {
        this.root = NULLNODE;
    }
    
    public int totalSum() {
        return this.root.totalSum();
    }    
    
    public void printPreOrder() {
        this.root.printPreOrder();
    }    

    public void printInOrder() {
        this.root.printInOrder();
    }    
        
    public void printPostOrder() {
        this.root.printPostOrder();
    }    
    public void makeTestTree() {
        TreeNode leftLeftLeftChild = new TreeNode( 9, NULLNODE, NULLNODE );
        TreeNode leftLeftRightChild = new TreeNode( 10, NULLNODE, NULLNODE );
        TreeNode leftLeftChild = new TreeNode( 8, leftLeftLeftChild, leftLeftRightChild );
        TreeNode leftChild = new TreeNode( 5, leftLeftChild, NULLNODE );
        TreeNode rightLeftChild = new TreeNode( 6, NULLNODE, NULLNODE );
        TreeNode rightChild = new TreeNode( 7, rightLeftChild, NULLNODE );
        this.root = new TreeNode( 3, leftChild, rightChild );
    }

    public static void main( String[] args ) {
        IntTree tree = new IntTree();
        tree.makeTestTree();
        System.out.println( "total sum = " + tree.totalSum() ); 
        System.out.print( "Pre-order: ");
        tree.printPreOrder();       
        System.out.println();
        System.out.print( "In-order: ");
        tree.printInOrder();       
        System.out.println();
        System.out.print( "Post-order: ");
        tree.printPostOrder();       
        System.out.println();
    }
}