-
How to duplicate a tree?
Hi!
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.
Code:
Root
|
(1)--------(3)---(5)
| | |
(2)--(6) (5) (2)--(3)--(1)
Thanks for your help!
-
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