preorder traversal (recursive)

Discussion in 'C' started by silbhumi, May 14, 2010.

  1. silbhumi

    silbhumi New Member

    Joined:
    May 14, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice