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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java