You are on page 1of 59

ADVANCED DATA STRUCTURES LAB

WEEK 1:

C++ programs to implement the following using an array.

a) Stack ADT

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x ;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow”;
return;
}
cout <<"deleted" <<stk[top--] ;
}
void display()
{

09L61A0 1 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

if(top<0)
}

cout <<" stack empty";


return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}

OUTPUTS
1 push 2.pop 3.display 4.exit
Enter your choice2
stack under flow
1. push 2.pop 3.display 4.exit
Enter your choice1
enter the element2
inserted2

09L61A0 2 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

1. push 2.pop 3.display 4.exit


Enter your choice1
enter the element3
inserted3
1.push 2.pop 3.display 4.exit
Enter your choice2
deleted3
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element5
inserted5
1.push 2.pop 3.display 4.exit
Enter your choice3
52
1. push 2. pop 3. display 4.exit
Enter your choice4

b)Queue ADT

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;

09L61A0 3 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

return;
}
queue1[++rear]=x;
cout <<"inserted" <<x ;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==fron t)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queu e1[i]<<" ";
}
};
main()
{
int ch;
queue qu;
while(1)
{
cout <<"\n1.insert 2.delet 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet(); break;

09L61A0 4 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

case 3: qu.display();break;
case 4: exit(0);
}
}
return (0);
}

OUTPUT
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element21
inserted21
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element22
inserted22
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element16
inserted16
1.insert 2.delet 3.display 4.exit
Enter ur choice3
21 22 16 1.insert 2.delet 3.display 4.exit
Enter ur choice2
deleted21
1.insert 2.delet 3.display 4.exit
Enter ur choice3
22 16
1.insert 2.delet 3.display 4.exit
Enter ur choice

09L61A0 5 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

WEEK 2:

Write C++ programs to implement the following using a singly linked


list.
a) Stack ADT

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *next;
int data;
};
class stack : public node
{
node *head;
int tos;
public:
stack()
{
tos=-1;
}
void push(int x)
{
if (tos < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
tos ++;
}
else
{
node *temp,*temp1;
temp=head;
if(tos >= 4)
{

09L61A0 6 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

cout <<"stack over flow";


return;
}
tos++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x ;
}
}
void display()
{
node *temp;
temp=head;
if (tos < 0)
{
cout <<" stack under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( tos < 0 )
{
cout <<"stack under flow";
return;
}
tos--;
while(temp->next->next!=NULL)
{
temp=temp->next;

09L61A0 7 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

}
temp->next=NULL;
}
};
main()
{
stack s1;
int ch;
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ur choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"\n enter a element";
cin >> ch;
s1.push(ch);
break;
case 2: s1.pop();
break;
case 3: s1.display();
break;
case 4: exit(0);
}
}
return (0);
}

OUTPUT
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ur choice:1
enter a element23

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:1
enter a element67

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:3
23 67

09L61A0 8 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:3
23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ur choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:2
stack under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ur choice:4

b) Queue ADT

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *next;
int data;
};
class queue : public node
{
node *head;
int front,rare;
public:
queue()
{
front=-1;
rare=-1;
}
void push(int x)

09L61A0 9 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

{
if (rare < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
rare ++;
}
else
{
node *temp,*temp1;
temp=head;
if(rare >= 4)
{
cout <<"queue over flow";
return;
}
rare++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x ;
} }
void display()
{
node *temp;
temp=head;
if (rare < 0)
{
cout <<" queue under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}

09L61A0 10 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

void pop()
{
node *temp;
temp=head;
if( rare < 0)
{
cout <<"queue under flow";
return;
}
if(front == rare)
{
front = rare =-1;
head=NULL;
return;
}
front++;
head=head->next;
}
};
main()
{
queue s1;
int ch;
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ru choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"\n enter a element";
cin >> ch;
s1.push(ch); break;
case 2: s1.pop();break;
case 3: s1.display();break;
case 4: exit(0);
}
}
return (0);
}

09L61A0 11 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

OUTPUT

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:1
enter a element23

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:1
enter a element54

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:3
23 54
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ur choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT


enter ur choice:2
queue under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ur choice:4

09L61A0 12 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

Week-3
Write C++ program to implement the deque
(double ended queue) ADT using a
doubly linked list

Coding:

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
int data;
class node *next;
class node *prev;
};
class dqueue: public node
{
node *head,*tail;
int top1,top2;
public:
dqueue()
{
top1=0;
top2=0;
head=NULL;
tail=NULL;
}
void push(int x){
node *temp;
int ch;
if(top1+top2 >=5)
{
cout <<"dqueue overflow";
return ;
}
if( top1+top2 == 0)
{
head = new node;
head->data=x;
head->next=NULL;
head->prev=NULL;

09L61A0 13 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

tail=head;
top1++;
}
else

{
cout <<" Add element 1.FIRST 2. LAST\n enter ur choice:";
cin >> ch;
if(ch==1)
{
top1++;
temp=new node;
temp->data=x ;
temp->next=head;
temp->prev=NULL;
head->prev=temp;
head=temp;
}
else
{
top2++;
temp=new node;
temp->data=x ;
temp->next=NULL;
temp->prev=tail;
tail->next=temp;
tail=temp;
}
}
}
void pop()
{
int ch;
cout <<"Delete 1.First Node 2.Last Node\n Enter ur choice:";
cin >>ch;
if(top1 + top2 <=0)
{
cout <<"\nDqueue under flow";
return;
}
if(ch==1)
{
head=head->next;
head->prev=NULL;
top1--;
}
else
{

09L61A0 14 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

top2--;
tail=tail->prev;
tail->next=NULL;
}
}
void display()
{
int ch;
node *temp;
cout <<"display from 1.Staring 2.Ending\n Enter ur choice";
cin >>ch;
if(top1+top2 <=0)
{
cout <<"under flow";
return ;
}
if (ch==1)
{
temp=head;
while(temp!=NULL)
{
cout << temp->data <<" ";
temp=temp->next;
}
}
else
{
temp=tail;
while( temp!=NULL)
{
cout <<temp->data << " ";
temp=temp->prev;
}
}
}
};
main()
{
dqueue d1;
int ch;
while (1){
cout <<"1.INSERT 2.DELETE 3.DISPLAU 4.EXIT\n Enter ur choice:";

cin >>ch;
switch(ch)
{
case 1: cout <<"enter element";

09L61A0 15 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

cin >> ch;


d1.push(ch); break;
case 2: d1.pop(); break;
case 3: d1.display(); break;
case 4: exit(1);
}
}
}

OUTPUT
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice:1
enter element4
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice:1
enter element5
Add element 1.FIRST 2.LAST
enter ur choice:1
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice:1
enter element6
Add element 1.FIRST 2.LAST
enter ur choice:2
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice:3
display from 1.Staring 2.Ending
Enter ur choice1
5 4 6 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice:2
Delete 1.First Node 2.Last Node
Enter ur choice:1
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice:3
display from 1.Staring 2.Ending
Enter ur choice1
4 6 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice:4

09L61A0 16 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

WEEK 4:

Write a C++ 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) Search 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;

09L61A0 17 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

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

09L61A0 18 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

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

09L61A0 19 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

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:
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:1
Enter the element to insert 10
1.INSERT
2.DELETE

09L61A0 20 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

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

09L61A0 21 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

Week-5

Write C++ program that uses non-recursive functions to traverse a


binary tree in
a)Pre-order b)Inorder c) Post order

Coding:

a) Pre-order:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *left;
class node *right;
int data;
};
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{

09L61A0 22 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;
temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
}
node *search(node *temp,int ch)
{
if(root== NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right== NULL)
return temp;
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
void display(node *temp)
{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data <<" ";
display(temp->right);
}
void preorder( node *root)

09L61A0 23 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

{
node *p,*q;
p=root;
q=NULL;
top=0;
while(p!=NULL)
{
cout <<p->data << " ";
if(p->right!=NULL)
{
stk[top]=p->right- >data;
top++;
}
p=p->left;
if(p==NULL && top>0)
{
p=pop(root);
}
}
}
node * pop(node *p)
{
int ch;
ch=stk[top-1] ;
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
main()
{
tree t1;
int ch,n,i;
while(1)

09L61A0 24 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

{
cout <<"\n1.INSERT\n2.DISPLAY 3.PREORDER
TRAVERSE\n4.EXIT\nEnter your choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cout<<"\n enter the elements";
cin >> n;
for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.preorder(t1.root); break;
case 4: exit(1);
}
}
}
OUTPUT
1.INSERT
2.DISPLAY 3.PREORDER TRAVE RSE
4.EXIT
Enter your choice:1
enter no of elements to insert
enter the elements7
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY 3.PREORDER TRAVE RSE
4.EXIT
Enter your choice:2
2 5 11 21 24 36 44
1.INSERT
2.DISPLAY 3.PREORDER TRAVE RSE
4.EXIT
Enter your choice:3
5 2 24 11 21 36 44
1.INSERT
2.DISPLAY 3.PREORDER TRAVE RSE

09L61A0 25 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

4.EXIT
Enter your choice:4

b) Inorder

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *left;
class node *right;
int data;
};
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
node *temp,*temp1;
if(root== NULL)
{
root=new node;
root->data=ch;
root->left=NULL;
root->right=NULL;
return;
}
temp1=new node;
temp1->data=ch;

09L61A0 26 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

temp1->right=temp1->left=NULL;
temp=search(root,ch);
if(temp->data>ch)
temp->left=temp1;
else
temp->right=temp1;
}
node *search(node *temp,int ch)
{
if(root== NULL)
{
cout <<"no node present";
return NULL;
}
if(temp->left==NULL && temp->right== NULL)
return temp;
if(temp->data>ch)
{ if(temp->left==NULL) return temp;
search(temp->left,ch);}
else
{ if(temp->right==NULL) return temp;
search(temp->right,ch);
} }
void display(node *temp)
{
if(temp==NULL)
return ;
display(temp->left);
cout<<temp->data;
display(temp->right);
}
void inorder( node *root)
{
node *p;
p=root;
top=0;
do
{
while(p!=NULL)
{

09L61A0 27 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

stk[top]=p->data;
top++;
p=p->left;
}
if(top>0)
{
p=pop(root);
cout << p->data;
p=p->right;
}
}while(top!=0 || p!=NULL);
}
node * pop(node *p)
{
int ch;
ch=stk[top-1] ;
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
main()
{
tree t1;
int ch,n,i;
while(1)
{
cout <<"\n1. INSERT\n2.DISPLAY 3.INORDER
TRAVERSE\n4.EXIT\nEnter your choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter no of elements to insert:";
cin >> n;

09L61A0 28 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

for(i=1;i<=n;i++)
{ cin >> ch;
t1.insert(ch);
}
break;
case 2: t1.display(t1.root);break;
case 3: t1.inorder(t1.root); break;
case 4: exit(1);
}
}
}
OUTPUT
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to inser
5 24 36 11 44 2 21
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:3
251121243644
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:3
251121243644
1.INSERT
2.DISPLAY 3.INORDER TRAVERSE
4.EXIT
Enter your choice:4

09L61A0 29 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

Week6 :

Write C++ programs for the implementation of bfs and dfs for a given graph.

Coding:

a) BFS
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"Visitied vertices\n";
cout << v;
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)

