How to detect if a given key lies in the binary search tree or not.
Searching in bellow illustrated example
struct node * search(struct node* root, int key){
if(root==NULL){
return NULL;
}
if(key==root->data){
return root;
}
else if(key<root->data){
return search(root->left, key);
}
else{
return search(root->right, key);
}
}
#include<stdio.h>
#include<malloc.h>
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* createNode(int data){
struct node *n; // creating a node pointer
n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap
n->data = data; // Setting the data
n->left = NULL; // Setting the left and right children to NULL
n->right = NULL; // Setting the left and right children to NULL
return n; // Finally returning the created node
}
struct node * search(struct node* root, int key){
if(root==NULL){
return NULL;
}
if(key==root->data){
return root;
}
else if(key<root->data){
return search(root->left, key);
}
else{
return search(root->right, key);
}
}
int main(){
// Constructing the root node - Using Function (Recommended)
struct node *p = createNode(5);
struct node *p1 = createNode(3);
struct node *p2 = createNode(6);
struct node *p3 = createNode(1);
struct node *p4 = createNode(4);
// Finally The tree looks like this:
// 5
// / \
// 3 6
// / \
// 1 4
// Linking the root node with left and right children
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
struct node* n = search(p, 10);
if(n!=NULL){
printf("Found: %d", n->data);
}
else{
printf("Element not found");
}
return 0;
}
struct node * searchIter(struct node* root, int key){
while(root!=NULL){
if(key == root->data){
return root;
}
else if(key<root->data){
root = root->left;
}
else{
root = root->right;
}
}
return NULL;
}
#include<stdio.h>
#include<malloc.h>
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* createNode(int data){
struct node *n; // creating a node pointer
n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap
n->data = data; // Setting the data
n->left = NULL; // Setting the left and right children to NULL
n->right = NULL; // Setting the left and right children to NULL
return n; // Finally returning the created node
}
struct node * searchIter(struct node* root, int key){
while(root!=NULL){
if(key == root->data){
return root;
}
else if(key<root->data){
root = root->left;
}
else{
root = root->right;
}
}
return NULL;
}
int main(){
// Constructing the root node - Using Function (Recommended)
struct node *p = createNode(5);
struct node *p1 = createNode(3);
struct node *p2 = createNode(6);
struct node *p3 = createNode(1);
struct node *p4 = createNode(4);
// Finally The tree looks like this:
// 5
// / \
// 3 6
// / \
// 1 4
// Linking the root node with left and right children
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
struct node* n = searchIter(p, 6);
if(n!=NULL){
printf("Found: %d", n->data);
}
else{
printf("Element not found");
}
return 0;
}