1
public class BinarySearchTree
{
public class TreeNode       
{   
private int item;
    private TreeNode leftLink;
    private TreeNode rightLink;
// One constructor for TreeNode
///////////////////////////////////////////////////////
    public TreeNode(int newItem, TreeNode left, TreeNode right)
    {
         item      = newItem;
         leftLink  = left;
         rightLink = right;
    }
}   //  End of TreeNode inner class

// Declaration of class BinarySearchTree begins here.
// Three instance variables.
private TreeNode root;
private TreeNode parent;    // parent node of a target node being sought
private int parentLink;     // pointer number of a parent node being sought


public BinarySearchTree( )
{
    root = parent = null;
    parentLink = 0;         // no such pointer at the beginning
}

i want a deep copy for this tree, heading should be same as given below, and it should work as describe, This constructor will create a binary search tree which is a deep copy of an existing one that is given by the lone parameter of this constructor.

public BinarySearchTree (BinarySearchTree  bst)
{

  //your code should be here
}
// more codes here but given code is ok for question
}
Rokso
  • 23
  • 1
  • 5
  • 1
    You need to give us more than just a block of code, see [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – Brad Koch Dec 07 '13 at 17:01

1 Answers1

2

You could add a function to your TreeNode:

public TreeNode deepCopy() {
    return new TreeNode(item, leftLink.deepCopy(), rightLink.deepCopy())
}

Then:

public BinarySearchTree (BinarySearchTree  bst) {
    root = bst.root.deepCopy();
    parent = bst.parent.deepCopy();
    parentLink = bst.parentLink;
}

This is an example of course, you should pay attention to null value.

You can find a better example here: How to deep copy a tree?

Community
  • 1
  • 1
HLP
  • 2,142
  • 5
  • 19
  • 20