Binary Search Tree Traversal


Traversing binary search tree is very important as most of its objectives are accomplished by traversing in various ways. Most popular Binary Search Tree traversals are

  • Inorder Traversal of Binary Search Tree
  • Preorder Traversal of Binary Search Tree
  • Postorder Traversal of Binary Search Tree
  • Pre-order traversals : the root node is visited, then the child nodes (left to right).
  • In-order traversals: only makes sense in binary trees – the left node will be visited, then the root node, and then the right node.
  • Post-order traversals: the child nodes will be visited before the root node (left to right).


void inorder ( struct tnode *p){
if( p != NULL){
inorder(p->left);
printf("%d\t",p->data);
inorder(p->right);
}
}

void preorder ( struct tnode *p){
if( p != NULL){
printf("%d\t",p->data);
preorder(p->left);
preorder(p->right);
}
}

void postorder ( struct tnode *p){
if( p != NULL){
postorder(p->left);
postorder(p->right);
printf("%d\t",p->data);
}
}

Advertisements
  1. Height of the binary tree! « Encrypt3d
  2. What is Binary Search Tree? « Encrypt3d

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: