To begin, we were asked to make a TREE... It says that we should traverse the left subtree of the ROOT/NODE in postorder and then make a copy of it... Next is to traverse the right subtree of the ROOT/NODE in postorder and then make a copy of it... Lastly, we should attach those copies of subtrees and display the tree...

So far i have this code:
THE BINARY TREE CLASS

class BinaryTree{

	BTNode root;

	BinaryTree(){
	}

	BinaryTree(BTNode n){
		root = n;
	}

	BinaryTree(BTNode n, BTNode left, BTNode right){
		root = n;
		root.left = left;
		root.right = right;
	}

	/* Outputs the preorder listing of elements in this tree */
	void preorder(){
		if (root != null) {
			System.out.println(root.info.toString());
			new BinaryTree(root.left).preorder();
			new BinaryTree(root.right).preorder();
		}
	}

	/* Outputs the inorder listing of elements in this tree */
	void inorder(){
		if (root != null) {
			new BinaryTree(root.left).inorder();
			System.out.println(root.info.toString());
			new BinaryTree(root.right).inorder();
		}
	}

	/* Outputs the postorder listing of elements in this tree */
	void postorder(){
		if (root != null) {
			new BinaryTree(root.left).postorder();
			new BinaryTree(root.right).postorder();
			System.out.println(root.info.toString());
		}
	}

	/* Copies this tree and returns the root of the duplicate */
	BTNode copy(){
		BTNode newRoot;
		BTNode newLeft;
		BTNode newRight;

		if (root != null){
			newLeft = new BinaryTree(root.left).copy();
			newRight = new BinaryTree(root.right).copy();
	  		newRoot = new BTNode(root.info, newLeft, newRight);
	  		return newRoot;
		}
		return null;
	}

	/* Compare another tree t2 if it's equivalent to this tree */
	boolean equivalent(BinaryTree t2){
		boolean answer = false;

		if ((root == null) && (t2.root == null)) answer = true;
		else {
			answer = (root.info.equals(t2.root.info));
			if (answer) answer =
					new BinaryTree(root.left).equivalent(
						new BinaryTree(t2.root.left));
			if (answer) answer =
					new BinaryTree(root.right).equivalent(
						new BinaryTree(t2.root.right));
		}
		return answer;
	}
	

	/* Main method used to test the methods */
	public static void main(String args[]){

		BinaryTree bt1 = new BinaryTree(new BTNode("Root1"), new BTNode("Left1"), new BTNode("Right1"));
		BinaryTree bt2 = new BinaryTree(new BTNode("Root2"), new BTNode("Left2"), new BTNode("Right2"));
		BinaryTree bt3 = new BinaryTree(new BTNode("Root1"), new BTNode("Left1"), new BTNode("Right1"));

		System.out.println("Preorder(bt1): "); bt1.preorder();
		System.out.println("Inorder(bt1): "); bt1.inorder();
		System.out.println("Postorder(bt1): "); bt1.postorder();

		BinaryTree bt4 = new BinaryTree(bt1.copy());
		System.out.println("Preorder (bt4): "); bt4.preorder();

		System.out.println(bt1.equivalent(bt2));
		System.out.println(bt1.equivalent(bt3));
	}

}

THE NODE CLASS

class BTNode {
	Object info;
	BTNode left, right;

	public BTNode(){
	}

	public BTNode(Object i) {
		info = i;
	}

	public BTNode(Object i, BTNode l, BTNode r) {
		info = i;
		left = l;
		right = r;
	}

/* Accessors and Mutators, if attributes are declared private *
	public void setLeft(BTNode l){ left = l; }
	public void setRight(BTNode r){ right = r; }
	public void setRight(Object i){ info = i; }
	public BTNode getLeft(){ return left; }
	public BTNode getRight(){ return right; }
	public Object getInfo(){ return info; }
*/

}

MAIN:

import java.io.*;

public class Main
{
	public static void main(String []args)
		throws IOException
		{
		BinaryTree tree1,tree2, lasttree;
		
		String choice;
		int nodeNum;
		int userInput;
		int slot=0;
		int i=0;
		String root;
		String leftChild1,leftChild2;
		String rightChild1,rightChild2;
		String parentNode1,parentNode2;
		
		BufferedReader read= new BufferedReader(new InputStreamReader(System.in));
				
		
		System.out.print("Enter value for root: ");
		root=read.readLine();
		
		//===LEFT SUBTREE===//
		System.out.println("\nINPUTS FOR LEFT SUBTREE\n");
			
		System.out.print("Enter value for parent node: ");
	 	parentNode1=read.readLine();
	 	
	 	System.out.print("Enter value for left child:  ");
	 	leftChild1=read.readLine();
	 	
		System.out.print("Enter value for right child: ");
	 	rightChild1=read.readLine();
	 	
	 	
	 	//===RIGHT SUBTREE===//
	 	System.out.println("\nINPUTS FOR RIGHT SUBTREE\n");
			
		System.out.print("Enter value for parent node: ");
	 	parentNode2=read.readLine();
	 	
	 	System.out.print("Enter value for left child:  ");
	 	leftChild2=read.readLine();
	 	
		System.out.print("Enter value for right child: ");
	 	rightChild2=read.readLine();
	 	
	 	
		tree1 = new BinaryTree(new BTNode(parentNode1), new BTNode(leftChild1), new BTNode(rightChild1));
		
		tree2 = new BinaryTree(new BTNode(parentNode2), new BTNode(leftChild2), new BTNode(rightChild2));
	 
	 	System.out.println("\nTRAVERSAL OF THE LEFT SUBTREE");
	 	System.out.println("Postorder Traversal: "); tree1.postorder();
	 	
	 	System.out.println("\nTRAVERSAL OF THE RIGHT SUBTREE");
	 	System.out.println("Postorder Traversal: "); tree2.postorder();
	 
		//===COPY OF SUBTREES===//
		BinaryTree tree3 = new BinaryTree(tree1.copy());
		System.out.println("\n=>>Copy of left subtree: "); tree3.postorder();	
		String copy1=""+tree3+"";
			
		BinaryTree tree4 = new BinaryTree(tree2.copy());
		System.out.println("\n=>>Copy of right subtree: "); tree4.postorder();	
		String copy2=""+tree4+"";		
			
		//===COPY OF NODE w/ LEFT & RIGHT SUBTREES===//
		lasttree = new BinaryTree(new BTNode(root), new BTNode(copy1), new BTNode(copy2));
		System.out.println("\nCOMPLETE COPY OF THE TREE \n"); lasttree.postorder();	
		
}
}

I WANT TO SHOW THE COMPLETE TREE...
WHATS WRONG WITH THESE CODES:

//===COPY OF NODE w/ LEFT & RIGHT SUBTREES===//
		lasttree = new BinaryTree(new BTNode(root), new BTNode(copy1), new BTNode(copy2));
		System.out.println("\nCOMPLETE COPY OF THE TREE \n"); lasttree.postorder();

PLease help... Thanks!

where shoud i put that method?
what are the arguments?
thanks!

class BTNode have three fields info,left,right - write them

class BTNode {

    Object info;
    BTNode left, right;

    public String toString() {
        return "info=" + info.toString() + ":" + left + "," + right;
    }
//...

class BinaryTree have one field

class BinaryTree {

    BTNode root;

    public String toString() {
        return "root=" + root.toString();
    }
//...

Edited 7 Years Ago by quuba: better view

This article has been dead for over six months. Start a new discussion instead.