
January 9th, 2013, 01:45 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Since it's not clear which you're describing, we should discuss the concept of deep copy vs shallow copy when pointers are involved. Basically, with a shallow copy you have the copy pointing to the exact same data location as the original did, but with a deep copy you also create copies of what the original pointed to so that both the original and the copy has their very own and separate data. With the result of a shallow copy, if either the original or the copy changes the data, both the original and the copy will be affected, but with the result of a deep copy a change in the data of one will not affect the other.
A simple example would be copying a C-style string:
Code:
char *dest;
char *src = "A string.";
// shallow copy
dest = src; // now both pointers point to the same string
// deep copy
dest = malloc(strlen(src)+1);
strcpy(dest, src);
So to deep-copy the subtree it would be:
malloc root node of the new tree, copy the node's data fields to the new node.
malloc a child node and assign the pointer to the new root node's children field, then copy the child's data to the new child.
Repeat.
|