You are on page 1of 11

DSA 14TH 15TH TASK

TRISHIT GUPTA
20BIT0374

Q14
#include<iostream>

using namespace std;

class node

int ucode,stckamt;

float cost;

string drec,dexp,name;

node *left;

node *right;

int data;

public:

node *root=NULL;

node* insert(node *root,int u,string n,float c,int samt,string dr,string dx)

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;

node* search(node *root,int s,float price)

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;

void inorder(node *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;

void preorder(node *root)

if(root!=NULL)

cout<<"unicode:: "<<root->ucode<<"name:: "<<root->name<<" price:: "<<root->cost;

preorder(root->left);

preorder(root->right);

return;

void postorder(node *root)

if(root!=NULL)

postorder(root->left);

postorder(root->right);

cout<<"unicode:: "<<root->ucode<<"name:: "<<root->name<<" price:: "<<root->cost;

node* inordersucc(node *isucc)

if(isucc==NULL)

return NULL;

while(isucc->left!=NULL)

isucc=isucc->left;

return isucc;
}

node* del(node *root,int delcode)

node *temp;

if(root==NULL)

cout<<"element not present";

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;
}

void inorderdis1(node *root)

if(root!=NULL)

inorderdis1(root->left);

if(root->stckamt>0)

cout<<"unicode:"<<root->ucode<<" "<<"name:"<<root->name<<" "<<"number of stocks:"<<root-


>stckamt;

inorderdis1(root->right);

return;

void inorderdis2(node *root)

if(root!=NULL)

inorderdis2(root->left);

if(root->stckamt==0)

cout<<"unicode:"<<root->ucode<<" "<<"name:"<<root->name<<" "<<"number of stocks:"<<root-


>stckamt;

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";

cout<<"3-preorder 4-inorder 5-postorder 6-delete 7-inorderdis1 8-inorderdis2\n";

cin>>choice;

switch(choice)

case 1: cout<<"enter the data(unicode,name,cost,stockamount,dateofmanufacture,dateofexpiry) to


be inserted\n";

cin>>uni>>nn>>cc>>amt>>drc>>dxy;

x.root=x.insert(x.root,uni,nn,cc,amt,drc,dxy);

break;

case 2: cout<<"enter the unicode and price of the item to be updated\n";

cin>>s>>price;

x.root=x.search(x.root,s,price);

break;

case 3: cout<<"pre-order traversal\n";

x.preorder(x.root);

break;

case 4: cout<<"in-order traversal\n";

x.inorder(x.root);

break;

case 5: cout<<"post-order traversal\n";

x.postorder(x.root);

break;

case 6: cout<<"enter the unicode to be deleted\n";

cin>>delcode;

x.root=x.del(x.root,delcode);

break;

case 7: cout<<"products with stocks \n";


x.inorderdis1(x.root);

break;

case 8: cout<<"products with no stocks left\n";

x.inorderdis2(x.root);

break;

}while(choice<10);

}
Q15
#include<iostream>

#include<conio.h>

using namespace std;

void dijkstra(int source,int dest);

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)

cout<<dest<<"not reachable from"<<source<<"\n";

else

cout<<"shortest path ";

i=dest;

while(i!=source)

{ cout<<i<<"<--";

i=p[i];

cout<<i<<"="<<d[dest]<<"\n";

getch();

return 0;

void dijkstra(int source,int dest)

{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;

You might also like