traversal of tree (short question)

This is a discussion on traversal of tree (short question) within the C Programming forums, part of the General Programming Boards category; with this pseudocode given what would be printed for the following tree algorithm traverse (val root <pointer to root node>) ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    Question traversal of tree (short question)

    with this pseudocode given what would be printed for the following tree

    algorithm traverse (val root <pointer to root node>)
    Code:
    1. if (root is not NULL)
        1. print (root)
        2. traverse (root->left)
        3. traverse (root->right)
        4. traverse (root)
    2. return
    and the tree would be
    Code:
                              (M)
                             /    \
                           (F)   (T)
                             \     /
                             (J) (P)
    thanks mates
    i actually need this answer in the next 4 hours so i am really hoping that someone sees it before then

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    71
    my thought is that the answer is

    M F J J F T P P T M


    but i am so not sure about this

    can u tell me what u think the answer is pleeeeeease

    i didnt understand recursion very well so its a little confusing for me with all that stack frames and stuff u know

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You print the node, then you print the left subtree, and last the right subtree. This is also known as preorder traversal.

    The preorder of your tree is then: M F J T P

    Why do you have traverse(root)??? That would probably lead to enless recursion.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    71
    yes i agree it is a preorder traversal but the problem lies in the 1.4 statement

    when it prints the root again

    to me i am not sure what happens then

    i know its gonna land up printing twice
    but in what order i am not sure

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Just remove the 1.4 statement

    Preorder traversal

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by mackol
    yes i agree it is a preorder traversal but the problem lies in the 1.4 statement

    when it prints the root again

    to me i am not sure what happens then

    i know its gonna land up printing twice
    but in what order i am not sure
    Not only twicem but an infiite number of times.
    You print the node's value, then call the function again with the current node, to print the node's value, then call the function again with the current node, to print the node's value, then call the function again with the current node, to print the node's value, then call the function again with the current node, to print the node's value, then call the function again with the current node, to print the node's value, then call the function again with the current node, to print the node's value, then call the function again with the current node, to print the node's value, then call the function again with the current node, to print the node's value, then call the function again with the current node...

    and so on...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree - one last question
    By tms43 in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2006, 02:58 AM
  2. problem in creating a tree
    By Lorin_sz in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2005, 01:28 PM
  3. problem in creating a tree
    By Lorin_sz in forum C Programming
    Replies: 1
    Last Post: 09-26-2005, 01:24 PM
  4. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 04:15 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21