Stack to simulate recursion

Hi,
I need help on my program. The program is to write an iterative version (using stack )of a postorder traversal on a binary tree.
The function for inserting tree works perfectly but when i call on the function iter_postorder()...nothing happens. Please tell me where I went wrong.

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First, it is not that nothing happens. It prints the top entry on the stack and then exits, right?
The array you declared does not do what you think.
Instead, up at the typedef, add another def for pointer to node, something like...
typedef struct Node
{
struct Node *left;
struct Node *right;
struct Node *parent;
int data;
}node, *pnode;

and then change the declaration to be...
pnode stack[MAX_STACK_SIZE];
see if that helps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

emmons, how is 'node* stack[];' different from 'pnode stack[];'. They look the same since 'pnode stack[];' will get translated to 'struct Node* stack[];'.

The other observation here is that only the leaf nodes get displayed on the screen. The reason is simple - all the non-leaf nodes get popped from the stack but do not get displayed. The solution is to push them back onto the stack. This brings us to one more question - if they are pushed back onto the stack, then the next time they are popped they will still have a right node & hence it will translate into an infinite loop!

This infinite loop can be avoided by checking if the popped out node's right child has already been processed. So when you are printing the popped out node, check if this node is the right child of the next node on the stack. If yes the next node has been completely processed & hence needs to be popped & printed.

BlackBeauty, note that I arrived at the following solution based on your code. My objective was to work on your code not to rewrite it. Hence this may not be the most elegant solution but it works