DFS Traversal of a Tree using Recursion in C

The following C program, using recursion, performs a Depth First Search traversal. Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure or graph. The concept of backtracking is used in DFS. In this program we are performing DFS on a binary tree. In DFS, the deepest and univisited node is visited and backtracks to it’s parent node if no siblings of that node exists.

Conditions: The DFS works on acyclic graph. DFS may fail if it enters a cycle. Care must be taken by not extending a path to a node if it already has.

Here is the source code of the C program to apply DFS on a binary tree recursively. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program for Depth First Binary Tree Search using Recursion
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int a;
  10.     struct node *left;
  11.     struct node *right;
  12. };
  13.  
  14. void generate(struct node **, int);
  15. void DFS(struct node *);
  16. void delete(struct node **);
  17.  
  18. int main()
  19. {
  20.     struct node *head = NULL;
  21.     int choice = 0, num, flag = 0, key;
  22.  
  23.     do
  24.     {
  25.         printf("\nEnter your choice:\n1. Insert\n2. Perform DFS Traversal\n3. Exit\nChoice: ");
  26.         scanf("%d", &choice);
  27.         switch(choice)
  28.         {
  29.         case 1: 
  30.             printf("Enter element to insert: ");
  31.             scanf("%d", &num);
  32.             generate(&head, num);
  33.             break;
  34.         case 2: 
  35.             DFS(head);
  36.             break;
  37.         case 3: 
  38.             delete(&head);
  39.             printf("Memory Cleared\nPROGRAM TERMINATED\n");
  40.             break;
  41.         default: 
  42.             printf("Not a valid input, try again\n");
  43.         }
  44.     } while (choice != 3);
  45.     return 0;
  46. }
  47.  
  48. void generate(struct node **head, int num)
  49. {
  50.     struct node *temp = *head, *prev = *head;
  51.  
  52.     if (*head == NULL)
  53.     {
  54.         *head = (struct node *)malloc(sizeof(struct node));
  55.         (*head)->a = num;
  56.         (*head)->left = (*head)->right = NULL;
  57.     }
  58.     else
  59.     {
  60.         while (temp != NULL)
  61.         {
  62.             if (num > temp->a)
  63.             {
  64.                 prev = temp;
  65.                 temp = temp->right;
  66.             }
  67.             else
  68.             {
  69.                 prev = temp;
  70.                 temp = temp->left;
  71.             }
  72.         }
  73.         temp = (struct node *)malloc(sizeof(struct node));
  74.         temp->a = num;
  75.         if (num >= prev->a)
  76.         {
  77.             prev->right = temp;
  78.         }
  79.         else
  80.         {
  81.             prev->left = temp;
  82.         }
  83.     }
  84. }
  85.  
  86. void DFS(struct node *head)
  87. {
  88.     if (head)
  89.     {
  90.         if (head->left)
  91.         {
  92.             DFS(head->left);
  93.         }
  94.         if (head->right)
  95.         {
  96.             DFS(head->right);
  97.         }
  98.         printf("%d  ", head->a);
  99.     }
  100. }
  101.  
  102. void delete(struct node **head)
  103. {
  104.     if (*head != NULL)
  105.     {
  106.         if ((*head)->left)
  107.         {
  108.             delete(&(*head)->left);
  109.         }
  110.         if ((*head)->right)
  111.         {
  112.             delete(&(*head)->right);
  113.         }
  114.         free(*head);
  115.     }
  116. }

$ cc pgm34.c
$ a.out
 
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 5
 
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 3
 
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 4
 
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 2
 
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 7
 
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 8
 
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 6
 
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 2
2  4  3  6  8  7  5  
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 3
Memory Cleared
PROGRAM TERMINATED

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Trees, go to Trees. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.