Prasanna Raman wrote:
Thank you. Sorry, I was only trying to edit but clicked on the wrong option.
Coming to your response, I just read somewhere that this kind of recursion (since this is not tail recursive) takes up a lot of stack space and hence the iterative version might be better. Is that correct? If not, why is recursion better for trees? I see that the code is cleaner, but any other reason?
To be fair, not all tree data-structures have a parallel list connecting all the nodes. So, in most cases, you don't get to choose which is better.
As for me, I seem to heavily favor recursive as an algorithm -- perhaps it is from my period with Prolog -- so, I would choose recursion... for me, it is cleaner, and easier to understand. However, years ago, I implemented a recursive solution, and was told to remove it. Why? The code was part of an appliance, and given a large amount of data, and not enough stack, it would be possible to cause a stack overflow. When you are writing code of the operating system, and running in an appliance, that is an error that can't be tolerated... so I guess, that is another reason to avoid recursion.
Henry