Professional Documents
Culture Documents
TRISHIT GUPTA
20BIT0374
Q14
#include<iostream>
class node
int ucode,stckamt;
float cost;
string drec,dexp,name;
node *left;
node *right;
int data;
public:
node *root=NULL;
if(root==NULL)
root=new node;
root->ucode=u;
root->cost=c;
root->name=n;
root->stckamt=samt;
root->drec=dr;
root->dexp=dx;
root->left=NULL;
root->right=NULL;
else
if(u<root->ucode)
root->left=insert(root->left,u,n,c,samt,dr,dx);
else
root->right=insert(root->right,u,n,c,samt,dr,dx);
return root;
if(s<root->ucode)
root->left=search(root->left,s,price);
else if (s>root->ucode)
root->right=search(root->right,s,price);
else
root->cost=price;
return root;
if(root!=NULL)
inorder(root->left);
cout<<"unicode::";
cout<<root->ucode<<" ";
cout<<"name::";
cout<<root->name;
cout<<"price:: ";
cout<<root->cost<<" ";
inorder(root->right);
return;
if(root!=NULL)
preorder(root->left);
preorder(root->right);
return;
if(root!=NULL)
postorder(root->left);
postorder(root->right);
if(isucc==NULL)
return NULL;
while(isucc->left!=NULL)
isucc=isucc->left;
return isucc;
}
node *temp;
if(root==NULL)
else if(delcode<root->ucode)
root->left=del(root->left,delcode);
else if(delcode>root->ucode)
root->right=del(root->right,delcode);
else
if(root->left!=NULL&&root->right!=NULL)
temp=inordersucc(root->right);
root->ucode=temp->ucode;
root->right=del(root->right,root->ucode);
else
temp=root;
if(root->left!=NULL)
root=root->left;
else if(root->right!=NULL)
root=root->right;
else
root=NULL;
delete(temp);
return root;
}
if(root!=NULL)
inorderdis1(root->left);
if(root->stckamt>0)
inorderdis1(root->right);
return;
if(root!=NULL)
inorderdis2(root->left);
if(root->stckamt==0)
inorderdis2(root->right);
return;
};
int main()
int uni,amt,s,delcode;
string nn,drc,dxy;
float cc,price;
node x;
int choice;
do
cout<<"\n1-insert 2-search\n";
cin>>choice;
switch(choice)
cin>>uni>>nn>>cc>>amt>>drc>>dxy;
x.root=x.insert(x.root,uni,nn,cc,amt,drc,dxy);
break;
cin>>s>>price;
x.root=x.search(x.root,s,price);
break;
x.preorder(x.root);
break;
x.inorder(x.root);
break;
x.postorder(x.root);
break;
cin>>delcode;
x.root=x.del(x.root,delcode);
break;
break;
x.inorderdis2(x.root);
break;
}while(choice<10);
}
Q15
#include<iostream>
#include<conio.h>
int d[6],p[6];
int i,j,n;
int a[6][6]={{0,15,10,99999,45,99999},{99999,0,15,99999,20,99999},{20,99999,0,20,99999,99999},
{99999,10,99999,0,35,99999},{99999,99999,99999,30,0,99999},{99999,99999,99999,4,99999,0}
};
int main()
{ int source,dest;
for(source=0;source<6;source++)
{ for(dest=0;dest<6;dest++)
dijkstra(source,dest);
if(d[dest]==99999)
else
i=dest;
while(i!=source)
{ cout<<i<<"<--";
i=p[i];
cout<<i<<"="<<d[dest]<<"\n";
getch();
return 0;
{int i,u,v,s[6],min;
for(i=0;i<6;i++)
d[i]=a[source][i];
s[i]=0;
p[i]=source;
s[source]=1;
for(i=1;i<6;i++)
{
min=99999;
u=-1;
for(j=0;j<6;j++)
if(s[j]==0)
if(d[j]<min)
min=d[j];
u=j;
if(u==-1)
return;
s[u]=1;
if(u==dest)
return;
for(v=0;v<6;v++)
if(s[v]==0)
{ if(d[u]+a[u][v]<d[v])
{ d[v]=d[u]+a[u][v];
p[v]=u;