Let's say we want to search for the number key, We start at the root. Then:
We compare the value to be searched with the value of the root.
If it's equal we are done with the search.
If it's smaller we know that we need to go to the left subtree.
If it's greater we search in the right subtree.
Repeat the above step till no more traversal is possible
If at any iteration, key is found, return True. If the node is null, return False.
Illustration of searching a key in BST:
See the illustration below for a better understanding:
Searching in Binary Search Tree using Recursion:
C++
#include<iostream>usingnamespacestd;// Node structureclassNode{public:intdata;Node*left;Node*right;Node(intitem){data=item;left=right=nullptr;}};boolsearch(Node*root,intkey){// root is null -> return falseif(root==nullptr)returnfalse;// if root has key -> return trueif(root->data==key)returntrue;if(key>root->data)returnsearch(root->right,key);elsereturnsearch(root->left,key);}intmain(){// Creating BST// 6// / \ // 2 8// / \ // 7 9Node*root=newNode(6);root->left=newNode(2);root->right=newNode(8);root->right->left=newNode(7);root->right->right=newNode(9);intkey=7;// Searching for key in BSTcout<<search(root,key)<<endl;}
C
#include<stdio.h>#include<stdlib.h>#include<stdbool.h>// Node structurestructNode{intdata;structNode*left;structNode*right;};// Create new nodestructNode*newNode(intitem){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=item;node->left=node->right=NULL;returnnode;}boolsearch(structNode*root,intkey){// root is null -> return falseif(root==NULL)returnfalse;// if root has key -> return trueif(root->data==key)returntrue;if(key>root->data)returnsearch(root->right,key);returnsearch(root->left,key);}intmain(){// Creating BST// 6// / \ // 2 8// / \ // 7 9structNode*root=newNode(6);root->left=newNode(2);root->right=newNode(8);root->right->left=newNode(7);root->right->right=newNode(9);intkey=7;// Searching for key in the BSTprintf("%d\n",search(root,key));}
Java
// Node StructureclassNode{intdata;Nodeleft,right;publicNode(intitem){data=item;left=right=null;}}classGFG{staticbooleansearch(Noderoot,intkey){// root is null -> return falseif(root==null)returnfalse;// if root has key -> return trueif(root.data==key)returntrue;if(key>root.data)returnsearch(root.right,key);returnsearch(root.left,key);}publicstaticvoidmain(String[]args){// Creating BST// 6// / \// 2 8// / \// 7 9Noderoot=newNode(6);root.left=newNode(2);root.right=newNode(8);root.right.left=newNode(7);root.right.right=newNode(9);intkey=7;// Searching for key in the BSTSystem.out.println(search(root,key));}}
Python
# Node structureclassNode:def__init__(self,item):self.data=itemself.left=Noneself.right=Nonedefsearch(root,key):# root is null -> return falseifrootisNone:returnFalse# if root has key -> return trueifroot.data==key:returnTrueifkey>root.data:returnsearch(root.right,key)returnsearch(root.left,key)# Creating BST# 6# / \# 2 8# / \# 7 9root=Node(6)root.left=Node(2)root.right=Node(8)root.right.left=Node(7)root.right.right=Node(9)key=7# Searching for key in the BSTprint(search(root,key))
C#
usingSystem;// Node StructureclassNode{publicintdata;publicNodeleft,right;publicNode(intitem){data=item;left=right=null;}}classGFG{staticboolsearch(Noderoot,intkey){// root is null -> return falseif(root==null)returnfalse;// if root has key -> return trueif(root.data==key)returntrue;if(key>root.data)returnsearch(root.right,key);returnsearch(root.left,key);}publicstaticvoidMain(){// Creating BST// 6// / \// 2 8// / \// 7 9Noderoot=newNode(6);root.left=newNode(2);root.right=newNode(8);root.right.left=newNode(7);root.right.right=newNode(9);intkey=7;// Searching for key in the BSTConsole.WriteLine(search(root,key));}}
JavaScript
// Node structureclassNode{constructor(item){this.data=item;this.left=null;this.right=null;}}functionsearch(root,key){// root is null -> return falseif(root===null)returnfalse;// if root has key -> return trueif(root.data===key)returntrue;if(key>root.data)returnsearch(root.right,key);returnsearch(root.left,key);}// Creating BST// 6// / \// 2 8// / \// 7 9constroot=newNode(6);root.left=newNode(2);root.right=newNode(8);root.right.left=newNode(7);root.right.right=newNode(9);constkey=7;// Searching for key in the BSTconsole.log(search(root,key));
Output
1
Time complexity: O(h), where h is the height of the BST. Auxiliary Space: O(h) This is because of the space needed to store the recursion stack.
We can avoid the auxiliary space and recursion overhead withe help of iterative implementation. Below is the iterative implementation that works in O(h) time and O(1) auxiliary space.
Searching in Binary Search Tree using Iterative approach: