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

4. 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. Just remove the 1.4 statement

Preorder traversal

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

Popular pages Recent additions