#include<iostream>
using namespace std;
struct BST
{
int data,use;
}ob[100];
int main()
{
int flag,a,b,index;
char ch='y',c='y';
for(int i=0;i<100;i++)
{
ob[i].data=0;
ob[i].use=0;
}
cout<<"\nEnter the root element:";
cin>>ob[0].data;
ob[0].use=1;
while(ch=='y' || ch=='Y')
{
flag=0;
cout<<"\nEnter the element to push in BST:";
cin>>a;
if(a<ob[0].data)
{
if(ob[1].use!=1)
{
ob[1].data=a;
ob[1].use=1;
}
else
{
index=1;
while(flag!=1)
{
if(a<ob[index].data?index=2*index+1:index=2*index+2)
{
if(ob[index].use==0)
{
ob[index].data=a;
ob[index].use=1;
flag=1;
}
}
}
}
}
//****************************************************************
else if(a>ob[0].data)
{
if(ob[2].use!=1)
{
ob[2].data=a;
ob[2].use=1;
}
else
{
index=2;
while(flag!=1)
{
if(a<ob[index].data?index=2*index+1:index=2*index+2)
{
if(ob[index].use==0)
{
ob[index].data=a;
ob[index].use=1;
flag=1;
}
}
}
}
}
cout<<"\nWant to enter more elements in BST?(y/n):";
cin>>ch;
}
cout<<"\n\n************************************************************************\n\n";
while(c=='Y' || c=='y')
{
cout<<"\nEnter the index to display its data:";
cin>>b;
cout<<"\n->>"<<ob[b].data;
cout<<"\nWant to enter more elements in BST?(y/n):";
cin>>c;
}
return 0;
}
using namespace std;
struct BST
{
int data,use;
}ob[100];
int main()
{
int flag,a,b,index;
char ch='y',c='y';
for(int i=0;i<100;i++)
{
ob[i].data=0;
ob[i].use=0;
}
cout<<"\nEnter the root element:";
cin>>ob[0].data;
ob[0].use=1;
while(ch=='y' || ch=='Y')
{
flag=0;
cout<<"\nEnter the element to push in BST:";
cin>>a;
if(a<ob[0].data)
{
if(ob[1].use!=1)
{
ob[1].data=a;
ob[1].use=1;
}
else
{
index=1;
while(flag!=1)
{
if(a<ob[index].data?index=2*index+1:index=2*index+2)
{
if(ob[index].use==0)
{
ob[index].data=a;
ob[index].use=1;
flag=1;
}
}
}
}
}
//****************************************************************
else if(a>ob[0].data)
{
if(ob[2].use!=1)
{
ob[2].data=a;
ob[2].use=1;
}
else
{
index=2;
while(flag!=1)
{
if(a<ob[index].data?index=2*index+1:index=2*index+2)
{
if(ob[index].use==0)
{
ob[index].data=a;
ob[index].use=1;
flag=1;
}
}
}
}
}
cout<<"\nWant to enter more elements in BST?(y/n):";
cin>>ch;
}
cout<<"\n\n************************************************************************\n\n";
while(c=='Y' || c=='y')
{
cout<<"\nEnter the index to display its data:";
cin>>b;
cout<<"\n->>"<<ob[b].data;
cout<<"\nWant to enter more elements in BST?(y/n):";
cin>>c;
}
return 0;
}
No comments:
Post a Comment