Saturday, September 12, 2009

PreOrder, Inorder and Post order

Preorder


preorder(N *root)
{
if(root)
{
printf("Value : [%d]", root->value);
preorder(root->left);
preorder(root->right);
}
}


Postorder

postorder(Node *root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
printf("Value : [%d]", root->value);
}
}




Inorder

inorder(Node *root)
{
if(root)
{
inorder(root->left);
printf("Value : [%d]", root->value);
inorder(root->right);
}
}



Time complexity of traversals is O(n).


PREV

No comments:

Post a Comment