Binary Tree traversal (recursive)

♠ Posted by GeekyFry in ,,,,, at 5:41 AM

Traversing a binary tree (recursive)


Pre-Order Traversal:
1
2
3
4
5
6
7
void preOrder(Node * t)
{
  if(t == NULL) return;
  (*visit)(t);  /* funtion to perform some action (like printing node) upon visiting */
  preOrder(t->left);
  preOrder(t->right);
}
In-Order Traversal:
1
2
3
4
5
6
7
void inOrder(Node * t)
{
  if(t == NULL) return;
  inOrder(t->left);
  (*visit)(t);  /* funtion to perform some action (like printing node) upon visiting */
  inOrder(t->right);
}
Post-Order Traversal:
1
2
3
4
5
6
7
void postOrder(Node * t)
{
  if(t == NULL) return;
  postOrder(t->left);
  postOrder(t->right);
  (*visit)(t);  /* funtion to perform some action (like printing node) upon visiting */
}

0 comments:

Post a Comment