Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

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.

Depth first search
Depth first search in C

In-order, Pre-order and Post-order tree traversal

  • 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.

C Code for Depth First Search

#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 printInorder(struct node* node)
{
if (node == NULL)
return;

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

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

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

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

printInorder(node->left);
printInorder(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");
printInorder(root);

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

printf("\nPostorder traversal of binary tree is \n");
printPostorder(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