# traversal of tree (short question)

• 11-25-2002
mackol
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 :D
• 11-25-2002
mackol
my thought is that the answer is

M F J J F T P P T M

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 :)
• 11-25-2002
Magos
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.
• 11-25-2002
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 :(
• 11-25-2002
Monster
Just remove the 1.4 statement

Preorder traversal
• 11-25-2002
Magos
Quote:

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...