could someone please help me, I need to write a deep copy constructor for BST. I have already written a method insert and checkEmpty. Can anyone help? thanks
deep copy as in clone? you mean you want to duplicate a BST in a construtor?
do u mean something like
public class bstnode
{
int bstnodevalue;
bstnode next;
.....
}
public class bst
{
bstnode left;
bstnode right;
public bst(bst tree) //constructor
{
...//every node of this input tree is copied over
}
}
i think in the constructor, this.left = tree.left and this.right = tree.right will assign new memory reference to the nodes. but if you don't want memory reassignment but value reassignment, then i think you need some recursion to do the insertion but i'm stuck at it..very sorry
}
[/tt]
this has some redundant copying in it to compensate for the root node of the tree, but it should illustrate how this is to be done. you'll probly have to add extra methods 'addLeft(Object)' and 'addRight(Object)' for this to work.
good luck. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.