Tree Traversal : Depth First Search in C

Tree Traversal : Depth First Search in C

Depth first search in C. DFS is an algorithm for traversing or searching tree data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.

Here, in this page we will discuss the C program for DFS tree traversal.

C language

Depth First Search (DFS) In C

  • Inorder Traversal : In inorder traversal, the left subtree is visited first, then the root and later the right sub-tree. Inorder traversal for the above example is 40 20 50 10 60 30 70.

     Algorithm inorder(tree)

  1.    Recursively traverse left subtree.
  2.   Visit root node.
  3.   Recursively traverse right subtree.

 

  • Preorder Traversal : In preorder traversal , the root node is visited first, then the left subtree and finally the right subtree. Preorder Traversal for the above example is 10 20 40 50 30 60 70.

      Algorithm  preorder(Tree):

  1. Visit root node.
  2. Recursively traverse left subtree.
  3. Recursively traverse right subtree.

 

  • Postorder Traversal : In postorder traversal , first we traverse the left subtree, then the right subtree and finally the root node. Postorder traversal for the above example is 40 50 20 60 70 30 10.

    Algorihtm postorder(Tree):

  1. Recursively traverse left sub-tree.
  2. Recursively traverse right sub-tree.
  3. Visit root node.
Depth first search in C

C Code for Depth First Search

Run
#include<stdio.h>
#include<stdlib.h>
struct node
{
  int data;
  struct node *left;
  struct node *right;
};

struct node *newNode (int data)
{
  struct node *node = (struct node *) malloc (sizeof (struct node));
  node->data = data;
  node->left = NULL;
  node->right = NULL;

  return (node);
}

/* Given a binary tree, print its nodes in inorder*/
void Inorder (struct node *node)
{
  if (node == NULL)
    return;

  Inorder (node->left);
  printf ("%d ", node->data);
  Inorder (node->right);
}

/* Given a binary tree, print its nodes in preorder*/
void Preorder (struct node *node)
{
  if (node == NULL)
    return;

  printf ("%d ", node->data);
  Inorder (node->left);
  Inorder (node->right);
}

/* Given a binary tree, print its nodes in postorder*/
void Postorder (struct node *node)
{
  if (node == NULL)
    return;

  Inorder (node->left);
  Inorder (node->right);
  printf ("%d ", node->data);
}

/* Driver program to test above functions*/
int main ()
{
  struct node *root = newNode (10);
  root->left = newNode (20);
  root->right = newNode (30);
  root->left->left = newNode (40);
  root->left->right = newNode (50);

  printf ("\nInorder traversal of binary tree is \n");
  Inorder (root);

  printf ("\nPreorder traversal of binary tree is \n");
  Preorder (root);

  printf ("\nPostorder traversal of binary tree is \n");
  Postorder (root);

  getchar ();
  return 0;
}

Output:

Inorder traversal of binary tree is
40 20 50 10 30

Preorder traversal of binary tree is
10 40 20 50 30

Postorder traversal of binary tree is
40 20 50 30 10