Click to See Complete Forum and Search --> : data srtructire-Tree traversal


emerald09
April 11th, 2009, 05:58 AM
We are given a binnary tree, we have to do inorder traversal and postorder traversal.

Waht i have done is

Inorder traversal
1, 2 ,3 ,4 ,5, 6, 7, 8,9 , 10, 11, 12, 13 ,14 , 18

Postorder traversal
1,2, 6, 7,9,8,5,11, 10,12, 4, 3, 13, 18,14

Please correct me. The BST is in the attachment.

SlickHawk
April 11th, 2009, 09:54 PM
For a valid binary tree, an in-order traversal will always result in a strictly non-descending order, so yours is correct.

In a postorder traversal, you visit both your children before you visit yourself. Therefore, yours is wrong right off the bat because 1 must come after 2, since 2 is a child of 1.


void visit_node(Node * node)
{
if(!node)
return;

visit_node(node->left);
visit_node(node->right);
//do something with node's val, like print it
std::cout << node->val;
}


That's all there is to it. Just follow that down to get your order.