preorder traversal (recursive)

silbhumi's Avatar, Join Date: May 2010
Newbie Member
void preorder(NODE *Node)
{
printf("%c ", Node->data);
preorder(Node->leftchild);
preorder(Node->rightchild);
}

In the above program fragment even though the statement 'preorder(Node->leftchild)'
occurs before the statement 'preorder(Node->rightchild)' the actual execution of the program shows that after printing the head node with the help of 'printf()', program control goes to 'preorder(Node->rightchild)'.

It seems that the issue here is related to SYSTEM STACK, but I am unable to say anything about it. Please try to explain in detail how such thing involving seemingly non-sequential execution comes into effect.

Also let me know about similar things occuring in postorder traversal of a binary tree.

Thanking you,

Yours

silbhumi@rediffmail.com
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Without the rest of the program, the data given and the output received, it's impossible to say. From the code given, the second printf *willl* be of Node->leftchild->data, so it's impossible to say why you think it's rightchild.