You are on page 1of 5

Aim:

Implementation of Binary Search Tree .

INPUT SPECIFICATION:
User has to entered his choice of operation they want to do that may be insert, delete and search
functions in a binary search tree

OUTPUT SPECIFICATION:
As per output is printed. entered choices by user

Theory:
Searching is an operation or a technique that helps finds the place of a given
element or value in the list. Any search is said to be successful or unsuccessful
depending upon whether the element that is being searched is found or not.
Types of searching techniques
 Linear Search or Sequential Search
 Index Sequential Search
 Binary Search
 Interpolation Search
 Hash Search

Binary search algorithm works on the principle of divide and conquer. For this
algorithm to work properly, the data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of
the collection. If a match occurs, then the index of item is returned. If the middle
item is greater than the item, then the item is searched in the sub-array to the left of
the middle item. Otherwise, the item is searched for in the sub-array to the right of
the middle item. This process continues on the sub-array as well until the size of
the subarray reduces to zero.

Example: Search for the key 20 in the following array using


Binary Search int a[ ] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22,
24, 26, 28, 30}

low=0, high=15, middle=(0+15)/2=7


a[middle] = 14 which is less than
key(20).

low=middle+1=7+1=8, high=15, middle =


(8+15)/2 = 11 a[middle] = 22 which is greater
than key.

low = 8, high = middle-1=11-1=10, middle=


(8+10)/2=9 a[middle]=18 which is less than key.

low= middle+1=9+1=10, high=10, middle =


(10+10)/2 = 10 a[middle] = 20 which is equal to
key(20).
So key found at index at 10
Algorithm:
Let ‘a’ be an array having ‘n’ elements. Let ‘key’ be the element to be searched.
1. Start
2. Initialize low=0, high = n-1
3. while low < = high do
3.1. mid = (low + high) / 2
3.2. if key = = a[mid]
return mid
else
if key < a[mid]
high = mid – 1
else
low = mid + 1
End while
4. Return -1, i.e. -1 will be returned if key is not present in the array
5. Stop

POGRAM CODE :

#include<stdio.h>
#include<conio.h>
int BinarySearch(int a[], int n, int key) {
int start = 0, last = n-1, mid;
while (start <= last) {
mid=(start + last)/2;
if(key == a[mid])
return mid;
else if (key < a[mid])
last = mid-1;
else
start = mid + 1;
}
return -1;
}
int main(){
int arry[20],i,n;
int flag, key;
//clrscr();
printf("\nEnter how many numbers you want to insert: ");
scanf("%d",& n);
printf("\nEnter the number\n");
for(i=0;i<n;i++)
scanf("%d",&arry[i]);
printf("\nEnter number to be serarch in given list:-");
scanf("%d",&key);
flag =BinarySearch(arry,n,key) ;
if(flag == -1)
printf("\nelement %d not found", key);
else
printf("\nelement %d found at position %d", flag);
getch();
return 0;
}
Conclusion:
Binary search is a fast search algorithm with run-time complexity of Ο(log n) when compared to
linear search whose worst case run-time complexity is O(n).

You might also like