简体   繁体   中英

Java Binary Search Tree Recursive Copy Tree

I'm working on a problem which requires me to copy a binary search tree recursively and to return the tree. I am coding in the binary search tree class, so it will copy whatever binary search tree it is called on. The requirements say that the private method must have a return type of Entry<E> and a parameter of type Entry<E> . The problem I'm running into is getting multiple entries added to the tree.

Here is what I currently have:

public BinarySearchTree<E> rcopy(){
   BinarySearchTree newTree = new BinarySearchTree();
   newTree.add(rcopy(root).element);
   return newTree;
}


private Entry <E> rcopy(Entry <E> current){
   if(current.left!=null) return rcopy(current.left);
   if(current.right!=null) return rcopy(current.right);
   return current;
}

And here is Entry class so you know what I have available to me:

protected static class Entry<E> {
    protected E element;
    protected Entry<E> left = null,
                       right = null,
                       parent;
    protected int  pos;
protected Entry<E> link = null;
public Entry() { }
    public Entry (E element, Entry<E> parent) 
{
       this.element = element;
       this.parent = parent;
    }
}
private Entry <E> rcopy(Entry <E> current){
   if(current.left!=null) return rcopy(current.left);
   if(current.right!=null) return rcopy(current.right);
   return current;
}

This will not copy anything. It will return the left-most ( or right-most, if no left child; or current, if it is a leaf node ) child of the current node. Because you always return current. You need somelthing like:

private Entry <E> rcopy(Entry <E> current){
    if (current == null) return null;
    return new Entry <E> (current.element, rcopy(current.left), rcopy(current.right)); //write a constructor for that
 }

and actually copy the nodes. I haven't tested the code and it is bit late, hope it is still correct.

Is there a reason you distinguish between BinarySearchTree<E> and Entry<E> ? Isn't a part of the tree also a tree?

Just thought I would share the solution that I got. My main problem was not doing a deep copy on the object so, it would reference the object instead of creating a new one.

public BinarySearchTree<E> rcopy(){
   BinarySearchTree<E> newTree = new BinarySearchTree<E>();
   newTree.root = rcopy(root);
   newTree.size=newTree.nodes();
   return newTree;
}
private Entry <E> rcopy(Entry <E> current){
   Entry <E> b=new Entry<E>();
   if(current!=null){
      if(current.left!=null)b.left=rcopy(current.left);
      if(current.right!=null)b.right=rcopy(current.right);
      b.element = current.element;
      b.parent = successor(current);
   }
   return b;
}

(successor is a method that returns an entry of the object that preceeds it) Thank you every one for help with the problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM