import Supporting.*;

public class BinaryTree
{
    public BinaryTree( )
    {
        root = null;
    }

    public BinaryTree( Object rootItem )
    {
        root = new BinaryNode( rootItem );
    }

    public void printPreOrder( )
    {
        if( root != null )
            root.printPreOrder( );
    }

    public void printInOrder( )
    {
        if( root != null )
           root.printInOrder( );
    }

    public void printPostOrder( )
    {
        if( root != null )
           root.printPostOrder( );
    }

    public void makeEmpty( )
    {
        root = null;
    }

    public boolean isEmpty( )
    {
        return root == null;
    }

    public void merge( Object rootItem, BinaryTree t1, BinaryTree t2 )
    {
        if( t1.root == t2.root && t1.root != null )
        {
            System.err.println( "leftTree==rightTree; merge aborted" );
            return;
        }

            // Allocate new node
        root = new BinaryNode( rootItem, t1.root, t2.root );

            // Ensure that every node is in one tree
        if( this != t1 )
            t1.root = null;
        if( this != t2 )
            t2.root = null;
    }

    public int size( )
    {
        return BinaryNode.size( root );
    }

    public int height( )
    {
        return BinaryNode.height( root );
    }

    private BinaryNode root;

    static public void main( String [ ] args )
    {
        BinaryTree t1 = new BinaryTree( new MyInteger( 1 ) );
        BinaryTree t3 = new BinaryTree( new MyInteger( 3 ) );
        BinaryTree t5 = new BinaryTree( new MyInteger( 5 ) );
        BinaryTree t7 = new BinaryTree( new MyInteger( 7 ) );
        BinaryTree t2 = new BinaryTree( );
        BinaryTree t4 = new BinaryTree( );
        BinaryTree t6 = new BinaryTree( );

        t2.merge( new MyInteger( 2 ), t1, t3 );
        t6.merge( new MyInteger( 6 ), t5, t7 );
        t4.merge( new MyInteger( 4 ), t2, t6 );

        System.out.println( "t4 should be perfect 1-7; t2 empty" );
        System.out.println( "----------------" );
        System.out.println( "t4" );
        t4.printInOrder( );
        System.out.println( "----------------" );
        System.out.println( "t2" );
        t2.printInOrder( );
        System.out.println( "----------------" );
        System.out.println( "t4 size: " + t4.size( ) );
        System.out.println( "t4 height: " + t4.height( ) );
    }
}