C Program to Print Left View of a Binary Tree

This C Program Prints only Nodes in Left SubTree.

Here is source code of the C program to Print only Nodes in Left SubTree. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print only Nodes in Left SubTree 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int data;
  10.     struct node* left;
  11.     struct node* right;
  12. };
  13.  
  14. int queue[100];
  15. int front = 0, rear = 0, val; 
  16.  
  17. /*Function to traverse the tree using Breadth First Search*/
  18. void bfs_traverse(struct node *node)
  19. {
  20.     val = node->data;
  21.     if ((front< = rear)&&(node->data =  = queue[front]))
  22.     {
  23.         if (node->left != NULL)
  24.             queue[rear++] = node->left->data;
  25.         if (node->right != NULL)
  26.             queue[rear++] = node->right->data;
  27.         front++;
  28.     }
  29.     if (node->left != NULL)
  30.     {
  31.         bfs_traverse(node->left);
  32.     }
  33.     if (node->right != NULL)
  34.     {
  35.         bfs_traverse(node->right);
  36.     }
  37. }
  38.  
  39. struct node* newnode(int data)
  40. {
  41.     struct node* node  =  (struct node *)malloc(sizeof(struct node));
  42.     node->data  =  data;
  43.     node->left  =  NULL;
  44.     node->right  =  NULL;
  45.  
  46.     return(node);
  47. }
  48.  
  49. int main()
  50. { 
  51.     int i;
  52.  
  53.     /*
  54.     The input tree is as shown below
  55.                 40
  56.                 / \
  57.             20        60
  58.             / \       \
  59.         10        30      80
  60.                           \
  61.                             90
  62.     */
  63.     struct node *root  =  newnode(40);
  64.     root->left         =  newnode(20);
  65.     root->right        =  newnode(60);
  66.     root->left->left   =  newnode(10);
  67.     root->left->right  =  newnode(30);
  68.     root->right->right  =  newnode(80);
  69.     root->right->right->right  =  newnode(90);
  70.     queue[rear++] = root->left->data;
  71.     bfs_traverse(root->left);
  72.     for (i = 0;i < rear;i++)
  73.         printf("%d->", queue[i]);
  74.     return 0;
  75. }

$ cc tree32.c
$ a.out
20->10->30->

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.

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.