|
where I'm using recursion though, it would have been nearly impossible to duplicate the process without it.
Recursion removal is quite common. For example, here is a bit of C code to
traverse a binary tree (using recursion)
traverse( struct node *t) {
if (t != z) {
visit(t);
traverse(t->left);
traverse(t->right);
}
}
By replacing the recursion code with a stack, the following non-recursive
implementation can be made:
traverse(struct node *t) {
push(t);
while (!stackempty()) {
t = pop();
visit(t);
if (t->right != z) push(t->right);
if (t->left != z) push(t->left);
}
}
If you are interested in reading all the gory details, this bit is from
'Algorithms in C' by Robert Sedgewick. I use code similar to the above to
traverse nodes in an XML document.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.