09L61A0 30 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
OUTPUT
enterno of vertices9
ente no of edges9
EDGES
12
23
15
14
47
78
89
26
57
enter initial vertex1
Visited vertices
12 4 5 3 6 7 8 9

09L61A0 31 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

b) DFS

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"ORDER OF VISITED VERTICES";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}

09L61A0 32 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
OUTPUT
Enter no of vertices9
Enter no of edges9
EDGES
12
23
26
15
14
47
57
78
89
enter initial vertex1
ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5

09L61A0 33 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

Week-7

Write a c++ program to perform the following sorting methods:


a) Merge sort b)Heap sort

a) Merge Sort:

Coding:

#include<iostream>
#include<conio.h>
using namespace std;
void mergesort(int *,int,int);
void merge(int *,int,int,int);
int a[20],i,n,b[ 20];
main()
{
cout <<"\n enter no of elements";
cin >> n;
cout <<"enter the elements";
for(i=0;i<n;i++)
cin >> a[i];
mergesort(a,0,n-1);
cout <<" numbers after sort";
for(i=0;i<n;i++)
cout << a[i] << " ";
getch();
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);

09L61A0 34 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

mergesort(a,mid+1,j);
merge(a,i,mid,j);
}
}
void merge(int a[],int low,int mid ,int high)
{
int h,i,j,k;
h=low;
i=low;
j=mid+1;
while(h<=mid && j<=high)
{
if(a[h]<=a[ j])
b[i]=a[ h++];
else
b[i]=a[ j++];
i++;
}
if( h > mid)
for(k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=h;k<=mid;k++)
b[i++]=a[k];
cout <<"\n";
for(k=low;k<=high;k++)
{ a[k]=b[k];
cout << a[k] <<" ";
}
}
OUTPUT
N enter no of elements8 12 5 61 60 50 1 70 81
enter the elements
5 12
60 61
5 12 60 61
1 50
70 81
1 50 70 81
1 5 12 50 60 61 70 81 numbers after sort1 5 12 50 60 61 70 81

09L61A0 35 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

b)Heap sort

Coding:
#include<iostream>
#include<iomanip>
#define MAX 10
using namespace std;
template <class T>
class Heap
{
private:
T arr[MAX];
int n;
public:
Heap();
void insert(T num);
void makeheap();
void heapsort();
void display();
};
template <class T>
Heap<T>::Heap()
{
n=0;
for(int i=0;i<MAX;i++)
arr[i]=0;
}
template <class T>
void Heap<T>::insert(T num)
{
if(n<MAX)

09L61A0 36 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

{
arr[n]=num;
n++;
}
else
cout<<"\n Array is full";
}
template <class T>
void Heap<T>::makeheap()
{
for(int i=1;i<n;i++)
{
T val=arr[i];
int j=i;
int f=(j-1)/2;
while(j>0 && arr[f]<val) //creating a MAX heap//
{
arr[j]=arr[f];
j=f;
f=(j-1)/2;
}
arr[j]=val;
}
}
template <class T>
void Heap<T>::heapsort()
{
for(int i=n-1;i>0;i-1)
{
T temp=arr[i];
arr[i]=arr[0];
int k=0;
int j;
if(i==1)
j=-1;
else
j=1;

09L61A0 37 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

if(i>2 && arr[2]>arr[1])


j=2;
while(j>0 && temp<arr[j])
{
arr[k]=arr[j];
k=j;
j=2*k+1;
if(j+1<i-1 && arr[j]<arr[j+1])
j++;
if(j>i-1)
j=-1;
}
arr[k]=temp;
}
}
template <class T>
void Heap<T>::display()
{
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<"\n";
}
int main()
{
Heap <int> Iobj;
Heap <char> Cobj;
Iobj.insert(10);
Iobj.insert(25);
Iobj.insert(30);
Iobj.insert(90);
Iobj.insert(7);
Iobj.insert(21);
Iobj.insert(3);
Iobj.insert(23);
cout<<"\n The elements are….."<<endl;
Iobj.display();
Iobj.makeheap();

09L61A0 38 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

cout<<"\n HEapefied"<<endl;
Iobj.display();
Iobj.heapsort();
cout<<"\n Elememts sorted by Heap sort…"<<endl;
Iobj.display();
cout<<"\n\n\t Now sorting the character type of elements"<<endl;
Cobj.insert('F');
Cobj.insert('D');
Cobj.insert('H');
Cobj.insert('B');
Cobj.insert('G');
Cobj.insert('A');
Cobj.insert('C');
Cobj.insert('F');
cout<<"\n The elements are…."<<endl;
Cobj.display();
Cobj.makeheap();
cout<<"\n HEapefied"<<endl;
Cobj.display();
Cobj.heapsort();
cout<<"\n Elememts sorted by Heap sort…"<<endl;
Cobj.display();
return(0);
}

