How to duplicate a tree?
How do a duplicate a tree in the shortest way? My tree is implemented using linked list.
Each node contains a value, a pointer to its next sibling, a pointer to its first child.
For an example, the tree looks like this. Do not worry about how the tree is ordered in this way, I just like to know how do I duplicate the whole thing out? My current idea is use recursive function to tranverse the nodes to duplicate each node.
Thanks for your help!
| | |
(2)--(6) (5) (2)--(3)--(1)
Well you just use your normal traversal function to visit each node. You create a copy and call your tree insert function.
Yea, thanks salem! It's simpler than I thought, just a recursive function is all it takes