C Program to Search a Node in Binary Tree

This C Program search for a particular value in a binary tree.

Here is source code of the C Program to search for a particular value in a binary tree. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Search for a Particular Value in a Binary Tree
  3.  *                  50
  4.  *                  /\
  5.  *                20  30
  6.  *                /\                               
  7.  *              70 80
  8.  *              /\   \
  9.  *            10 40  60
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <malloc.h>
  14. /* Structure to create the binary tree */
  15.  
  16. struct btnode
  17. {
  18.     int value;
  19.     struct btnode *l;
  20.     struct btnode *r;
  21. };
  22.  
  23. struct btnode *root = NULL;
  24. int flag;
  25.  
  26. /* Function Prototypes */
  27. void in_order_traversal(struct btnode *);
  28. void in_order_search(struct btnode *,int);
  29. struct btnode *newnode(int);
  30.  
  31. void main()
  32. { 
  33.     /* Inserting elements in the binary tree */
  34.     int search_val;
  35.     root = newnode(50);
  36.     root->l = newnode(20);
  37.     root->r = newnode(30);
  38.     root->l->l = newnode(70);
  39.     root->l->r = newnode(80);
  40.     root->l->l->l = newnode(10);
  41.     root->l->l->r = newnode(40);
  42.     root->l->r->r = newnode(60);
  43.  
  44.     printf("The elements of Binary tree are:");
  45.     in_order_traversal(root);
  46.     printf("Enter the value to be searched:");
  47.     scanf("%d", &search_val);
  48.     in_order_search(root, search_val);
  49.     if (flag  =  =  0)    // flag to check if the element is present in the tree or not
  50.     {
  51.         printf("Element not present in the binary tree\n");
  52.     }
  53. }
  54.  
  55. /* Code to dynamically create new nodes */
  56. struct btnode* newnode(int value)
  57. {
  58.     struct btnode *temp = (struct btnode *)malloc(sizeof(struct btnode));
  59.     temp->value = value;
  60.     temp->l = NULL;
  61.     temp->r = NULL;
  62.     return temp;
  63. }
  64.  
  65. /* Code to display the elements of the binary tree */
  66.  
  67. void in_order_traversal(struct btnode *p)
  68. {
  69.     if (!p)
  70.     {
  71.         return;
  72.     }
  73.     in_order_traversal(p->l);
  74.     printf("%d->", p->value);
  75.     in_order_traversal(p->r);
  76. }
  77.  
  78. /* Code to search for a particular element in the tree */
  79. void in_order_search(struct btnode *p, int val)
  80. {
  81.     if (!p)
  82.     {
  83.         return;
  84.     }
  85.     in_order_search(p->l, val);
  86.     if(p->value == val)
  87.     {
  88.         printf("\nElement present in the binary tree.\n");
  89.         flag = 1;
  90.     }
  91.     in_order_search(p->r, val);
  92. }

$ cc tree3.c
$ a.out
The elements of Binary tree are:10->70->40->20->80->60->50->30
Enter the value to be searched:60
Element present in the binary tree.
 
$ ./a.out
The elements of Binary tree are:10->70->40->20->80->60->50->30
Enter the value to be searched:99
Element not present in the binary tree

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.