OUTPUT:
The elements are…..
10 25 30 90 7 21
3 23

Heapefied….
90 30 25 23 7 21
3 10

Elements sorted by Heap sort…..

09L61A0 39 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

3 7 10 21 23 25
30 90

Now sorting the character type elements


The elements are…
F D H B G A C F

Heapefied
H G F F D A C B

Elements sorted by Heap sort….


A B C D F F G H

09L61A0 40 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

Write a C++ program to perform the following operations


a) Insertion into a B-tree b) Deletion from a B-tree

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template<class T>
class bintree
{
public:
typedef struct bst
{
T data;
struct bst *left,*right;
}
node;
node *root,*New,*temp,*parent;
public:
bintree()
{
root=NULL;
}
void create();
void display();
void insert(node *,node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
void push(node *item,int *top,node *s[10]);
int sempty(int);
void pop(int *top,node *s[10],node **current);
};
template<class T>
void bintree<T>::insert(node *root,node *New)

09L61A0 41 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

{
if(new->data<(root)->data)
{
if((root->left)==NULL)
root->left=New;
else
insert(root->left,New);
}
if(New->data>root->data)
{
if(root->right==NULL)
root->right=New;
else
insert(root->right,New);
}
}
template<class T>
void bintree::create()
{
New=new node;
New->left=NULL;
New->right=NULL;
cout<<"\n enter the element";
cin>>New->data;
if(root==NULL)
root=New;
else
insert(root,New);
}
template<class T>
void bintree<T>::display()
{
if(root==NULL)
cout<<"tree is not created";
else
{
cout<<"\n the inorder traversal of tree is:";
inorder(root);
cout<<"\n the preorder traversal of tree is:";
preorder(root);

09L61A0 42 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

cout<<"\n the postorder traversal of tree is:";


postorder(root);
}
}
template<class T>
void bintree<T>::push(node *item,int *top,node *s[])
*top=*top+1;
s[*top]=item;
}
template<class T>
void bintree<T>::pop(int *top,node *s[],node **current)
{
*current=s[(*top)--];
}
template<class T>
int bintree<T>::sempty(int top)
{
if(top==-1)
return 1;
else
return 0;
}
template<class T>
void bintree<T>::inorder(node *root)
{
node *current,*s[10];
int top=-1;
if(root==NULL)
{
cout<<"\n tree is empty"<<endl;
return;
}
current=root;
for(;;)
{
while(current!=NULL)
{
push(current,&top,s);
current=current->left;
}

09L61A0 43 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

if(!stempty(top))
{
pop(&top,s,&current);
cout<<" "<<current->data;
current=current->right;
}
else
return;
}
}
template<class T>
void bintree<T>::preorder(node *root)
{
node *current,*s[10];
int top=-1;
if(root==NULL)
{
cout<<"\n the tree is empty"<<endl;
return;
}
current=root;
for(;;)
{
while(current!=NULL)
{
cout<<" "<<current->data;
push(current,&top,s);
current=current->left;
}
if(sempty(top))
{
pop(&top,s,&current);
current=current->right;
}
else
return;
}
}
template<class T>
void bintree<T>::postorder(node *root)

09L61A0 44 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

{
struct stack
{
node *element;
int check;
}
st[10];
int top=-1;
node *current;
if(root==NULL)
{
cout<<"\n the tree is empty"<<endl;
return;
}
current=root;
for(;;)
{
while(current!=NULL)
{
top++;
st[top].element=current;
st[top].check=1;
current=current->left;
}
while(st[top].check==0)
{
current=st[top].element;
top--;
cout<<" "<<current->data;
if(stempty(top))
return;
}
current=st[top].element;
current=current->right;
st[top].check=0;
}
}
void main()
{
int choice;

09L61A0 45 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

char ans='N';
bintree<int>tr;
cout<<"\n \t program for binary search tree";
while(1)
{
cout<<"\n 1.create \n 2.display \n 3.exit";
cout<<"\n \n enter your choice:";
cin>>choice;
switch(choice)
{
case 1:do
{
tr.create();
cout<<"do u want to enter more elements?(y/n)"<<endl;
ans=getche();
}
while(ans=='y');
break;
case 2:tr.display();
break;
case 3:exit(0);
}
}
return;
}

09L61A0 46 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

Write a c++ program to perform the following operations


a) Insertion into an AVL-tree b) Deletion from an AVL-tree

Coding:
#include<iostream>
#include<stdlib.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define NULL 0
class AVL;
class AVLNODE
{
friend class AVL;
private:
int data;
AVLNODE *left,*right;
int bf;
};
class AVL
{
private:
AVLNODE *root;
public:
AVLNODE *loc,*par;
AVL()
{
root=NULL;
}
int insert(int);
void displayitem();
void display(AVLNODE *);
void removeitem(int);
void remove1(AVLNODE *,AVLNODE *,int);
void remove2(AVLNODE *,AVLNODE *,int);
void search(int x);
void search1(AVLNODE *,int);

09L61A0 47 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

};
int AVL::insert(int x)
{
AVLNODE *a,*b,*c,*f, *p,*q,*y,*clchild,*crchild;
int found,unbalanced;
int d;
if(!root) //special case empty tree
{ y=new AVLNODE;
y->data=x ;
root=y;
root->bf=0;
root->left=root->right=NULL;
return TRUE; }
//phase 1:locate insertion point for x.a keeps track of the most
// recent node with balance factor +/-1,and f is the parent of a
// q follows p through the tree.
f=NULL;
a=p=root;
q=NULL;
found=FALSE;
while(p&&!found)
{ //search for insertion point for x
if(p->bf)
{
a=p;
f=q;
}
if(x<p->data) //take left branch
{
q=p;
p=p->left;
}
else if(x>p->data)
{
q=p;
p=p->right;
}
else
{
y=p;
found=TRUE;
}
} //end while
//phase 2:insert and rebalance.x is not in the tree and

09L61A0 48 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

// may be inserted as the appropriate child of q.


if(!found)
{
y = new AVLNODE;
y->data=x ;
y->left=y->right=NULL;
y->bf=0;
if(x<q->data) //insert as left child
q->left=y;
else
q->right=y; //insert as right child
//adjust balance factors of nodes on path from a to q
//note that b y the definition of a,all nodes on this
//path must have balance factors of 0 and so will change
//to +/- d=+1 implies that x is inserted in the left
// subtree of a d=-1 implies
//to that x inserted in the right subtree of a.
if(x>a->data)
{
p=a->right;
b=p;
d=-1;
}
else
{
p=a->left;
b=p;
d=1;
}
while(p!=y)
if(x>p->data) //height of right increases by 1
{
p->bf=-1;
p=p->right;
}
else //height of left increases by 1
{
p->bf=1;
p=p->left;
}
//is tree unbalanced
unbalanced=TRUE;
if(!(a->bf)||!(a->bf+d))
{ //tree still balanced

09L61A0 49 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

a->bf+=d;
unbalanced=FALSE;
}
if(unbalanced) //tree unbalanced,determine rotation type
{
if(d==1)
{ //left imbalance
if(b->bf==1 ) //rotation type LL
{
a->left=b->right;
b->right=a;
a->bf=0;
b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->left=c->right;
c->left=b;
c->right=a;
switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
} //end of left imbalance
else //right imbalance
{
if(b->bf==-1) //rotation type RR
{
a->right=b->left;
b->left=a;
a->bf=0;

09L61A0 50 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

b->bf=0;
}
else //rotation type LR
{
c=b->right;
b->right=c->left;
a->right=c->left;
c->right=b;
c->left=a;
switch(c->bf)
{
case 1: a->bf=-1; //LR(b)
b->bf=0;
break;
case -1:b->bf=1; //LR(c)
a->bf=0;
break;
case 0: b->bf=0; //LR(a)
a->bf=0;
break;
}
c->bf=0;
b=c; //b is the new root
} //end of LR
}
//subtree with root b has been rebalanced and is the new subtree
if(!f)
root=b;
else if(a==f->left)
f->left=b;
else if(a==f->right)
f->right=b;
} //end of if unbalanced
return TRUE;
} //end of if(!found)
return FALSE;
} //end of AVL INSERTION
void AVL::displayitem()
{
display(root);
}
void AVL::display(AVLNODE *temp)
{
if(temp==NULL)

09L61A0 51 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

return;
cout<<temp->data<<" ";
display(temp->left);
display(temp->right);
}
void AVL::removeitem(int x)
{
search(x);
if(loc==NULL)
{
cout<<"\nitem is not in tree";
return;
}
if(loc->right!=NULL&&loc->left!=NULL)
remove1(loc,par,x);
else
remove2(loc,par,x);
}
void AVL::remove1(AVLNODE *l,AVLNODE *p,int x)
{
AVLNODE *ptr,*save,*suc,*psuc;
ptr=l->right;
save=l;
while(ptr->left!=NULL)
{
save=ptr;
ptr=ptr->left;
}
suc=ptr;
psuc=save;
remove2(suc,psuc,x);
if(p!=NULL)
if(l==p->left)
p->left=suc;
else
p->right=suc;
else
root=l;
suc->left=l->left;
suc->right=l->right;
return;
}
void AVL::remove2(AVLNODE *s,AVLNODE *p,int x)
{

09L61A0 52 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

AVLNODE *child;
if(s->left==NULL && s->right==NULL)
child=NULL;
else if(s->left!=NULL)
child=s->left;
else
child=s->right;
if(p!=NULL)
if(s==p->left)
p->left=child;
else
p->right=child;
else
root=child;
}
void AVL::search(int x)
{
search1(root,x);
}
void AVL::search1(AVLNODE *temp,int x)
{
AVLNODE *ptr,*save;
int flag;
if(temp==NULL)
{
cout<<"\nthe tree is empty";
return;
}
if(temp->data==x )
{
cout<<"\nthe item is root and is found";
par=NULL;
loc=temp;
par->left=NULL;
par->right=NULL;
return; }
if( x < temp->data)
{
ptr=temp->left;
save=temp;
}
else
{
ptr=temp->right;

09L61A0 53 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

save=temp;
}
while(ptr!=NULL)
{
if(x==ptr->data)
{ flag=1;
cout<<"\nitemfound";
loc=ptr;
par=save;
}
if(x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(flag!=1)
{
cout<<"item is not there in tree";
loc=NULL;
par=NULL;
cout<<loc;
cout<<par;
}
}
main()
{
AVL a;
int x,y,c;
char ch;
do
{
cout<<"\n1.insert";
cout<<"\n2.display";
cout<<"\n3.delete";
cout<<"\n4.search";
cout<<"\n5.exit";
cout<<"\nEnter u r choice to perform on AVL tree";
cin>>c;
switch(c)
{
case 1:cout<<"\nEnter an element to insert into tree";
cin>>x;
a.insert(x);
break;

09L61A0 54 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

case 2:a.displayitem(); break;


case 3:cout<<"\nEnter an item to deletion";
cin>>y;
a.removeitem(y);
break;
case 4:cout<<"\nEnter an element to search";
cin>>c;
a.search(c);
break;
case 5:exit(0); break;
default :cout<<"\nInvalid option try again";
}
cout<<"\ndo u want to continue";
cin>>ch;
}
while(ch=='y'||ch=='Y');
}

09L61A0 55 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

Week-10

Write a C++ program to implement all the functions of a dictionary


(ADT) using
hashing
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
# define max 10
typedef struct list
{
int data;
struct list *next;
}node_type;
node_type *ptr[max],*root[max],*temp[max];
class Dictionary
{
public:
int index ;
Dictionar y();
void insert(int);
void search(int);
void delete_ele(int);
};
Dictionar y::Dictionary()
{
index=-1;
for(int i=0;i<max;i++)
{
root[i]=NULL;
ptr[i]=NULL;
temp[i]=NULL;

09L61A0 56 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

}
}
void Dictionary::insert(int key)
{
index=int(key%max);
ptr[index]=(node_type*)malloc(sizeof(node_type));
ptr[index]->data=key;
if(root[index]==NULL)
{
root[index]=ptr[index];
root[index]->next=NULL;
temp[index]=ptr[index] ;
}
else
{
temp[index]=root[index] ;
while(temp[index]->nex t!=NULL)
temp[index]=temp[ index]->next;
temp[index]->next=ptr[index];
}
}
void Dictionary::search(int key)
{
int flag=0;
index=int(key%max);
temp[index]=root[index] ;
while(temp[index]!=NULL)
{
if(temp[index]->data==key)
{
cout<<"\nSearch key is found!!";
flag=1;
break;
}
else temp[index]=temp[index]->next;
}
if (flag==0)
cout<<"\nsearch key not found.......";
}
void Dictionary::delete_ele(int key)

09L61A0 57 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

{
index=int(key%max);
temp[index]=root[index] ;
while(temp[index]->data!=key && temp[index]!=NULL)
{
ptr[index]=temp[index] ;
temp[index]=temp[ index]->next;
}
ptr[index]->next=temp[index]->next;
cout<<"\n"<<temp[index ]->data<<" has been deleted.";
temp[index]->data=-1;
temp[index]=NULL;
free(temp[index]);
}
main()
{
int val,ch,n,num;
char c;
Dictionar y d;
do
{
cout<<"\nMENU:\n1.Create";
cout<<"\n2.Search for a value\n3.Delete an value";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nEnter the number of elements to be inserted:";
cin>>n;
cout<<"\nEnter the elements to be inserted:";
for(int i=0;i<n;i++)
{
cin>>num;
d.insert(num);
}
break;
case 2:cout<<"\nEnter the element to be searched:";
cin>>n;
d.search(n);
case 3:cout<<"\nEnter the element to be deleted:";

09L61A0 58 DEPARTMENT OF CSE,CEC


ADVANCED DATA STRUCTURES LAB

cin>>n;
d.delete_ele(n);
break;
default:cout<<"\nInvalid choice....";
}
cout<<"\nEnter y to continue......";
cin>>c;
}while(c=='y');
getch();
}

OUTPUT
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:1
Enter the num ber of elements to be inserted:8
Enter the elements to be inserted:10 4 5 8 7 12 6 1
Enter y to continue......y
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:2
Enter the element to be searched:12
Search key is found!!
Enter the element to be deleted:1
1 has been deleted.
Enter y to continue......y

09L61A0 59 DEPARTMENT OF CSE,CEC

You might also like