views:

2708

answers:

9
+2  A: 

If you read carefully you see that the first "definition" says to start left of the root and that the order of the nodes is determined by when you pass under them. So B is not the first node, as you pass it from the left on the way to A, then first pass under A after which you go up and pass under B. Therefore it seems that both definitions give the same result.

mweerden
+7  A: 
John Boker
That's fine it's cleared it up, thanks
Chris S
How does this answer the question?
ForYourOwnGood
@ForYourOwnGood: Correctly.
Jonathan Leffler
It shows the definitions aren't conflicting at all: They have exactly the same effect.
Tim
Chris already said that the traversal was A, B, C, D, E, F, G, H, I , so by drawing that sequence thats answers his question? He already knew that.
ForYourOwnGood
Chris' reading of the first definition was wrong, specifically "passes underneath". Chris has BDCE but you pass under C before D. So. other than the red line on A not pointing down (and the one under G being marginal :-), this diagram explains it all, esp. with the "directly above" comment.
paxdiablo
+1 for the correct answer, although the diagram also won me over :-).
paxdiablo
@ForYourOwnGood the image example the academic text gave didn't have as many nodes so I was using that instead of reading the text properly (mweerden's comment)
Chris S
+1  A: 

Both definitions give the same result. Don't be fooled by the letters in the first example - look at the numbers along the path. The second example does use letters to denote the path - perhaps that is what is throwing you off.

For example, in your example order showing how you thought the second tree would be traversed using the algorithm of the first one, you place "D" after "B" but you shouldn't because there is still a left-hand child node of D available (that's why the first item says "the order in which this line passes underneath them."

Mark Brittingham
A: 

But according to (my understanding of) definition #1, this should be

A, B, D, C, E, F, G, I, H

Unfortunately, your understanding is wrong.

Whenever you arrive at a node, you must descend to an available left node, before you look at the current node, then you look at an available right node. When you chose D before C, you didn't descend to the left node first.

Calyth
+1  A: 

Forget the definitions, it's so much easier to just apply the algorithm:

void inOrderPrint(Node root)
{
  if (root.left != null) inOrderPrint(root.left);
  print(root.name);
  if (root.right != null) inOrderPrint(root.right);
}

It's just three lines. Rearrange the order for pre- and post- order.

Andrew Coleson
A: 

Hey according to me as mentioned in wiki is correct the sequence for a inorder traversal is left-root-right.

Till A, B, C, D, E, F i think you have understood already. Now after root F the next node is G that doesn't hav a left node but a right node so as per the rule (left-root-right) its null-g-right. Now I is the right node of G but I has a left node hence the traversal would be GHI. This is correct.

Hope this helps.

swaps
A: 

For an inline tree traversal you have to keep in mind that the order of traversal is left-node-right. For the above diagram that you are conflicted on, your error occurs when you read a parent node before reading any leaf(children) nodes to the left.

The proper traversal would be: as far left as possible with leaf nodes(A), return to parent node(B), move to the right, but since D has a child to its left you move down again(C), back up to C's parent(D), to D's right child(E), reverse back to the root(F), move to the right leaf(G), move to G's leaf but since it has a left leaf node move there(H), return to parent(I).

the above traversal reads the node when I have it listed in parenthesis.

Matt
A: 

I personally found this lecture quite helpful.

c411
A: 

hey i think the first binary tree with the root of "a" is a Binary tree which is not correctly constructed...

thanks...try to implement that all the left side of the tree should be less than the root and the right side of the tree is greater than or equal to the root

Chester Ryan M. Pascua