hi. is it possible to write an iterative version of the post-order and in-order binary tree traversal algorithms? I know how to do the one for preorder using stacks, but I can't get my head around the other two.
please help
friend,
Name:
Anonymous2006-10-10 5:17
It's really simple: think of how you would implement each traversal using recursion, then substitute a stack with push on entry and pop on exit for each call.
Which is exactly how it's done at machine level (at least without tail recursion removal).
Name:
Anonymous2011-06-17 19:40
Thank you for your kind and informative answer!
Name:
Anonymous2011-06-17 20:31
I just had an exam that asked for pre and post order BST and I couldn't remember which one was which.
Am I destined for ENTERPRISE RUBY?
Name:
Anonymous2011-06-18 1:25
no. probably doomed for IT aka geek squad. it's obvious from the function name what the order is.
preorder is
int traverse(treeNode *curr) {
if (curr != NULL) {
printf("%d", curr->item);
traverse(curr->left);
traverse(curr->right);
}
}
and postorder is moving the curr->item instruction after the recursive calls. simple.
Name:
Anonymous2011-06-18 15:02
>>2
Of if you want to be an asshole about it, do this without the aid of a stack.
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy