C Program to Perform Searching in a BST

This is a C Program to perform searching an element in BST. To search a given key in Bianry Search Tree, we first compare it with root, if the key is present at root, we return root. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur for left subtree.

Here is source code of the C Program to Perform Searching in a BST. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node {
  5.     int key;
  6.     struct node *left, *right;
  7. };
  8.  
  9. // A utility function to create a new BST node
  10. struct node *newNode(int item) {
  11.     struct node *temp = (struct node *) malloc(sizeof(struct node));
  12.     temp->key = item;
  13.     temp->left = temp->right = NULL;
  14.     return temp;
  15. }
  16.  
  17. // A utility function to do inorder traversal of BST
  18. void inorder(struct node *root) {
  19.     if (root != NULL) {
  20.         inorder(root->left);
  21.         printf("%d ", root->key);
  22.         inorder(root->right);
  23.     }
  24. }
  25.  
  26. /* A utility function to insert a new node with given key in BST */
  27. struct node* insert(struct node* node, int key) {
  28.     /* If the tree is empty, return a new node */
  29.     if (node == NULL)
  30.         return newNode(key);
  31.  
  32.     /* Otherwise, recur down the tree */
  33.     if (key < node->key)
  34.         node->left = insert(node->left, key);
  35.     else if (key > node->key)
  36.         node->right = insert(node->right, key);
  37.  
  38.     /* return the (unchanged) node pointer */
  39.     return node;
  40. }
  41.  
  42. struct node* search(struct node* root, int key) {
  43.     // Base Cases: root is null or key is present at root
  44.     if (root == NULL || root->key == key)
  45.         return root;
  46.  
  47.     // Key is greater than root's key
  48.     if (root->key < key)
  49.         return search(root->right, key);
  50.  
  51.     // Key is smaller than root's key
  52.     return search(root->left, key);
  53. }
  54.  
  55. // Driver Program to test above functions
  56. int main() {
  57.     /* Let us create following BST
  58.      50
  59.      /     \
  60.    30      70
  61.      /  \    /  \
  62. 20   40  60   80 */
  63.     struct node *root = NULL;
  64.     root = insert(root, 50);
  65.     insert(root, 30);
  66.     insert(root, 20);
  67.     insert(root, 40);
  68.     insert(root, 70);
  69.     insert(root, 60);
  70.     insert(root, 80);
  71.  
  72.     // print inoder traversal of the BST
  73.     printf("Inorder traversal: ");
  74.     inorder(root);
  75.     printf("\nThe key %d found at: %u",30, search(root, 30));
  76.     return 0;
  77. }

Output:

$ gcc SearchBST.c
$ ./a.out
 
Inorder traversal: 20 30 40 50 60 70 80 
The key 30 found at: 8530784

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 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.