Are there examples of recursion using only heap area?
I do it this way :
then...
Usually, I implement this as a utility class "Recursor" Objects implement the interface "IRecursable"
The Recursor class can be asked to recurse Depth-First or Width-First.
My implementation also calls RecursionBegin() and RecursionEnd() on the root node, so that the class being recursed can handle any initialisation and cleanup code required.
The recursed class can also have the root notified of each called peer - allowing it to see :
RecursionBegin() - Called once, before recursion starts.
RecursionElement() - Called multiple times (optional).
RecursionEnd() - Called once, after recursion ends.
These methods are a part of the IRecursable interface and can be overridden to allow the class to be flexible in how it uses the recurser.
But there is a more concise method
Sometimes I move these utilities into a single class called Recursable that combines the features of class Recursor and interface IRecursable… or combine them fully into the object class meaning that the class to be recursed becomes self-recursable, without any need for supporting classes.
Hope that helps someone make a start on converting Recursive algorithms to Heap-based algorithms... and avoid clobbering the stack or causing nasty Stack-Overflows
TBH, recursion is evil and should be avoided like the plague! (except where your language allows true tail-call recursion)
… respect the Stack, it is a precious resource!!!