public class MyBinaryTree<AnyType>
{
	//step 11.
	private MyBinaryNode<AnyType> root;

	//step 12.
	static public void main( String [ ] args )
    {
		System.out.println( "Binary Tree:" );
		MyBinaryTree<Integer> t1 = new MyBinaryTree<Integer>(1);
		//step 4.
		MyBinaryTree<Integer> t3 = new MyBinaryTree<Integer>(3);
		MyBinaryTree<Integer> t5 = new MyBinaryTree<Integer>(5);
		MyBinaryTree<Integer> t7 = new MyBinaryTree<Integer>(7);
		MyBinaryTree<Integer> t2 = new MyBinaryTree<Integer>();
		MyBinaryTree<Integer> t4 = new MyBinaryTree<Integer>();
		MyBinaryTree<Integer> t6 = new MyBinaryTree<Integer>();
		//step 16
		t2.merge(2, t1, t3);
		t6.merge(6, t5, t7);
		t4.merge(4, t2, t6);
		System.out.println( "t4 should be the root, and perfect 1-7 inorder; t2 is now empty" );
		System.out.println( "----------------" );
		System.out.println( "t4" );
        t4.printInOrder( );
		//t2 should be null
		System.out.println( "----------------" );
        System.out.println( "t2" );
        t2.printInOrder( );

	}
	
	//step 13.
	public MyBinaryTree()
	{
		root = null;
	}
	
	//step 13.
	public MyBinaryTree (AnyType rootItem) 
	{
		root = new MyBinaryNode<AnyType> (rootItem, null, null);
	}
	
	//step 15.
	public void merge (AnyType rootItem, MyBinaryTree<AnyType> t1, MyBinaryTree<AnyType> t2)
	{
		if (t1.root == t2.root && t1.root != null)
		{
			System.err.println( "leftTree==rightTree; merge aborted" );
            return;
		}
		//allocate new node
		root = new MyBinaryNode<AnyType> (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;
	}
	
	//step 17
	public void printInOrder()
	{
		if (root != null) 
			root.printInOrder ();	//root is MyBinaryNode
	}

}






