Question

I have browsed the other questions and found solutions as to how to do a deep copy of objects that contain references. I am specifically wanting to make a deep copy of a tree. Logically, each tree node contain references to its children nodes. Here is the basics of my node class

public class BSTNode implements Comparable, Serializable  {

    private String token;
    private int count;
    private BSTNode leftChild;
    private BSTNode rightChild; 

I know I must only be making a shallow copy because when I make a copy of tree1, called tree2, and edit tree1 the edits also appear on tree2. Here is my copy method within my BSTNode class

public  BSTNode copy()
   {
   BSTNode obj = null;
   try{
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       ObjectOutputStream out = new ObjectOutputStream(bos);
       out.writeObject(this);
       out.flush();
       out.close();

       ObjectInputStream in= new ObjectInputStream(new ByteArrayInputStream(
               bos.toByteArray()));
       obj = (BSTNode) in.readObject();
   }
   catch(Exception e)
   {
       e.printStackTrace();
   }
   return obj;
 } 

When I wish to copy the entire tree, I call that above copy method from my BSTree class using the methods below. (I have 2 methods because this is a homework assignment that requires a copy method that calls a preorder traversal method)

  public BSTNode copy()
{
    BSTNode copiedTreeRoot = new BSTNode();
    return copyTree(copiedTreeRoot,root);
}

public BSTNode copyTree(BSTNode copiedTreeRoot, BSTNode otherTreeRoot)
{
    if(otherTreeRoot == null)
    {
        copiedTreeRoot = null;
    }
    else
    {
         copiedTreeRoot = otherTreeRoot.copy();
         copyTree(copiedTreeRoot.getLeft(), otherTreeRoot.getLeft());
        copyTree(copiedTreeRoot.getRight(), otherTreeRoot.getRight());          
    }
    return copiedTreeRoot;
}

I use these lines to create the new tree and assign the copy to it

BSTree tree2 = new BSTree();
tree2.setRoot(tree1.copy());

And further along when I make changes to tree1, tree 2 also changes. I have no clue what I'm doing wrong. I believe it must be somewhere in how I return the new tree or something. Any help is greatly appreciated!!

EDIT Here is where I call the copy originally. It is in a class of its own that has the main method tree2.setRoot(tree1.copy());

It then moves to the BSTree class and executes this

public BSTNode copy()
{
    //BSTNode copiedTreeRoot = new BSTNode();
   //return copyTree(copiedTreeRoot,root);
    return root.copyTree(root
}

The BSTree class has a member element of its root, called root It then bounces to the BSTNode class and executes this

 public BSTNode copyTree(BSTNode input)
{
   if(input == null)
   {
       return null;
   }
   else
   {
       BSTNode node = new BSTNode();
       node.token = input.token;
       node.count = input.count;
       node.leftChild = copyTree(input.leftChild);

       node.rightChild = copyTree(input.rightChild);
       System.out.println("node being returned: "+ node.getToken());
       return node;    
   }

}

When I go to print the output of the new tree, I get a blank.

Was it helpful?

Solution

     public class BSTNode
{
public String token;
public int count;

public BSTNode leftChild;
public BSTNode rightChild;


public static BSTNode copyTree(BSTNode input){
if(input == null){
return null;
}
else{
BSTNode node = new BSTNode();
node.token = input.token;
node.count = input.count;

node.leftChild = BSTNode.copyTree(input.leftChild);
node.rightChild = BSTNode.copyTree(input.rightChild);
return node;

}

}

public static void main(String args[]){
BSTNode root = new BSTNode();
root.token  = "root";
root.count = 1;

BSTNode leftChild  = new BSTNode();;
leftChild.token  = "left child";
leftChild.count = 2;

root.leftChild = leftChild;

BSTNode copy = copyTree(root);
System.out.println(copy.token);
System.out.println(copy.leftChild.token);

}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top