C Program to Display Tree Nodes using BFS Traversal

This C Program Display the Nodes of a Tree using BFS Traversal. Breadth-first search (BFS) is a strategy for searching in a graph when search is limited to essentially two operations: (a) visit and inspect a node of a graph; (b) gain access to visit the nodes that neighbor the currently visited node. The BFS begins at a root node and inspects all the neighboring nodes. Then for each of those neighbor nodes in turn, it inspects their neighbor nodes which were unvisited, and so on.

Here is source code of the C Program to Display the Nodes of a Tree using BFS Traversal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Display the Nodes of a Tree using BFS Traversal 
  3.  *                 40
  4.  *                 /\
  5.  *                20 60
  6.  *                /\  \
  7.  *              10 30  80
  8.  *                      \
  9.  *                       90
  10.  */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. struct btnode
  15. { 
  16.     int value; 
  17.     struct btnode *left, *right; 
  18. }; 
  19. typedef struct btnode node;
  20.  
  21. /* function declarations */
  22. void insert(node *, node *);
  23. void bfs_traverse(node *);
  24.  
  25. /*global declarations */
  26. node *root = NULL;
  27. int val, front = 0, rear = -1, i;
  28. int queue[20];
  29.  
  30. void main() 
  31. { 
  32.     node *new = NULL ; 
  33.     int num = 1; 
  34.     printf("Enter the elements of the tree(enter 0 to exit)\n"); 
  35.     while (1) 
  36.     {     
  37.         scanf("%d",  &num); 
  38.         if (num  ==  0) 
  39.             break; 
  40.         new = malloc(sizeof(node)); 
  41.         new->left = new->right = NULL; 
  42.         new->value = num; 
  43.         if (root == NULL) 
  44.             root = new; 
  45.         else 
  46.         { 
  47.             insert(new, root); 
  48.         } 
  49.     }
  50.     printf("elements in a tree in inorder are\n"); 
  51.     queue[++rear] = root->value;
  52.     bfs_traverse(root);
  53.     for (i = 0;i <= rear;i++)
  54.         printf("%d -> ", queue[i]);
  55.     printf("%d\n", root->right->right->right->value);
  56. }
  57.  
  58. /* inserting nodes of a tree */
  59. void insert(node * new , node *root) 
  60. { 
  61.     if (new->value>root->value) 
  62.     {     
  63.         if (root->right == NULL) 
  64.             root->right = new; 
  65.         else 
  66.             insert (new, root->right); 
  67.     } 
  68.     if (new->value < root->value) 
  69.     {     
  70.         if (root->left  ==  NULL) 
  71.             root->left = new; 
  72.         else 
  73.             insert (new, root->left); 
  74.     }     
  75. }
  76.  
  77. /* displaying elements using BFS traversal */
  78. void bfs_traverse(node *root)
  79. {
  80.     val = root->value;
  81.     if ((front <= rear)&&(root->value == queue[front]))
  82.     {
  83.         if (root->left != NULL)
  84.             queue[++rear] = root->left->value;
  85.         if (root->right != NULL || root->right  ==  NULL)
  86.             queue[++rear] = root->right->value;
  87.         front++;
  88.     }
  89.     if (root->left != NULL)
  90.     {
  91.         bfs_traverse(root->left);
  92.     }
  93.     if (root->right != NULL)
  94.     {
  95.         bfs_traverse(root->right);
  96.     }
  97. }

$ cc tree28.c
$ a.out
Enter the elements of the tree(enter 0 to exit)
40
20
10
30
60
70
80
0
elements in a tree in inorder are
40 -> 20 -> 60 -> 10 -> 30 -> 70 -> 80

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 programming examples on all topics, go to C Programming Examples.

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.