C++ program to perform Insert, Delete, Search an element into a binary search tree

/* Write a C++ Data structure program to perform the following operations:
a) Insert an element into a binary search tree.
b) Delete an element from a binary search tree.
c) for a key element in a binary search tree. */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
 
void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;
 
main()
{
	int ch,y;
	for(i=1;i<40;i++)
	tree[i]=-1;
	while(1)
	{
cout <<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter your choice:";
		cin >> ch;
		switch(ch)
		{
		case 1:
			cout <<"enter the element to insert";
			cin >> ch;
			insert(1,ch);
			break;
		case 2:
			cout <<"enter the element to delete";
			cin >>x;
			y=search(1);
			if(y!=-1) delte(y);
			else cout<<"no such element in tree";
			break;
		case 3:
			display(1);
			cout<<"\n";
			for(int i=0;i<=32;i++)
			cout <<i;
			cout <<"\n";
			break;
case 4:
			cout <<"enter the element to search:";
			cin >> x;
			y=search(1);
			if(y == -1) cout <<"no such element in tree";
			else cout <<x << "is in" <<y <<"position";
			break;
		case 5:
			exit(0);
		}
	}
}
 
void insert(int s,int ch )
{
	int x;
	if(t==1)
	{
		tree[t++]=ch;
		return;
	}
	x=search1(s,ch);
	if(tree[x]>ch)
		tree[2*x]=ch;
	else
		tree[2*x+1]=ch;
	t++;
}
void delte(int x)
{
	if( tree[2*x]==-1 && tree[2*x+1]==-1)
		tree[x]=-1;
	else if(tree[2*x]==-1)
	      {	tree[x]=tree[2*x+1];
		tree[2*x+1]=-1;
	      }
	else if(tree[2*x+1]==-1)
	      {	tree[x]=tree[2*x];
		tree[2*x]=-1;
	      }
	else
	{
	  tree[x]=tree[2*x];
	  delte(2*x);
	}
	t--;
}
 
int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}
 
void display(int s)
{
if(t==1)
{cout <<"no element in tree:";
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i];
return ;
}
 
int search1(int s,int ch)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return s/2;
if(tree[s] > ch)
search1(2*s,ch);
else search1(2*s+1,ch);
}

OUTPUT
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:3

no element in tree:
0123456789011121314151617181920212223242526272829303132

1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:1

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

Enter the element to insert 10
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:4

Enter the element to search: 10
10 is in 1 position
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT

Enter your choice:5


17 Responses to “C++ program to perform Insert, Delete, Search an element into a binary search tree”

  1. tejaswini

    I run the program in c with all the modifications…. but it shows a warning message: Parameter ‘s’ is never used in the display function. Why so…

    Reply
  2. showing this line “0123456789011121314151617181920212223242526272829303132” every time while m choosing “display” option ..

    Reply
  3. shadrack mahenge

    0123456789011121314151617181920212223242526272829303132″ every time while m choosing “display” option .. how to remove this

    Reply
    • Priyank Jain

      Dear Shadrack Mahenge, Please see the display function clearly. These numbers are just an indicator of the index number.

      Reply
  4. Priyank Jain

    Dear Shadrack Mahenge, Please see the display function clearly. These numbers are just an indicator of the index number.
    ADMIN ATTENTION: The above index should start from 1 upto 32 instead of starting from 0. if it start from 0, its left (2*I) and right(2*I+1) would also be zero.

    Reply
  5. Error
    1.function should a return type
    2.s is never used
    3.function have return value

    Reply
  6. aisa program dala he ki error ke alwa kuch bhi nahi he isme….if u dnt knw the logic thn why r u doind stupid programming….leave it nw….n go to hell for programming

    Reply
  7. gonmrod

    Your code es very very very… old and isn’t standard:
    #include //no standard
    #include // in c++ use cstdlib
    And the objects ?
    void insert(int,int );
    void delte(int);
    void display(int);
    int search(int);
    int search1(int,int);
    int tree[40],t=1,s,x,i;
    sugg:
    template
    struct BST {
    //…def…
    BST( int n) { tree = new T[n] ; //others }
    //…
    bool insert(const T & data) { //…bla bla …}
    //…
    };

    !!! … Please update the page and very thanks for you contribution … !!!

    Reply

Leave a Reply