Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Can anyone provide executing code for preorder/inorder/postorder traversal without recursion?
Posted
Updated 10-Aug-13 8:17am
v2

Any effort? It looks like a school assignment.

First of all, we do not provide code here on CodeProject. The whole purpose of the community to help the programmers who want to learn and if they have problems, they can ask for help in the community.

Second, the purpose of assignment is to see the level of student to know if he/she has understood the concept. By asking the ready-made code, you are giving a wrong impression to your guide.
 
Share this answer
 
Comments
ridoy 10-Aug-13 14:15pm    
exactly,a 5.
Manas Bhardwaj is totally right!

But at least try to search for yourself.
Ah... you know google? Well, look what it has given to me for you: http://cybergrouch.blogspot.hu/2012/03/non-recursive-binary-tree-traversal.html[^]. But don't even dream to give that code as your homework, since your tutor will know, that it is too advanced for you. Still, you can compile and debug/trace it, and see how it works.
 
Share this answer
 
From how you posted your question one can assume that you have a tree to traverse where the nodes that are not leafes have some kind of operand in them and the leafes are numbers. Hoping you at least know what preorder, inorder and postorder are this is quite easy to work out using a recursive algorithm. Recursion works by presering state on the call stack where the local variables reside.

And now for something totally whack and never before thought of (kidding): Now just write an algorithm without using the implicit stack mechanism found in all the languages that support methods or procedures, but rather write a non-recursive algorithm that uses an explicit stack to keep track or where in the tree you currently are.

As the others before me already mentioned, do give it a try!

Regards,

— Manfred
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900