C Program to Search an Element in a Binary Search Tree

This C program implements search in Binary search tree. This is the data structure where inorder traversal of the binary search tree leads to sorting of elements.

Here is the source code of the C program to search element in Binary search 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 an element in a binary search tree
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct TreeNode {
  8.     int data;
  9.     struct TreeNode *leftChildNode;
  10.     struct TreeNode *rightChildNode;
  11. };
  12.  
  13. typedef struct TreeNode node;
  14. node *rootNode = NULL;
  15.  
  16. /*  Function to insert a node in a Binary search tree  */
  17. void insertNode(int i, node **n) {
  18.     if (*n == NULL) {
  19.         (*n) = (node*)malloc(sizeof(node));
  20.         (*n)->leftChildNode = NULL;
  21.         (*n)->rightChildNode = NULL;
  22.         (*n)->data = i;
  23.     }
  24.     else if ((*n)->data == i)
  25.         printf("\nThis value already exists in the tree!");
  26.     else if (i > (*n)->data)
  27.         insertNode(i, &((*n)->rightChildNode));
  28.     else
  29.         insertNode(i, &((*n)->leftChildNode));
  30. }
  31. /* End of insertNode() */
  32.  
  33. /*  Function to search an element in a Binary search tree  */
  34. void searchNode(int i, node **n) {
  35.     if (*n == NULL)
  36.         printf("\nValue does not exist in tree!");
  37.     else if((*n)->data == i)
  38.         printf("\nValue found!");
  39.     else if(i > (*n)->data)
  40.         searchNode(i, &((*n)->rightChildNode));
  41.     else
  42.         searchNode(i, &((*n)->leftChildNode));
  43. }
  44. /* End of serachNode() */
  45.  
  46. /* The main() program begins */
  47. int main()
  48. {
  49.     int ch, num, num1;
  50.     do {
  51.         printf("\nSelect a choice from the menu below.");
  52.         printf("\n1. Insert a node.");
  53.         printf("\n2. Search for a node.");
  54.         printf("\nChoice: ");
  55.         scanf("%d", &ch);
  56.         switch(ch) {
  57.         case 1: 
  58.             printf("\nEnter an element: ");
  59.             scanf("%d", &num);
  60.             insertNode(num, &rootNode);
  61.             break;
  62.         case 2: 
  63.             printf("\nEnter the element to be searched for: ");
  64.             scanf("%d", &num);
  65.             searchNode(num, &rootNode);
  66.             break;
  67.         default: 
  68.            exit(0);
  69.         }
  70.         printf("\nIf you want to return to the menu, press 1.");
  71.         printf("\nChoice: ");
  72.         scanf("%d", &num);
  73.     } while(num == 1);
  74.     return 0;
  75. }

$ gcc binarysearchtree.c
$ a.out
 
Select a choice from the menu below.
1. Insert a node.
2. Search for a node.
Choice: 1
 
Enter an element: 65
 
If you want to return to the menu, press 1.
Choice: 1
 
Select a choice from the menu below.
1. Insert a node.
2. Search for a node.
Choice: 1
 
Enter an element: 21
 
If you want to return to the menu, press 1.
Choice: 1
 
Select a choice from the menu below.
1. Insert a node.
2. Search for a node.
Choice: 2
 
Enter the element to be searched for: 65
 
Value found!
If you want to return to the menu, press 1.
Choice: 2

Sanfoundry Global Education & Learning Series – 1000 C Algorithms.

advertisement
advertisement
If you wish to look at all C Algorithms and Solutions, go to C Algorithms.

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.