Write a method called copy that returns a reference to a new IntTree that is an independent copy of the original tree. Do not change the original tree.
Add the above method to the IntTree class from this chapter. You may define additional private methods to implement your public method if necessary. Several problem descriptions refer to the following reference binary trees:
public IntTree copy()
{
IntTree newIntTree = new IntTree(1);
copy(overallRoot, newIntTree.overallRoot);
return newIntTree;
}
private void copy(IntTreeNode root, IntTreeNode newRoot)
{
if(root == null)
{
newRoot = null;
}
else
{
newRoot.data = root.data;
newRoot.left = root.left;
newRoot.right = root.right;
copy(root.left, newRoot.left);
copy(root.right, newRoot.right);
}
}