C Program to Traverse the Tree using Recursion

The following C program, using recursion, performs traversal operation across the nodes in a tree. The tree we have used is the binary search tree. The user generates a tree by inserting integers. The user is also asked to select one of the three modes of traversal, ie, infix, prefix and postfix. The user can use any of the three options to see the result. A binary search tree follows a concept of the nodes whose numbers are lesser than the parent/pointed node are linked to the left and the nodes whose number are greater than the parent/pointed node are linked to the right.

Here is the source code of the C program to traverse in a tree iteratively. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Traverse the Tree Recursively
  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 infix(struct node *);
  16. void postfix(struct node *);
  17. void prefix(struct node *);
  18. void delete(struct node **);
  19.  
  20. int main()
  21. {
  22.     struct node *head = NULL;
  23.     int choice = 0, num, flag = 0, key;
  24.  
  25.     do
  26.     {
  27.         printf("\nEnter your choice:\n1. Insert\n2. Traverse via infix\n3.Traverse via prefix\n4. Traverse via postfix\n5. Exit\nChoice: ");
  28.         scanf("%d", &choice);
  29.         switch(choice)
  30.         {
  31.         case 1: 
  32.             printf("Enter element to insert: ");
  33.             scanf("%d", &num);
  34.             generate(&head, num);
  35.             break;
  36.         case 2: 
  37.             infix(head);
  38.             break;
  39.         case 3: 
  40.             prefix(head);
  41.             break;
  42.         case 4: 
  43.             postfix(head);
  44.             break;
  45.         case 5: 
  46.             delete(&head);
  47.             printf("Memory Cleared\nPROGRAM TERMINATED\n");
  48.             break;
  49.         default: printf("Not a valid input, try again\n");
  50.         }
  51.     } while (choice != 5);
  52.     return 0;
  53. }
  54.  
  55. void generate(struct node **head, int num)
  56. {
  57.     struct node *temp = *head, *prev = *head;
  58.  
  59.     if (*head == NULL)
  60.     {
  61.         *head = (struct node *)malloc(sizeof(struct node));
  62.         (*head)->a = num;
  63.         (*head)->left = (*head)->right = NULL;
  64.     }
  65.     else
  66.     {
  67.         while (temp != NULL)
  68.         {
  69.             if (num > temp->a)
  70.             {
  71.                 prev = temp;
  72.                 temp = temp->right;
  73.             }
  74.             else
  75.             {
  76.                 prev = temp;
  77.                 temp = temp->left;
  78.             }
  79.         }
  80.         temp = (struct node *)malloc(sizeof(struct node));
  81.         temp->a = num;
  82.         if (num >= prev->a)
  83.         {
  84.             prev->right = temp;
  85.         }
  86.         else
  87.         {
  88.             prev->left = temp;
  89.         }
  90.     }
  91. }
  92.  
  93. void infix(struct node *head)
  94. {
  95.     if (head)
  96.     {
  97.         infix(head->left);
  98.         printf("%d   ", head->a);
  99.         infix(head->right);
  100.     }
  101. }
  102.  
  103. void prefix(struct node *head)
  104. {
  105.     if (head)
  106.     {
  107.         printf("%d   ", head->a);
  108.         prefix(head->left);
  109.         prefix(head->right);
  110.     }
  111. }
  112.  
  113. void postfix(struct node *head)
  114. {
  115.     if (head)
  116.     {
  117.         postfix(head->left);
  118.         postfix(head->right);
  119.         printf("%d   ", head->a);
  120.     }
  121. }
  122.  
  123. void delete(struct node **head)
  124. {
  125.     if (*head != NULL)
  126.     {
  127.         if ((*head)->left)
  128.         {
  129.             delete(&(*head)->left);
  130.         }
  131.         if ((*head)->right)
  132.         {
  133.             delete(&(*head)->right);
  134.         }
  135.         free(*head);
  136.     }
  137. }

$ pgm36.c
$ a.out
 
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 1
Enter element to insert: 5
 
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 1
Enter element to insert: 3
 
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 1
Enter element to insert: 4
 
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 1
Enter element to insert: 6
 
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 1
Enter element to insert: 2
 
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 2
2   3   4   5   6   
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 3
5   3   2   4   6   
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 4
2   4   3   6   5   
Enter your choice:
1. Insert
2. Traverse via infix
3. Traverse via prefix
4. Traverse via postfix
5. Exit
Choice: 5
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.