I had a question about a Binary Sorted Tree design principle.
I need to create a deep copy of a Binary Expression Tree, and I am accomplishing this by going through all the nodes in the tree and creating a new, identical one.
I already have a treeIterator set up for other uses and was wondering if an iterator would be faster, slower, or about the same speed/memory usage as doing it recursively.
Thanks!
I think recursion would be faster.
I don’t know your iterator’s exact implementation, but I assume that it goes to each node? If your BST is based on a root node structure, then going to each node (as in a iterator) would be slower than recursion.
Here’s how I would implement it:
Recursively,
Create a new root node (identical to the original root node).
Add copies of the original root’s left and right nodes. (If they exist)