#include<iostream>
using namespace std;
#include<stdlib.h>
struct BST_Ar_L
{
int data,dex;
struct BST_Ar_L *left;
struct BST_Ar_L *right;
}*root,*l,*r,*temp,*ctr;
int main()
{
int a[100],t,index,m,n,key,flag=0;
char ch='y';
for(int i=0;i<99;i++)
a[i]=-1;
cout<<"\nEnter the elements of BST as per array implementation(starting from '0' index)\n";
while(ch=='y' || ch=='Y')
{
cout<<"\nEnter the index of array:";
cin>>t;
cout<<"\nEnter the element of BST at that index:";
cin>>a[t];
cout<<"\nWant to enter more elements?(Y/N):";
cin>>ch;
}
if(a[0]==-1)
{
cout<<"\nNo root element\nProgram will now exit...";
return 0;
}
root=new BST_Ar_L;
root->data=a[0];
root->dex=0;
root->left=NULL;
root->right=NULL;
if(a[1]!=-1)
{
l=new BST_Ar_L;
l->data=a[1];
l->dex=1;
l->left=NULL;
l->right=NULL;
root->left=l;
}
if(a[2]!=-1)
{
r=new BST_Ar_L;
r->data=a[2];
r->dex=2;
r->left=NULL;
r->right=NULL;
root->right=r;
}
index=3;
while(index!=99)
{
if(a[index]==-1)
{
index++;
continue;
}
else
{
temp=new BST_Ar_L;
temp->data=a[index];
if(index%2==1)
{
n=(index-1)/2;
m=n;
if(m%2==0)
while(m!=1)
m=(m-2)/2;
else
while(m!=2)
m=(m-1)/2;
if(m==1)
ctr=l;
else
ctr=r;
while(ctr->dex!=n)
ctr=n%2?ctr->left:ctr->right;
index%2?ctr->left=temp:ctr->right=temp;
temp->dex=index;
temp->left=NULL;
temp->right=NULL;
}
else
{
n=(index-2)/2;
m=n;
if(m%2==0)
while(m!=2)
m=(m-2)/2;
else
while(m!=1)
m=(m-1)/2;
if(m==1)
ctr=l;
else
ctr=r;
while(ctr->dex!=n)
n%2?ctr=ctr->left:ctr=ctr->right;
index%2?ctr->left=temp:ctr->right=temp;
temp->dex=index;
temp->left=NULL;
temp->right=NULL;
}
index++;
}
}
system("cls");
ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"\n********************************************************************************";
cout<<"\n********************************************************************************\n";
cout<<"\nEnter the element to display its details in BST:";
cin>>key;
flag=0;
for(int i=0;i<100;i++)
{
if(a[i]==key)
flag=1;
}
if(flag==1)
{
if(key==a[0])
{
cout<<"\nIt is the root element";
if(a[1]!=-1 && a[2]!=-1)
cout<<"\nRight Child="<<a[2]<<"\t\tLeft Child="<<a[1] ;
else if(a[1]!=-1 && a[2]!=1)
cout<<"\nRight Child=empty"<<"\t\tLeft Child="<<a[1] ;
else
cout<<"\nRight Child="<<a[2]<<"\t\tLeft Child=empty" ;
}
else
{
key<a[0]?ctr=l:ctr=r;
temp=ctr;
flag=0;
while(flag!=1)
{
if(ctr->data==key)
{
if(ctr->dex==1 || ctr->dex==2)
{
if(ctr->right!=NULL && ctr->left!=NULL)
cout<<"\nParent element="<<a[0]<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child="<<ctr->left->data;
else if(ctr->right!=NULL && ctr->left==NULL)
cout<<"\nParent element="<<a[0]<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child=empty";
else if(ctr->right==NULL && ctr->left!=NULL)
cout<<"\nParent element="<<a[0]<<"\nRight Child=empty"<<"\t\tLeft Child="<<ctr->left->data;
else
cout<<"\nParent element="<<a[0]<<"\nRight Child=empty"<<"\t\tLeft Child=empty";
flag=1;
}
else
{
if(ctr->right!=NULL && ctr->left!=NULL)
cout<<"\nParent element="<<temp->data<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child="<<ctr->left->data;
else if(ctr->right!=NULL && ctr->left==NULL)
cout<<"\nParent element="<<temp->data<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child=empty";
else if(ctr->right==NULL && ctr->left!=NULL)
cout<<"\nParent element="<<temp->data<<"\nRight Child=empty"<<"\t\tLeft Child="<<ctr->left->data;
else
cout<<"\nParent element="<<temp->data<<"\nRight Child=empty"<<"\t\tLeft Child=empty";
flag=1;
flag=1;
}
}
else
{
if(key<(ctr->data))
{
temp=ctr;
ctr=ctr->left;
}
else
{
temp=ctr;
ctr=ctr->right;
}
}
}
}
}
else
cout<<"\nElement not present in BST!!";
cout<<"\nWant to display more elements?(Y/N):";
cin>>ch;
system("cls");
}
}
using namespace std;
#include<stdlib.h>
struct BST_Ar_L
{
int data,dex;
struct BST_Ar_L *left;
struct BST_Ar_L *right;
}*root,*l,*r,*temp,*ctr;
int main()
{
int a[100],t,index,m,n,key,flag=0;
char ch='y';
for(int i=0;i<99;i++)
a[i]=-1;
cout<<"\nEnter the elements of BST as per array implementation(starting from '0' index)\n";
while(ch=='y' || ch=='Y')
{
cout<<"\nEnter the index of array:";
cin>>t;
cout<<"\nEnter the element of BST at that index:";
cin>>a[t];
cout<<"\nWant to enter more elements?(Y/N):";
cin>>ch;
}
if(a[0]==-1)
{
cout<<"\nNo root element\nProgram will now exit...";
return 0;
}
root=new BST_Ar_L;
root->data=a[0];
root->dex=0;
root->left=NULL;
root->right=NULL;
if(a[1]!=-1)
{
l=new BST_Ar_L;
l->data=a[1];
l->dex=1;
l->left=NULL;
l->right=NULL;
root->left=l;
}
if(a[2]!=-1)
{
r=new BST_Ar_L;
r->data=a[2];
r->dex=2;
r->left=NULL;
r->right=NULL;
root->right=r;
}
index=3;
while(index!=99)
{
if(a[index]==-1)
{
index++;
continue;
}
else
{
temp=new BST_Ar_L;
temp->data=a[index];
if(index%2==1)
{
n=(index-1)/2;
m=n;
if(m%2==0)
while(m!=1)
m=(m-2)/2;
else
while(m!=2)
m=(m-1)/2;
if(m==1)
ctr=l;
else
ctr=r;
while(ctr->dex!=n)
ctr=n%2?ctr->left:ctr->right;
index%2?ctr->left=temp:ctr->right=temp;
temp->dex=index;
temp->left=NULL;
temp->right=NULL;
}
else
{
n=(index-2)/2;
m=n;
if(m%2==0)
while(m!=2)
m=(m-2)/2;
else
while(m!=1)
m=(m-1)/2;
if(m==1)
ctr=l;
else
ctr=r;
while(ctr->dex!=n)
n%2?ctr=ctr->left:ctr=ctr->right;
index%2?ctr->left=temp:ctr->right=temp;
temp->dex=index;
temp->left=NULL;
temp->right=NULL;
}
index++;
}
}
system("cls");
ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"\n********************************************************************************";
cout<<"\n********************************************************************************\n";
cout<<"\nEnter the element to display its details in BST:";
cin>>key;
flag=0;
for(int i=0;i<100;i++)
{
if(a[i]==key)
flag=1;
}
if(flag==1)
{
if(key==a[0])
{
cout<<"\nIt is the root element";
if(a[1]!=-1 && a[2]!=-1)
cout<<"\nRight Child="<<a[2]<<"\t\tLeft Child="<<a[1] ;
else if(a[1]!=-1 && a[2]!=1)
cout<<"\nRight Child=empty"<<"\t\tLeft Child="<<a[1] ;
else
cout<<"\nRight Child="<<a[2]<<"\t\tLeft Child=empty" ;
}
else
{
key<a[0]?ctr=l:ctr=r;
temp=ctr;
flag=0;
while(flag!=1)
{
if(ctr->data==key)
{
if(ctr->dex==1 || ctr->dex==2)
{
if(ctr->right!=NULL && ctr->left!=NULL)
cout<<"\nParent element="<<a[0]<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child="<<ctr->left->data;
else if(ctr->right!=NULL && ctr->left==NULL)
cout<<"\nParent element="<<a[0]<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child=empty";
else if(ctr->right==NULL && ctr->left!=NULL)
cout<<"\nParent element="<<a[0]<<"\nRight Child=empty"<<"\t\tLeft Child="<<ctr->left->data;
else
cout<<"\nParent element="<<a[0]<<"\nRight Child=empty"<<"\t\tLeft Child=empty";
flag=1;
}
else
{
if(ctr->right!=NULL && ctr->left!=NULL)
cout<<"\nParent element="<<temp->data<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child="<<ctr->left->data;
else if(ctr->right!=NULL && ctr->left==NULL)
cout<<"\nParent element="<<temp->data<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child=empty";
else if(ctr->right==NULL && ctr->left!=NULL)
cout<<"\nParent element="<<temp->data<<"\nRight Child=empty"<<"\t\tLeft Child="<<ctr->left->data;
else
cout<<"\nParent element="<<temp->data<<"\nRight Child=empty"<<"\t\tLeft Child=empty";
flag=1;
flag=1;
}
}
else
{
if(key<(ctr->data))
{
temp=ctr;
ctr=ctr->left;
}
else
{
temp=ctr;
ctr=ctr->right;
}
}
}
}
}
else
cout<<"\nElement not present in BST!!";
cout<<"\nWant to display more elements?(Y/N):";
cin>>ch;
system("cls");
}
}
No comments:
Post a Comment