You are on page 1of 82

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

ADVANCED DATA STRUCTURES LAB


MANUAL

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 1
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

INDEX

S.NO NAME OF THE EXERCISE PAGE


Stack ADT using arrays 2
Queue ADT using arrays 5
Stack ADT using linked lists 9
Queue ADT using linked lists 13
Deque using stack 18
Deque using doubly linked list 25
Binary Search Tree- Operations 30
Tree Traversals 35
Graph-DFS 40
Graph-BFS 43
Merge sort 47
Heap Sort 51
B-Tree- Insertion & Deletion 54
AVL Tree- Insertion & Deletion 58
Kruskal’s Algorithm 70
Prim’s Algorithm 74
Dictionary ADT using hashing 78

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 2
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

1) Write a C++ program to implement stack ADT using arrays.

AIM:
To write a C++ program to implement stack ADT using arrays.

ALGORITHM:

1. Push ( )

1. start
2. read n
3. if top greater than (n-1) then
4. print “overflow”
5. else
6. toptop+1
7. data[top]=d
8. print “element pushed to stack”
9. stop.

2. Pop( )

1. start
2. if top< 0 then
3. print “underflow”
4. else
5. ddata[top]
6. toptop-1
7. print “element is popped from the stack”
8. stop.

PROGRAM:

#include <iostream.h>
#include <conio.h>
#define size 15
template <class T>
class stack
{
T top,stk[size];
public:
stack();
void push(T n);
T pop();
};
template<class T> stack<T>::stack()
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 3
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

top=0;
}
template<class T> void stack<T>::push(T n)
{
if (top==size)
{cout<<"\n stack is full";
}
stk[top]=n;
top++;
}
template<class T> stack<T>::pop()
{
if(top==NULL)
{
cout<<"\n stack is empty\n";
}
top--;
return(stk[top]);
}
void main()
{
stack <int> ob;
int i=0;
clrscr();
cout<<"elements of stack"<<endl;
for(i=0;i<size;i++)
{
ob.push(i);
cout<<"\n pushed element "<<i;
}
cout<<endl;
cout<<"elements out of stack"<<endl;
for(i=0;i<5;i++)
{cout<<"\npopped out element is "<<ob.pop();
}
getch();
}

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 4
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

OUTPUT:

elements of stack

pushed element 0
pushed element 1
pushed element 2
pushed element 3
pushed element 4
pushed element 5
pushed element 6
pushed element 7
pushed element 8
pushed element 9
pushed element 10
pushed element 11
pushed element 12
pushed element 13
pushed element 14
elements out of stack

popped out element is 14


popped out element is 13
popped out element is 12
popped out element is 11
popped out element is 10

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 5
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

2) Write a C++ program to implement queue ADT using arrays.

AIM:
To write a C++ program to implement queue ADT using arrays.

ALGORITHM:

1. Insertion( )

1. start
2. read n
3. if rear== (n-1) then
4. print “overflow”
5. print “enter a number”
6. read num
7. if front == -1 then
8. front=rear=0
9. else
10. rear=rear+1
11. a[rear]=num
12. print”number is inserted”
13. stop

2. Deletion( )

1. start
2. if front == -1 then
3. print “underflow”
4. print “deleted element is”
5. print a[front]
6. if front==rear then
7. front=rear=-1
8. else
9. front=front+1
10. print “number is deleted”
11. stop.

PROGRAM:

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
const int size=10;
int rear=-1,front=0;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 6
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

template<class T>
class queue
{
T qa[size];
T temp;
public:
int empty(void);
int full(void);
void insert(T x);
T delet(void);
void disp();
};
template<class T>
int queue<T>::empty(void)
{
if(front>rear)
return(0);
else
return 1;
}
template<class T>
int queue<T>::full(void)
{
if(rear>size-1)
return 0;
else
return 1;
}
template<class T>
void queue<T>::insert(T x)
{
qa[++rear]=x;
cout<<endl;
}
template<class T>
T queue<T>::delet()
{
return (qa[front++]);
}
template<class T>
void queue<T>::disp()
{
if(front>rear)
cout<<"queue is empty"<<endl;
else
{for(int i=rear;i>=front;i--)

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 7
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<qa[i];
}
}
void main()
{
int p;
int v;
queue<int> q;
clrscr();
q.empty();
q.full();
q.insert(5);
q.delet();
q.disp();
getch();
}

OUTPUT:

*************MENU************
1.Insert
2.Delete
3.Display
4.Quit
Enter Ur Choice
1
Enter size
3
Enter elements
12
13
14

*************MENU************
1.Insert
2.Delete
3.Display
4.Quit
Enter Ur Choice
2
12 13 14
*************MENU************
1.Insert
2.Delete
3.Display

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 8
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

4.Quit
Enter Ur Choice
3

deleted element is
12
*************MENU************
1.Insert
2.Delete
3.Display
4.Quit
Enter Ur Choice
3

deleted element is
13

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 9
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

3) Write a C++ Program to implement a Stack ADT using a singly


linked list

AIM:
To write a C++ program to implement a Stack ADT using a singly
linked list.

ALGORITHM:

1. Push (d)

1. start
2. *n is a new node
3. read num
4. nd = num
5. n  next = top
6. top=n
7. print num
8. print “is pushed to stack”
9. stop.

2. Pop ( )

1. start
2. if top == NULL then
3. print “underflow”
4. num= topd
5. *d is the new node
6. d=top
7. top=topnext
8. deleted
9. stop.

PROGRAM:

#include <iostream.h>
#include <conio.h>
#include <process.h>
class stak
{
struct stks
{ int data;
stks *next;
};
stks *p,*temp,*top;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 10
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

public:
stak()
{ top = NULL; }
push();
pop();
disp();
};
stak::push()
{ clrscr();
temp= new stks;
cout<<"enter a value \n";
cin>>temp->data;
if(top==NULL)
{
top = temp;
temp->next=NULL;
}
else
{ temp->next=top;
top=temp;
}
cout<<"the element inserted = "<<temp->data<<endl;
getch();
return 0;
}
stak::pop()
{
clrscr();
p=top;
if(top==NULL)
cout<<"stack is empty\n";
else
{
top = top->next;
cout<<"element iremoved = "<<p->data;
delete p;
} return 0;}
stak::disp()
{ if(top==NULL)
cout<<"stack is empty\n";
else
{
p=top;
cout<<"stack contains \n";
while(p!=NULL)
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 11
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<p->data<<" ";
p=p->next;
} }return 0; }
void main()
{
stak s1;
int ch,l;
clrscr();
cout<< "stack operations using Linked list\n";
while(l)
{
cout<<"\n 1. PUSH\n";
cout<<"2.POP\n";
cout<<"3.DISPLAY\n";
cout<<"4.quit\n";
cout<<"enter Ur choice\n";
cin>>ch;
switch(ch)
{
case 1: s1.push();
break;
case 2: s1.pop();
break;
case 3: s1.disp();
break;
default: exit(0);
}}}

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 12
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

OUTPUT:

stack operations using Linked list

1. PUSH
2.POP
3.DISPLAY
4.quit
enter Ur choice
1
enter a value
12
the element inserted = 12

1. PUSH
2.POP
3.DISPLAY
4.quit
enter Ur choice
1
enter a value
13
the element inserted = 13

1. PUSH
2.POP
3.DISPLAY
4.quit
enter Ur choice
3
stack contains
13 12
1. PUSH
2.POP
3.DISPLAY
4.quit
enter Ur choice
2
element iremoved = 13
1. PUSH
2.POP
3.DISPLAY
4.quit
enter Ur choice

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 13
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

2
4) Write a C++ program to implement a queue ADT using a singly
linked list.

AIM:
To write a C++ program to implement a queue ADT using a singly
linked list.

ALGORITHM:

1. Insertion ( )

1. start
2. *link is a new node
3. read num
4. *new is a new node
5. ndata=num //storing the data
6. nlink = NULL //storing the address
7. If front == NULL then
1.7.1 Front = rear=n
1.7.2 Print “number is inserted”
1.7.3 Return
8. rearlink =n
9. rear=n
10. print “number is inserted”

2. Deletion ( )

1.start
2. *d is a new node
3. if front == NULL then
1.3.1 print “underflow”
1.3.2 return
4. if front is equal to rear then
1.4.1 front = rear = NULL
1.4.2 print “ no. is deleted”
1.4.3 return
5. d=front
6. front=frontlink
7. print ddata
8. print “is deleted”
9. deleted
10. stop.

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 14
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

PROGRAM:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

class rque
{
struct node
{
int data;
node *link;
}*fr,*re;
public:
rque();
void addq(int item);
int delq();
void disp();
// ~rque();
};
rque::rque()
{
fr=re=NULL;
}
void rque::addq(int item)
{
node *temp;
temp=new node;
if(temp==NULL)
cout<<"Queue is full\n";
temp->data=item;
temp->link=NULL;
if(fr==NULL)
{
re=fr=temp;
return;
}
re->link=temp;
re=re->link;
}
int rque::delq()
{
if(fr==NULL)
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 15
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<"\nQueue is empty\n";
return NULL;
}
else
{node *temp;
int item;
item=fr->data;
temp=fr;
fr=fr->link;
delete temp;
return item;}
}
/* rque::~rque()
{
if(fr==NULL)
return;
node *temp;
while(fr!=NULL)
{
temp=fr;
fr=fr->link;
delete temp;
}
} */
void rque::disp()
{
while(fr!=NULL)
{cout<<fr->data<<"\n";
fr=fr->link;
} }
void main()
{
rque rq;
int i,size,a[10],ch=1;
clrscr();
while(ch!=5)
{
cout<<"\n *************MENU***********\n";
cout<<" 1.Insert\n";
cout<<" 2.Display\n";
cout<<" 3.Delete\n";
cout<<" 4.Quit\n";
cout<<"Enter Ur Choice\n";
cin>>ch;
switch(ch)
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 16
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

case 1:
{
cout<<"\n enter the size of the queue\n";
cin>>size;
cout<<"Enter elements\n";
for(i=0;i<size;i++)
{ cin>>a[i];
rq.addq(a[i]);
} break;
}
case 2:
{
cout<<"Display list\n";
rq.disp();break;
}
case 3:
{
cout<<"Delete process\n";
int j=rq.delq();
cout<<"Extracted element is "<<j;
break;
}
case 4: {
exit(1);
break; }
}}
getch();
}

OUTPUT:

*************MENU***********
1.Insert
2.Display
3.Delete
4.Quit
Enter Ur Choice
1

enter the size of the queue


4
Enter elements
12
13
14

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 17
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

15

*************MENU***********
1.Insert
2.Display
3.Delete
4.Quit
Enter Ur Choice
2
Display list
12
13
14
15

*************MENU***********
1.Insert
2.Display
3.Delete
4.Quit
Enter Ur Choice
3
Delete process

Queue is empty
Extracted element is 0
*************MENU***********
1.Insert
2.Display
3.Delete
4.Quit
Enter Ur Choice

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 18
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

5) Write a C++ program to implement the deque( doubly ended


queue ) ADT using a stack.

AIM:
To write a c++ program to implement deque ADT using a stack.

ALGORITHM:

1.Insertion_Front()

1. start
2. if front == 0 & rear==-1
a. d[front]==x
b. rear++
3. if front == 0 & rear != -1
a. print “insertion not possible”
4. else
a. f--
b. dq[f]=x
5. print “ no. is inserted”

2.Deletion_Front()

1. start
2. if rear = = -1
3. print “queue is empty”
4. else
5. if(front = = rear)
a. rear=-1;
b. front=0;
6. else
7. front++;
8. print “ deleted”

3.Insertion_Rear()

1. start
2. if(r==n-1)
3. print “Queue Is Full”
4. else
a. r++;
b. dq[r]=x;
5. print “inserted”

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 19
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

4.Deletion_Rear ()

1. start
2. if rear == -1
3. print “Queue is empty”
4. else
5. if front == rear
a. front =0
b. rear=-1
6. print “deleted”

PROGRAM:

// Program De queue
#include<iostream.h>
#include<conio.h>
#define n 3
template<class t>
class dqueue
{
private:
t dq[n],x;
int i,j,f,r;
public:
dqueue()
{
f=0;
r=-1;
}
void insert_rear();
void insert_front();
void del_rear();
void del_front();
void show();
};
template<class t>
void dqueue<t>::insert_rear()
{
if(r==n-1)
cout<<"\t\t\tQUEUE IS FULL";
else
{
char ch='y';
while(ch=='y')

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 20
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{
cout<<"ENTER THE DATA:";
cin>>x;
r++;
dq[r]=x;
cout<<"\t\tANY MORE DATA(y/n):";
cin>>ch;
}
}
}

template<class t>
void dqueue<t>::insert_front()
{
if((r==-1) && (f==0))
{
dq[f]=x;
r++;
}
else
{
if((f==0) && (r!=-1))
cout<<"\t\tNOT POSSIBLE TO INSERT";
else
{
char ch='y';
while(ch=='y')
{
if(f==0)
cout<<"\t\tNOT POSSIBLE TO INSERT";
else
{
cout<<"\t\tENTER THE DATA:";
cin>>x;
f--;
dq[f]=x;
cout<<"\t\tANY MORE DATA(y/n):";
cin>>ch;
}
}
}
}
}
template<class t>
void dqueue<t>::del_rear()
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 21
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

if(r==-1)
cout<<"\t\t\tQUEUE IS EMPTY";
else
{
if(f==r)
{
f=0;
r=-1;
show();
}
else
{
r--;
show();
}
}
}
template<class t>
void dqueue<t>::del_front()
{
if(r==-1)
cout<<"\t\t\tQUEUE IS EMPTY";
else
{
if(f==r)
{
r=-1;
f=0;
show();
}
else
{
f++;
show();
}
}
}
template<class t>
void dqueue<t>::show()
{
if(r==-1)
cout<<"\t\t\tQUEUE IS EMPTY";
else
{
cout<<"\n\t\tTHE ELEMENTS ARE:"<<"\n\n\t\t\t";
for(i=f;i<=r;i++)

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 22
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<dq[i]<<"\t";
}
}
template<class t>
void dq_op(dqueue<t> dq)
{
int choice;
do
{
cout<<"\n1.INSERT_REAR"<<"\n";
cout<<"2.INSERT_FRONT"<<"\n";
cout<<"3.DELETE_REAR"<<"\n";
cout<<"4.DELETE_FRONT"<<"\n";
cout<<"5.SHOW"<<"\n";
cout<<"6.EXIT"<<"\n";
cout<<"ENTER THE CHOICE:";
cin>>choice;
switch(choice)
{
case 1:dq.insert_rear();break;
case 2:dq.insert_front();break;
case 3:dq.del_rear();break;
case 4:dq.del_front();break;
case 5:dq.show();break;
case 6:break;
}
}while(choice!=6);
}

main()
{
clrscr();
int ch;
do
{
cout<<"\n1.INT"<<"\n";
cout<<"2.CHAR"<<"\n";
cout<<"3.EXIT"<<"\n";
cout<<"ENTER THE CHOICE:";
cin>>ch;
switch(ch)
{
case 1: dqueue<int> i;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 23
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

int x1;
dq_op(i);
break;
case 2: dqueue<char> c;
char x2;
dq_op(c);
break;
case 3:break;
}
}while(ch!=3);
}

OUTPUT:

1.INT
2.CHAR
3.EXIT
ENTER THE CHOICE:1

1.INSERT_REAR
2.INSERT_FRONT
3.DELETE_REAR
4.DELETE_FRONT
5.SHOW
6.EXIT
ENTER THE CHOICE:1
ENTER THE DATA:12
ANY MORE DATA(y/n):y
ENTER THE DATA:13
ANY MORE DATA(y/n):n

1.INSERT_REAR
2.INSERT_FRONT
3.DELETE_REAR
4.DELETE_FRONT
5.SHOW
6.EXIT
ENTER THE CHOICE:5

THE ELEMENTS ARE:

12 13
1.INSERT_REAR
2.INSERT_FRONT
3.DELETE_REAR

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 24
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

4.DELETE_FRONT
5.SHOW
6.EXIT
ENTER THE CHOICE:3

THE ELEMENTS ARE:

12
1.INSERT_REAR
2.INSERT_FRONT
3.DELETE_REAR
4.DELETE_FRONT
5.SHOW
6.EXIT
ENTER THE CHOICE:

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 25
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

6) Write a C++ program to implement the deque ( doubly ended


queue ) ADT using a doubly linked list

AIM:
To write a C++ program to implement the deque ADT using a
doubly linked list.

ALGORITHM:

I. Insertion ( ):

1. start
2. * link is a new node
3. read num
4. * n is a new node
5. n  data = num
6. n  link = NULL
7. if front == NULL then
7.1 front = rear = n
7.2 print “no is inserted
7.3 return
8. rear  link = n
9. rear = n
10. print “no is inserted”

II. Deletion ( ):

1. start
2. * d is a new node
3. if front is equal to NULL then
3.1 print “underflow”
3.2 return
4. front is equal to rear then
4.1 front = rear = NULL
4.2 print “no is deleted
4.3 return
5. d = front
6. front = front  link
7. print d  data
8. print “ is deleted”
9. delete d
10. stop

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 26
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

PROGRAM:

#include <iostream.h>
#include <conio.h>
template <class T>
class node
{ node<T> *prev;
T data;
node<T> *next;
public:
friend class Doub<T>;
};
template<class T>
class Doub
{
node<T> *fr;
public:
Doub(){fr = 0;}
~Doub();
void create();
void insert(int , T);
void Delete(int, T &);
void disp();
};
template<class T> Doub<T>::~Doub()
{
node<T> *p=fr;
node<T> *q;
while(p)
{
q=p;
p=p->next;
delete q;
cout<<"object destsroyed"<<endl;
} }
template<class T> void Doub<T>:: create()
{
T data;
char ch;
do
{cout<<"Enter the data element";
cin>>data;
if (fr==0)
{
fr=new node<T>;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 27
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

fr->prev=0;
fr->data=data;
fr->next=0;
}
else
{
node<T> *q=fr,*r;
while(q->next!=0)
{
q=q-> next;
}
r=new node<T>;
r->data=data;
r->next=0;
r->prev=q;
q->next=r;
}
cout<<"Do you want to continue(Y/N)";
cin>>ch;
}while(ch=='Y'||ch=='y');
}
template<class T> void Doub<T>::insert(int pos,T data)
{
node<T> *q,*temp;
if(pos==0)
{
temp=new node<T>;
temp->prev=0;
temp->data=data;
temp->next=fr;
fr=temp;
}
else
{
node<T> *p=fr;
for(int i=0;i<pos&&p;i++)
p=p->next;
if(p)
{
temp=new node<T>;
temp->data=data;
p->prev->next=temp;
temp->next=p;
p->prev=temp;
}
else

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 28
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<"Invalid position entered";


}
}
template<class T> void Doub<T>::Delete(int pos,T &data)
{
node<T> *p=fr;
if (pos==0)
{
fr=fr->next;
fr->prev=0;
data=p->data;
delete p;
}
else
{
for(int k=0; k<pos&&p;k++)
p=p->next;
data=p->data;
if(p->next==NULL)
p->prev->next=NULL;
else
{
p->next->prev=p->prev;
p->prev->next=p->next;
}
delete p;
}
}
template <class T> void Doub<T>:: disp()
{
node<T> *curr=fr;
cout<<endl;
while (curr)
{
cout<<curr->data<<"->";
curr=curr->next;
}}
void main()
{
Doub<int> obj;
int pos,data;
clrscr();
cout<<"create method is invocked"<<endl;
obj.create();
cout <<"Display"<<endl;
obj.disp();

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 29
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<"Enter the position & data";


cin>>pos>>data;
obj.insert(pos,data);
cout<<"Display"<<endl;
obj.disp();
cout<<"enter element position to be deleted \n";
cin>>pos>>data;
obj.Delete(pos,data);
cout<<"Display after Delete";
obj.disp();
getch();
}

OUTPUT:

create method is invoked

Enter the data element2


Do you want to continue(Y/N)y
Enter the data element3
Do you want to continue(Y/N)y
Enter the data element4
Do you want to continue(Y/N)y
Enter the data element5
Do you want to continue(Y/N)n
Display

2->3->4->5->Enter the position & data3


67
Display

2->3->4->67->5->enter element position to be deleted


5

5
Display after Delete
2->3->4->67->5->object destroyed

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 30
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

7) Write a C++ program to perform the following operations.


a. Insert an element into binary search tree
b. Delete an element from binary search tree
c. Search fr a key element in binary search tree.

AIM: To write a c++ program to implement a Binary search tree.

ALGORITHM:

inorder ( node * r) :
1. start
2. if ptr ! = NULL then
3. in order (ptr  LC)
4. print data (ptr)
5. in order (ptr  RC)
6. stop

PROGRAM:

#include <iostream.h>
#include <conio.h>
#define TRUE 1
#define FALSE 0
class btree
{
struct btnode
{
btnode *left;
int data;
btnode *right;
}*root;
public:
btree();
void create(int num);
static void insert(btnode **sr,int);
static void search(btnode **sr,int num,btnode **par,btnode **x,int
*found);
void remove(int num);
static void rem(btnode **sr,int num);
void disp();
static void inorder(btnode *sr);
~btree();
static void del(btnode *sr);
};
btree::btree()

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 31
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{ root=NULL; }
void btree::create(int num)
{
insert(&root,num);
}
void btree::insert(btnode **sr,int num)
{
if(*sr==NULL)
{ *sr=new btnode;
(*sr)->left=NULL;
(*sr)->data=num;
(*sr)->right=NULL;
}
else
{
if(num<(*sr)->data)
insert(&((*sr)->left),num);
else
insert(&((*sr)->right),num);
}}
void btree::remove(int num)
{
rem(&root,num);
}
void btree::rem(btnode **sr,int num)
{
int found;
btnode *parent,*x,*xsucc;
if(*sr==NULL)
{
cout<<"\n Tree is empty";
return;
}
parent=x=NULL;
search(sr,num,&parent,&x,&found);
if(found==FALSE)
{
cout<<"\n data to be deleted, not found";
return;
}
if(x->left!=NULL&&x->right!=NULL)
{
parent=x;
xsucc=x->right;
while(xsucc->left!=NULL)
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 32
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

parent=xsucc;
xsucc=xsucc->left;
}
x->data=xsucc->data;
x=xsucc;
}
if(x->left==NULL&&x->right==NULL)
{ if(parent->right==x)
parent->right=NULL;
else
parent->left=NULL;
delete x;
return;
}
if(x->left==NULL&&x->right!=NULL)
{
if(parent->left==x)
parent->left=x->right;
else
parent->right=x->right;
delete x;
return;
}
if(x->left!=NULL&&x->right==NULL)
{
if(parent->left==x)
parent->left=x->left;
else
parent->right=x->left;
delete x;
return;
}}
void btree::search(btnode **sr,int num,btnode **par,btnode **x,int
*found)
{
btnode *q;
q=*sr;
*found=FALSE;
*par=NULL;
while(q!=NULL)
{
if(q->data==num)
{
*found=TRUE;
*x=q;
return;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 33
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

}
*par=q;
if(q->data>num)
q=q->left;
else
q=q->right;
}}
void btree::disp()
{
inorder(root);
}
void btree::inorder(btnode *sr)
{
if(sr!=NULL)
{
inorder(sr->left);
cout<<sr->data<<"\t";
inorder(sr->right);
}}
btree::~btree()
{ del(root);
}
void btree::del(btnode *sr)
{
if(sr!=NULL)
{ del(sr->left);
del(sr->right);
}
delete sr;
}

void main()
{
btree bt;
char ch='y';
int req,i=0,size,num,a[10],elm;
clrscr();
cout<<"Enter size of the tree\n";
cin>>size;
cout<<"enter elements\n";
while(i<=size)
{ cin>>a[i];
bt.create(a[i]);
i++;
}
cout<<"\nBtree before deletion \n";

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 34
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

bt.disp();
while(ch=='y')
{
cout<<"enter deleting elem\n";
cin>>elm;
bt.remove(elm);
cout<<"\nBinary tree after delete\n";
bt.disp();
cout<<"do you want delete ?\n";
cin>>ch;
}
getch();
}

OUTPUT:

Output

Enter size of the tree


3
enter elements
34
23
12
56

Btree before deletion


12 23 34 56

enter deleting elem


34

Binary tree after delete


12 23 56
do you want continue ?
n

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 35
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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


traverse the given binary tree
a. Preorder
b. Postorder
c. Inorder

AIM:
A c++ program to create a binary tree and traverse it inorder,
preorder, and postorder.

ALGORITHM:

I. build ( ):

1. start
2. read num
3. if num is equal to zero then
3.1 root = NULL
4. root = new node
5. root  data = num
6. root  lptr = root  rptr = NULL
7. read num
8. while num ! = 0 then
8.1 n = new node
8.2 n  data = num
8.3 n  lptr = n  rptr = NULL
8.4 s = root
8.5 while s! = NULL
8.6 c = s
8.6.1 if num > s  data
8.6.2 s = s  rptr
8.6.3 else
8.6.4 s = s  lptr
8.6.5 if num > c  data
8.6.6 c rptr = n
8.6.7 else
8.6.8 c  lptr = n
8.6.9 read num
9. stop

II.Inorder ( node * r) :

1. start

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 36
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

2. if ptr ! = NULL then


3. in order (ptr  LC)
4. print data (ptr)
5. in order (ptr  RC)
6. stop

III.Preorder ( node * r) :

1. start
2. if ptr ! = NULL then
3. print data (ptr)
5. pre order (ptr  LC)
4. pre order (ptr  RC)
5. stop

IV.Postorder ( node * r) :

1. start
2. if ptr ! = NULL then
3. post order (ptr  LC)
4. post order (ptr  RC)
5. print data (ptr)
6. stop

PROGRAM

#include <iostream.h>
#include <conio.h>
class btre
{
struct bnode
{ bnode *lc;
int data;
bnode *rc;
}*root;
public:
btre();
void ctree(int num);
static void insert(bnode **sr,int num);
void traverse();
static void inorder(bnode *sr);
static void preorder(bnode *sr);
static void postorder(bnode *sr);
static void del(bnode *sr);
For more visit www.hitamtech.blogspot.com
For cool softwares and gadgets visit www.opensofty.blogspot.com Page 37
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

~btre();
};
btre::btre()
{ root=NULL;
}
void btre::ctree(int num)
{
insert(&root,num);
}
void btre::insert(bnode **sr,int num)
{
if(*sr==NULL)
{ *sr=new bnode;
(*sr)->lc=NULL;
(*sr)->data=num;
(*sr)->rc=NULL;
return;
}
else
{
if(num<(*sr)->data)
insert(&((*sr)->lc),num);
else
insert(&((*sr)->rc),num);
}
return;
}
void btre::traverse()
{ cout<<"Inordertra\n";
inorder(root);
cout<<"\npre order\n";
preorder(root);
cout<<"\npost order \n";
postorder(root);
}

void btre::inorder(bnode *sr)


{
if(sr!=NULL)
{ inorder(sr->lc);
cout<<"\t"<<sr->data;
inorder(sr->rc);
}
else
return;
}

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 38
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

void btre::preorder(bnode *sr)


{
if(sr!=NULL)
{
cout<<"\t"<<sr->data;
preorder(sr->lc);
preorder(sr->rc);
}
else
return;
}
void btre::postorder(bnode *sr)
{
if(sr!=NULL)
{
postorder(sr->lc);postorder(sr->rc);
cout<<"\t"<<sr->data;
}
else
return;
}
btre::~btre()
{
del(root);
}
void btre::del(bnode *sr)
{
if(sr!=NULL)
{ del(sr->lc);
del(sr->rc);
}
delete sr;
}

void main()
{
btre bt;
int req,i=1,num;

clrscr();
cout<<"specify the no of items to be inserted \n";
cin>>req;
while(i++<=req)
{
cout<<"enter the data \n";
cin>>num;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 39
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

bt.ctree(num);
}
bt.traverse();
getch();
}

OUTPUT:

specify the no of items to be inserted


5
enter the data
12
enter the data
23
enter the data
34
enter the data
11
enter the data
45
Inorder
11 12 23 34 45

pre order
12 11 23 34 45
post order
11 45 34 23 12

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 40
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

9) Write a C++ program for the implementation of dfs for a given


graph

AIM: A c++ program to implement the depth first search algorithm for
the given graph.

ALGORITHM:

I. DFS (int i)
1. start
2. visited [i] = 1
3. print “Node visited”
4. print i = i + 1
5. for j = 0 to max size
5.1 if ((visisted [j] ==0) && graph [i][j] ==1))
5.2 dfs(j);
6. stop

PROGRAM:

#include <iostream.h>
#include <conio.h>
#define TRUE 1
#define FALSE 0
const int MAX=8;
struct node
{
int data;
node *next;
};
class graph
{
int visit[MAX];
public:
graph();
void dfs(int v,node **p);
node *getn(int val);
void del(node *n);
};
graph::graph()
{
for(int i=0;i<MAX;i++)

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 41
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

visit[i]=FALSE;
}
void graph::dfs(int v,node **p)
{
node *t;
visit[v-1]=TRUE;
cout<<v<<"\t";
t=*(p+v-1);
while(t!=NULL)
{
if( visit[t->data-1]==FALSE)
dfs(t->data,p);
else
t=t->next;
} }
node *graph::getn(int val)
{
node *newnode=new node;
newnode->data=val;
return newnode;
}
void graph::del(node *n)
{
node *temp;
while(n!=NULL)
{
temp=n->next;
delete n;
n=temp;
} }
void main()
{
node *arr[MAX];
node *v1,*v2,*v3,*v4;
graph g;
clrscr();
v1=g.getn(2);
arr[0]=v1;
v1->next=v2=g.getn(3);
v2->next=NULL;
v1=g.getn(1);
arr[1]=v1;
v1->next=v2=g.getn(4);
v2->next=v3=g.getn(5);
v3->next=NULL;
v1=g.getn(1);

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 42
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

arr[2]=v1;
v1->next=v2=g.getn(6);
v2->next=v3=g.getn(7);
v3->next=NULL;
v1=g.getn(2);
arr[3]=v1;
v1->next=v2=g.getn(8);
v2->next=NULL;
v1=g.getn(2);
arr[4]=v1;
v1->next=v2=g.getn(8);
v2->next=NULL;
v1=g.getn(3);
arr[5]=v1;
v1->next=v2=g.getn(8);
v2->next=NULL;
v1=g.getn(3);
arr[6]=v1;
v1->next=v2=g.getn(8);
v2->next=NULL;
v1=g.getn(4);
arr[7]=v1;
v1->next=v2=g.getn(5);
v2->next=v3=g.getn(6);
v3->next=v4=g.getn(7);
v4->next=NULL;
cout<<endl;
cout<<"dfs format is \n:";
g.dfs(1,arr);
cout<<"\n array after deletion \n";
for(int i=0;i<MAX;i++)
g.del(arr[i]);}

OUTPUT:

dfs format is
:1 2 4 8 5 6 3 7

Graph after deletion

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 43
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

10) Write a C++ program for the implementation of bfs for a


given graph.

AIM:
A c++ program to implement the breadth first search algorithm for
the given graph.

ALGORITHM:
1. bfs (int) :
1. start
2. visited [i] = 1
3. addq [i]
4. while front ! = rear
4.1 i = deleteq ( )
4.2 i = i + 1
5. for j = 0 to max size
6. if visited [j] ==0 && graph [i][j] == 1 then
6.1 addq (i)
6.2 visited [j] = 1
7. j = j+1
8. stop

PROGRAM:

#include<iostream.h>
#include <conio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
const int MAX=8;
struct node
{
int data;
node*next;
};
class graph
{
private:
int visited[MAX];
int q[8];
int front, rear;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 44
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

public:
graph();
void bfs(int v, node **p);
node *getnode_write(int val);
static void addqueue(int *a, int vertex,int *f, int *r);
static int deletequeue(int *q,int *f,int *r);
static int isempty(int *f);
void del(node *n);
};
graph::graph()
{
for(int i=0;i<MAX;i++)
visited[i]=FALSE;
front=rear=-1;
}
void graph::bfs(int v, node **p)
{
node *u;
visited[v-1]=TRUE;
cout<<v<<"\t";
addqueue(q,v,&front,&rear);
while(isempty(&front)==FALSE)
{
v=deletequeue(q,&front,&rear);
u=*(p+v-1);
while(u!=NULL)
{
if(visited[u->data-1]==FALSE)
{
addqueue(q,u->data,&front,&rear);
visited[u->data-1]=TRUE;
cout<<u->data<<"\t";
}
u=u->next;
}
}
}
node *graph::getnode_write(int val)
{
node *newnode=new node;
newnode->data=val;
return newnode;
}
void graph::addqueue(int *a,int vertex, int *f, int *r)
{
if(*r==MAX-1)

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 45
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{
cout<<"\n Queue Overflow.";
exit(0);
}
(*r)++;
a[*r]=vertex;
if(*f==-1)
*f=0;
}
int graph::deletequeue(int *a,int *f, int *r)
{
int data;
if(*f==-1)
{
cout<<"\nQueue Underflow.";
exit(0);
}
data=a[*f];
if(*f==*r)
*f=*r=-1;
else
(*f)++;
return data;
}
int graph::isempty(int *f)
{
if(*f==-1)
return TRUE;
return FALSE;
}
void graph::del(node *n)
{
node *temp;
while(n!=NULL)
{
temp=n->next;
delete n;
n=temp;
}
}
void main()
{
node *arr[MAX];
node *v1,*v2,*v3,*v4;
graph g;
clrscr();

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 46
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<"Graph before deletion\n";


v1=g.getnode_write(2);
arr[0]=v1;
v1->next=v2=g.getnode_write(3);
v2->next=NULL;
v1=g.getnode_write(1);
arr[1]=v1;
v1->next=v2=g.getnode_write(4);
v2->next=v3=g.getnode_write(5);
v3->next=NULL;
v1=g.getnode_write(1);
arr[2]=v1;
v1->next=v2=g.getnode_write(6);
v2->next=v3=g.getnode_write(7);
v3->next=NULL;
v1=g.getnode_write(2);
arr[3]=v1;
v1->next=v2=g.getnode_write(8);
arr[4]=v1;
v1->next=v2=g.getnode_write(8);
v2->next=NULL;
v1=g.getnode_write(3);
arr[5]=v1;
v1->next=v2=g.getnode_write(8);
v2->next=NULL;
v1=g.getnode_write(3);
arr[6]=v1;
v1->next=v2=g.getnode_write(8);
v2->next=NULL;
v1=g.getnode_write(4);
arr[7]=v1;
v1->next=v2=g.getnode_write(5);
v2->next=v3=g.getnode_write(6);
v3->next=v4=g.getnode_write(7);
v4->next=NULL;
cout<<endl;
g.bfs(1,arr);
cout<<"\nGraph after deletion\n";
for(int i=0;i<MAX;i++)
g.del(arr[i]);
getch();
}
OUTPUT:

Graph before deletion

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 47
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

1 2 3 4 5 6 7 8
Graph after deletion
11) Write a C++ program to implement the Merge sort

AIM:
To implement a c++ program for one of the sorting technique.

ALGORITHM:

I. Merge (k, first, second, third):


1. Initialization of the variables
f = first
s = second
i=0
2. Repeat while (f<second and s <= third)
if (k[f] = k[s]) then
{
i =i+1
temp [i] = k[f]
f=f+1
}
else
{
i=i+1
temp [i] = k[s]
s=s+1
}
3. store the elements which are not processed
if (f>= second) then repeat while (s<=third)
{
i = i +1
temp [i] = k[s]
s=s+1
}
else
repeat while (f< second)
{
i=i +1
temp[i] = k[f]
f=f+1
}
4. get back the elements from the temporary elements
repeat for i = 1 to n do
k [first – 1 + i] = temp [i]
5. return

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 48
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

PROGRAM:

#include <iostream.h>
#include <conio.h>
class merges
{
public:
void msort(int,int *,int,int *,int *);
void bsort(int,int *);
};
void merges::bsort(int n,int a[])
{
int flag=1;
for(int j=0;j<n-1;j++)
{
for(int k=0;k<n-j-1;k++)
{
if(a[k]>a[k+1])
{
int temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
flag=0;
}}
if(flag)
break;
else
flag=1;
}
cout<<"\nentered list is \n";
cout<<"\nascending order\n";
for(int i=0;i<n;i++)
cout<<" "<<a[i];
}
void merges::msort(int n,int a[],int m,int b[],int c[])
{
int i=0,j=0,k=0;
cout<<"\nMERGED ARRAY IS \n";
while((i<n) && (j<m))
{
if(a[i]<b[j])
{c[k]=a[i];
i++;
k++;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 49
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

}
else
if(a[i]>b[j])
{ c[k]=b[j];
j++;
k++;
}
else
{
c[k]=a[i];
i++;j++;k++;
}
cout<<endl;
for(int ch=0;ch<k;ch++)
cout<<" "<<c[ch];
}
if(i<n)
{ for(int l=i;l<n;l++)
{
c[k]=a[i];
j++;k++;} cout<<endl;
for(int ch=0;ch<k;ch++)
cout<<" "<<c[ch];
}
else
if(j<m)
{ for(int l=j;l<m;l++)
{ c[k]=b[j];
j++;k++; }}
cout<<endl;
for(int ch=0;ch<k;ch++)
cout<<" "<<c[ch];

}
void main()
{
merges ms;
int a[20],b[20],c[40];
int n,m,k,i;
clrscr();
cout<<"enter n value\n";
cin>>n;
cout<<"\nenter elements \n";
for(i=0;i<n;i++)
cin>>a[i];
ms.bsort(n,a);

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 50
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<"\nenter m value\n";
cin>>m;
cout<<"\nenter elements\n";
for(i=0;i<m;i++)
cin>>b[i];
ms.bsort(m,b);
ms.msort(n,a,m,b,c);
getch();

OUTPUT:

enter n value
5

enter elements
1
4
23
12
56

entered list is

ascending order
1 4 12 23 56
enter m value
3

enter elements
13
12
1

entered list is

ascending order
1 12 13
MERGED ARRAY IS

1
14
1 4 12
1 4 12 13
1 4 12 13 23 23
1 4 12 13 23 23

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 51
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

12) Write a C++ program to implement the Heap sort

AIM:
A C++ program to implement Heap Sort

ALGORITHM:

I. heapSort(a, count)

1. end := count - 1
2. while end > 0 do
3. swap the root(ma value) of the heap with the last element of the
heap)
swap(a[end], a[0])
4. decrease the size of the heap by one so that the previous max value
will
stay in its proper placement)
5. end := end - 1
6. (put the heap back in max-heap order)
7. siftDown(a, 0, end)

II.heapify(a,count)

1. (start is assigned the index in a of the last parent node)


2. start := (count - 1) / 2
3. while start ≥ 0 do
4. (sift down the node at index start to the proper place such that
all nodes below the start index are in heap order)
5. siftDown(a, start, count-1)
6. start := start – 1
7. (after sifting down the root all nodes/elements are in heap
order)

III.siftDown(a, start, end)

1. root := start
2. while root * 2 + 1 ≤ end do
3. (While the root has at least one child)
4. child := root * 2 + 1 (root*2+1 points to the left child)
5. (If the child has a sibling and the child's value is less than its
sibling's...)
6. if child < end and a[child] < a[child + 1] then
7. child := child + 1 (... then point to the right child instead)
8. if a[root] < a[child] then (out of max-heap order)

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 52
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

9. swap(a[root], a[child])
10. root := child (repeat to continue sifting down the child
now)
11. else
12. return

PROGRAM:

#include <iostream.h>
#include <conio.h>
void hsort(int x[],int n)
{
int i,elt,s,f,ivalue;
for(i=1;i<n;i++)
{
elt=x[i];
s=i;
f=(s-1)/2;
while(s>0 && (x[f]<elt))
{
x[s]=x[f];
s=f;
f=(s-1)/2;
}
x[s]=elt;
}
for(i=n-1;i>0;i--)
{ivalue=x[i];
x[i]=x[0];
f=0;
if(i==1)
s=-1;
else
s=1;
if((i>2) && (x[2]>x[1]))
s=2;
while((s>=0) && (ivalue<x[s]))
{
x[f]=x[s];
f=s;
s=2*f+1;
if((s+1<=i-1) && (x[s]<x[s+1]))
s=s+1;
if(s>i-1)
s=-1;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 53
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

}
x[f]=ivalue;
}}
void main()
{
int i,size,a[10];
clrscr();
cout<<"enter the size\n";
cin>>size;
cout<<"enter elements\n";
for(i=0;i<size;i++)
cin>>a[i];
hsort(a,size);
cout<<"sorted array \n";
for(i=0;i<size;i++)
cout<<a[i]<<endl;
getch();
}

OUTPUT:

enter the size


4
enter elements
12
2
1
4
sorted array
1
2
4
12

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 54
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

13) Write a C++ program to perform the following operations


a. Insertion int a B-Tree
b. Deletion from a B-Tree

AIM:
A C++ Program to perform insertion and deletion on B-
Tree

ALGORITHM:

I.B-Tree-Insert(T, k)

1. r <- root[T]
2. if n[r] = 2t - 1
a. then s <- Allocate-Node()
b. root[T] <- s
c. leaf[s] <- FALSE
d. n[s] <- 0
e. c1 <- r
f. B-Tree-Split-Child(s, 1, r)
g. B-Tree-Insert-Nonfull(s, k)
h.else B-Tree-Insert-Nonfull(r, k)

II. B-Tree-Delete(x, k)

1. if x is a leaf then
2. if k is in x then
3. delete k from x and return true
4. else return false //k is not in subtree
5. else //x is an internal node
6. if k is in x then
7. y = the child of x that precedes k
a. if y has at least t keys then
b. k' = the predecessor of k (use B-Tree-
FindLargest)
c. Copy k' over k //i.e., replace k with k'
B-Tree-Delete(y, k') //Note: recursive call
8. else //y has t-1 keys
9. z = the child of x that follows k
10. if z has at least t keys then
11. k' = the successor of k
12. Copy k' over k //i.e., replace k with k'
13. B-Tree-Delete(z, k') //Note: recursive call
14. stop

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 55
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

PROGRAM:

#include <iostream.h>
#include <conio.h>
class btre
{
struct node
{
node *left;
char data;
node *right;
}*root;
char *a;
int *lc;int *rc;
public:
btre(char *,int *l,int *r,int size);
void insert(int index);
static node *create(char *a1,int *l,int *r,int index);
void display();
static void inorder(node *sr);
~btre();
static void del(node *sr);
};
btre::btre(char *a1,int *l,int *r,int size)
{
root=NULL;
a=new char[size];
lc=new int[size];
rc=new int[size];
for(int i=0;i<size;i++)
{ *(a1+i)=*(a1+i);
*(lc+i)=*(l+i);
*(rc+i)=*(r+i);
}}
void btre::insert(int index)
{
root=create(a,lc,rc,index);
}
node *btre::create(char *a1,int *l,int *r,int index)
{
node *temp=NULL;
if(index!=-1)
{ temp=new node;
temp->left=create(a1,l,r,*(l+index));

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 56
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

temp->data=*(a1+index);
temp->right=create(a1,l,r,*(r+index));
}
return temp;
}
void btre::display()
{
inorder(root);
}
void btre::inorder(node *sr)
{
if(sr!=NULL)
{ inorder(sr->left);
cout<<sr->data<<"\t";
inorder(sr->right);
}}
btre::~btre()
{
delete a;
delete lc;
delete rc;
del(root);
}
void btre::del(node *sr)
{
if(sr!=NULL)
{ del(sr->left);
del(sr->right);
}
delete sr;
}
void main()
{
char a1[15];
int l[15];
int r[15];
int sz;
clrscr();
cout<< "enter the size\n";
cin>>sz;
int sz1=sizeof(sz);
cout<<"enter the elements \n";
for(int i=0;i<sz1;i++)
{cin>>a1[i];}
btre bt(a1,l,r,sz);
bt.insert(0);

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 57
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<"\n in-order traversal : "<<endl;


bt.display();
getch();
}

OUTPUT:

Enter size of the tree


6
enter elements
12
56
232
45
56
67
78

Btree before deletion


12 45 56 56 67 78 232 enter deleting elem
67

Binary tree after delete


12 45 56 56 78 232 do you want delete ?
n

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 58
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

14) Write a C++ program to perform the following operations


a. Insertion into an AVL Tree b. Deletion from an
AVL Tree

AIM:
A C++ program to implement AVL Trees.

ALGORITHM:

I. INSERT:

1 Do Binary Search Tree Insert (recursive algorithm)

2 While the recursion returns, keep track of

a. node p,
b. p's child q and
c. p's grandchild r within the path from inserted node to p.

3 If p is unbalanced, do one of the following rotations:

a. if (p.left == q) and (p.left.left == r), single rotation right in p;


b. if (p.right == q) and (p.right.right == r), single rotation left in p;
c. if (p.left == q) and (p.left.right == r), LR-double rotation in p; or
d. if (p.right == q) and (p.right.left == r), RL-double rotation in p.

II. DELETE:

1. Let ptr, p be a reference to a Node.


2. ptr = find(X
3. If ptr is not null,
4. Decrement elementCount;
5. If ptr == root
6. Set root to null and return
7. If ptr is a leaf node,
8. p = ptr.parent
9. Set p's left/right child to null.
10. Else If ptr is a node with 1 child (left/right),
11. p = ptr.parent;
12. Set p's left/right child to be ptr's left/right child.
13. Else
14. Let ptr2 be a Node*.
15. ptr2 = findMin(ptr.right);
16. ptr.element = ptr2.element;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 59
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

17. p = ptr2.parent
18. Set p's left child to be ptr2's right child.

PROGRAM:

#include <iostream.h>
#include <stdlib.h>

#define FALSE 0
#define TRUE 1

struct AVLNode
{
int data ;
int balfact ;
AVLNode *left ;
AVLNode *right ;
};

class avltree
{
private :

AVLNode *root ;

public :

avltree( ) ;
AVLNode* insert ( int data, int *h ) ;
static AVLNode* buildtree ( AVLNode *root, int data, int *h ) ;
void display( AVLNode *root ) ;
AVLNode* deldata ( AVLNode* root, int data, int *h ) ;
static AVLNode* del ( AVLNode *node, AVLNode* root, int *h ) ;
static AVLNode* balright ( AVLNode *root, int *h ) ;
static AVLNode* balleft ( AVLNode* root, int *h ) ;
void setroot ( AVLNode *avl ) ;
~avltree( ) ;
static void deltree ( AVLNode *root ) ;
};

// initialises data member


avltree :: avltree( )
{
root = NULL ;
}
For more visit www.hitamtech.blogspot.com
For cool softwares and gadgets visit www.opensofty.blogspot.com Page 60
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

// inserts an element in a binary tree by calling buildtree


AVLNode* avltree :: insert ( int data, int *h )
{
root = buildtree ( root, data, h ) ;
return root ;
}

// inserts an element into tree


AVLNode* avltree :: buildtree ( AVLNode *root, int data, int *h )
{
AVLNode *node1, *node2 ;

if ( root == NULL )
{
root = new AVLNode ;
root -> data = data ;
root -> left = NULL ;
root -> right = NULL ;
root -> balfact = 0 ;
*h = TRUE ;
return ( root ) ;
}

if ( data < root -> data )


{
root -> left = buildtree ( root -> left, data, h ) ;

// If left subtree is higher


if ( *h )
{
switch ( root -> balfact )
{

case 1 :

node1 = root -> left ;


if ( node1 -> balfact == 1 )
{
cout << "\nRight rotation." ;
root -> left = node1 -> right ;
node1 -> right = root ;
root -> balfact = 0 ;
root = node1 ;
}
else

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 61
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{
cout << "\nDouble rotation, left then
right." ;
node2 = node1 -> right ;
node1 -> right = node2 -> left ;
node2 -> left = node1 ;
root -> left = node2 -> right ;
node2 -> right = root ;

if ( node2 -> balfact == 1 )


root -> balfact = -1 ;
else
root -> balfact = 0 ;
if ( node2 -> balfact == -1 )
node1 -> balfact = 1 ;
else
node1 -> balfact = 0 ;
root = node2 ;
}
root -> balfact = 0 ;
*h = FALSE ;
break ;

case 0 :

root -> balfact = 1 ;


break ;

case -1 :

root -> balfact = 0 ;


*h = FALSE ;
}
}
}

if ( data > root -> data )


{
root -> right = buildtree ( root -> right, data, h ) ;

// If the right subtree is higher


if ( *h )
{
switch ( root -> balfact )
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 62
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

case 1 :

root -> balfact = 0 ;


*h = FALSE ;
break ;

case 0 :

root -> balfact = -1 ;


break ;

case -1 :

node1 = root -> right ;


if ( node1 -> balfact == -1 )
{
cout << "\nLeft rotation." ;
root -> right = node1 -> left ;
node1 -> left = root ;
root -> balfact = 0 ;
root = node1 ;
}
else
{
cout << "\nDouble rotation, right then
left." ;
node2 = node1 -> left ;
node1 -> left = node2 -> right ;
node2 -> right = node1 ;
root -> right = node2 -> left ;
node2 -> left = root ;

if ( node2 -> balfact == -1 )


root -> balfact = 1 ;
else
root -> balfact = 0 ;
if ( node2 -> balfact == 1 )
node1 -> balfact = -1 ;
else
node1 -> balfact = 0 ;
root = node2 ;
}
root -> balfact = 0 ;
*h = FALSE ;
}
}

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 63
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

}
return ( root ) ;
}

// prints data
void avltree :: display ( AVLNode* root )
{
if ( root != NULL )
{
display ( root -> left ) ;
cout << root -> data << "\t" ;
display ( root -> right ) ;
}
}

// To delete an item from the tree


AVLNode* avltree :: deldata ( AVLNode *root, int data, int *h )
{
AVLNode *node ;

if ( root -> data == 13 )


cout << root -> data ;

if ( root == NULL )
{
cout << "\nNo such data." ;
return ( root ) ;
}
else
{
if ( data < root -> data )
{
root -> left = deldata ( root -> left, data, h ) ;
if ( *h )
root = balright ( root, h ) ;
}
else
{
if ( data > root -> data )
{
root -> right = deldata ( root -> right, data, h ) ;
if ( *h )
root = balleft ( root, h ) ;
}
else
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 64
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

node = root ;
if ( node -> right == NULL )
{
root = node -> left ;
*h = TRUE ;
delete ( node ) ;
}
else
{
if ( node -> left == NULL )
{
root = node -> right ;
*h = TRUE ;
delete ( node ) ;
}
else
{
node -> right = del ( node -> right, node,
h);
if ( *h )
root = balleft ( root, h ) ;
}
}
}
}
}
return ( root ) ;
}

AVLNode* avltree :: del ( AVLNode *succ, AVLNode *node, int *h )


{
AVLNode *temp = succ ;

if ( succ -> left != NULL )


{
succ -> left = del ( succ -> left, node, h ) ;
if ( *h )
succ = balright ( succ, h ) ;
}
else
{
temp = succ ;
node -> data = succ -> data ;
succ = succ -> right ;
delete ( temp ) ;
*h = TRUE ;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 65
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

}
return ( succ ) ;
}

// To balance the tree, if right sub-tree is higher


AVLNode* avltree :: balright ( AVLNode *root, int *h )
{
AVLNode *temp1, *temp2 ;

switch ( root -> balfact )


{

case 1 :

root -> balfact = 0 ;


break ;

case 0 :

root -> balfact = -1 ;


*h = FALSE ;
break ;

case -1 :

temp1 = root -> right ;


if ( temp1 -> balfact <= 0 )
{
cout << "\nLeft rotation." ;
root -> right = temp1 -> left ;
temp1 -> left = root ;

if ( temp1 -> balfact == 0 )


{
root -> balfact = -1 ;
temp1 -> balfact = 1 ;
*h = FALSE ;
}
else
{
root -> balfact = temp1 -> balfact = 0 ;
}
root = temp1 ;
}
else
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 66
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout << "\nDouble rotation, right then left." ;


temp2 = temp1 -> left ;
temp1 -> left = temp2 -> right ;
temp2 -> right = temp1 ;
root -> right = temp2 -> left ;
temp2 -> left = root ;

if ( temp2 -> balfact == -1 )


root -> balfact = 1 ;
else
root -> balfact = 0 ;
if ( temp2 -> balfact == 1 )
temp1 -> balfact = -1 ;
else
temp1 -> balfact = 0 ;
root = temp2 ;
temp2 -> balfact = 0 ;
}
}
return ( root ) ;
}

// To balance the tree, if left sub-tree is higher


AVLNode* avltree :: balleft ( AVLNode *root, int *h )
{
AVLNode *temp1, *temp2 ;

switch ( root -> balfact )


{

case -1 :

root -> balfact = 0 ;


break ;

case 0 :

root -> balfact = 1 ;


*h = FALSE ;
break ;

case 1 :

temp1 = root -> left ;


if ( temp1 -> balfact >= 0 )
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 67
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout << "\nRight rotation." ;


root -> left = temp1 -> right ;
temp1 -> right = root ;

if ( temp1 -> balfact == 0 )


{
root -> balfact = 1 ;
temp1 -> balfact = -1 ;
*h = FALSE ;
}
else
{
root -> balfact = temp1 -> balfact = 0 ;
}
root = temp1 ;
}
else
{
cout << "\nDouble rotation, left then right." ;

temp2 = temp1 -> right ;


temp1 -> right = temp2 -> left ;
temp2 -> left = temp1 ;
root -> left = temp2 -> right ;
temp2 -> right = root ;

if ( temp2 -> balfact == 1 )


root -> balfact = -1 ;
else
root -> balfact = 0 ;

if ( temp2-> balfact == -1 )
temp1 -> balfact = 1 ;
else
temp1 -> balfact = 0 ;
root = temp2 ;
temp2 -> balfact = 0 ;
}
}
return ( root ) ;
}

// sets new the root node


void avltree :: setroot ( AVLNode *avl )
{
root = avl ;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 68
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

// calls deltree to deallocate memory


avltree :: ~avltree( )
{
deltree ( root ) ;
}

// deletes the tree


void avltree :: deltree ( AVLNode *root )
{
if ( root != NULL )
{
deltree ( root -> left ) ;
deltree ( root -> right ) ;
}
delete ( root ) ;
}

void main( )
{
avltree at ;
AVLNode *avl = NULL ;
int h ;

avl = at.insert ( 20, &h ) ;


at.setroot ( avl ) ;
avl = at.insert ( 6, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 29, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 5, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 12, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 25, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 32, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 10, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 15, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 27, &h ) ;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 69
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

at.setroot ( avl ) ;
avl = at.insert ( 13, &h ) ;
at.setroot ( avl ) ;

cout << endl << "AVL tree:\n" ;


at.display ( avl ) ;

avl = at.deldata ( avl, 20, &h ) ;


at.setroot ( avl ) ;
avl = at.deldata ( avl, 12, &h ) ;
at.setroot ( avl ) ;

cout << endl << "AVL tree after deletion of a node:\n" ;


at.display ( avl ) ;
}

OUTPUT:

Enter your choice


1
enter the size
1
14
MENU
1. Insert
2. Search
3. Delete
4. Display
5. Quit
Enter your choice

5
Sorry

Left rotation.
AVL tree:
5 6 10 12 13 15 20 25 27 29
32
AVL tree after deletion of a node:
5 6 10 13 15 25 27 29 32
Left rotation.
AVL tree:
5 6 10 12 13 15 20 25 27 29

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 70
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

15) Write a C++ program to implement kruskal’s algorithm to


generate a minimum cost spanning tree

AIM:
A c++ program to perform Kruskals algorithm to generate
minimum cost spanning trees.

ALGORITHM:

I. Short_path ( )

1. start
2. set s[0] = 1, dist[0] = 0
3. repeat steps v = 1 to v = nov
i. steps set s[v] = 0
ii. set dist [v] = c[0] [v]
4. repeat steps i = 1 to i < nov
i. set min = 999
ii. repeat step w = 1 to w < nov
iii. a. if (s[w] == 0) then
i. if (dist [w] < min)
set v = w
min = dist[w].
b. s[v] = 1
iv. repeat step w = 1 to w< nov
i. if (s[w] ==0) then
if (min + c[v][w] < dist [w])
dist[w] = min + c[v][w]
5. end

PROGRAM:

#include <iostream.h>
#include <conio.h>
struct edge
{
int v1,v2,wt;
};
struct edge ed[20];
int A[20],v,e;
int getedges(struct edge ed[20])

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 71
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{
char c='y';
int p,v1,v2,wt;
e=0;
cout <<"Enter value of edges \n";
cin>>p;
while(c!='n')
{
e++;
cout<<"Enter v1,v2,wt\n";
cin>>v1>>v2>>wt;
ed[e].v1=v1;
ed[e].v2=v2;
ed[e].wt=wt;
cout<<"Read 9999 to stop\n";
cin>>c;
}
cout<<"Edges= "<<e;
return e;
}
void sorted(struct edge ed[20],int e)
{
int i,j;
struct edge temp;
ed[1].wt=-1;
for (i=2;i<e+1;i++)
{
temp=ed[i];
j=i;
while(ed[j-1].wt>temp.wt)
{
ed[j]=ed[j-1];
j--;}
ed[j]=temp;}}

initi(int A[20],int v)
{
int i;
{for(i=1;i<v+3;i++)
A[i]=0;}return 0;}

void findunion(int A[20],int v1,int v2)


{
int i,j;
i=v1;j=v2;
while(A[i]>0)

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 72
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{
i=A[i];}
while(A[j]>0)
{
j=A[j];}
if (i!=j)
{
A[j]=i;
cout<<"\n";
cout<<v1<<"to"<<v2;}}
void krusk(struct edge ed[20],int v)
{
int eds,edind,i,j,A[20];
eds=0;edind=0;
do
{
edind++;
eds++;
i=ed[edind].v1;
j=ed[edind].v2;
findunion(A,i,j);
}
while(eds<v);
}
void main()
{
int i,A[20],v,e;
clrscr();
cout<<"Enter the no. of vertices\n";
cin>>v;
e=getedges(ed);
sorted(ed,e);
initi(A,v);
cout<<"\nKruskals Spanning Tree Edge=\n";
krusk(ed,v);
getch();
}

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 73
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

OUTPUT:

Enter the no. of vertices


3
Enter value of edges
12
Enter v1,v2,wt
12
13
4
Read n to stop
12
Enter v1,v2,wt
13
14
Read n to stop
2
Enter v1,v2,wt
34
35
5
Read n to stop
n
Edges= 3
Kruskals Spanning Tree Edge=

12to13
34to35
2to13

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 74
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

16) Wite a C++ program to implement Prim’s algorithm to generate


a minimum cost spanning tree

AIM:

To write a c++ program to implement prims algorithm to generate


minimum spanning tree.

ALGORITHM:

1. start
2. for i equal to 0 to no do
3. selected [i] = false
4. end
5. for i is equal to 1 to n do
6. for j is equal to 1 to n do
7. tree [i][j] = q
8. end for
9. end for
10. selected [i] = true, ne = 1
11. while (ne<n)
1. min = 
2. for is equal to n do
3. if (selected [i] = true)
4. for j is equal to n do
5. if (selected [j] = false)
6. if (min > cptr [i][j])
7. min = cptr [i][j]
8. x< - i, y <-j
9. end if
12. tree[x][y] = 1
13. selected [y] = true
14. ne ++
15. return [tree]
16, end

PROGRAM:

#include <iostream.h>
#include <conio.h>
const int MAX=5;
struct lledge
{

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 75
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

int v1,v2;
float cost;
lledge *next;
};
int stree[MAX];
int count[MAX];
int mincost;
lledge *create(int cr1,int vr2,int cs);
lledge *kminstree(lledge *root,int n);
int getrval(int i);
void combine(int i,int j);
void del(lledge *root);

lledge *kminstree(lledge *root,int n)


{
lledge *temp=NULL;
lledge *p,*q;
int noe=0;
int i,p1,p2;
for(i=0;i<n;i++)
stree[i]=i;
for(i=0;i<n;i++)
count[i]=0;
while((noe<(n-1)) && (root!=NULL))
{
p=root;
root=root->next;
p1=getrval(p->v1);
p2=getrval(p->v2);
if(p1!=p2)
{
combine(p->v1,p->v2);
noe++;
mincost+=p->cost;
if(temp==NULL)
{
temp=p;
q=temp;
}
else
{ q->next=p;
q=q->next;
}
q->next=NULL;
}}
return temp;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 76
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

}
int getrval(int i)
{
int j,k,temp;
k=i;
while(stree[k]!=k)
k=stree[k];
j=i;
while(j!=k)
{
temp=stree[j];
stree[j]=k;
j=temp;
}
return k;
}
void combine(int i,int j)
{
if(count[i]<count[j])
stree[i]=j;
else
{ stree[j]=i;
if(count[i]==count[j])
count[j]++;
}}
void del(lledge *root)
{
lledge *temp;
while(root!=NULL)
{
temp=root->next;
delete root;
root=temp;
}}
void main()
{
lledge *temp,*root;
int i;
root= new lledge;
clrscr();
root->v1=4;
root->v2=3;
root->cost=1;
temp=root->next=new lledge;
temp=temp->next;
temp->v1=4;

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 77
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

temp->v2=2;
temp->cost=2;
temp->next=new lledge;
temp=temp->next;
temp->v1=3;
temp->v2=2;
temp->cost=3;
temp->next=new lledge;

temp=temp->next;
temp->v1=4;
temp->v2=1;
temp->cost=4;
temp->next=new lledge;
root=kminstree(root,MAX);
for(i=1;i<MAX;i++)
cout<<"\n stree "<<i<<"j->"<<stree[i];
cout<<"\n the min cost of spanning tree is\n "<<mincost;
cout<<"\n after deleting\n";
del(root);
getch();
}

OUTPUT:

stree 1j->4
stree 2j->4
stree 3j->4
stree 4j->4
the min cost of spanning tree is
7
after deleting

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 78
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

17) Write a C++ program to implement all the functions of a


dictionary (ADT) using hashing.

AIM:
A C++ Program to implement dictionary (ADT) using
hashing

PROGRAM:

#include <iostream.h>
#include <conio.h>
#include <process.h>
void init(int h[]);
void insert(int h[],int);
void search(int h[],int);
void Delete(int h[],int);
void disp();
void insert(int h[],int a)
{
int r,i;
r=a%10;
i=r;
if(h[i]==0)
h[i]=a;
else
i--;
h[i]=a;
}
void search(int h[],int key)
{
int i,r;
r=key%10;
while(h[r]!=0)
{
if(h[r]==key)
{cout<<"found";
break;
}
else
r--;
if(h[r]!=key)
cout<<"not found";
} }

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 79
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

void init(int h[])


{
int i;
for(i=0;i<10;i++)
h[i]=0;
}
void Delete(int h[],int e)
{
int i,r;
r=e%10;
while(h[r]!=0)
{
if(h[r]==e)
{cout<<"found";
h[r]=0;break;
}
else
r--;
if(h[r]!=e)
cout<<"not found";
}}
void disp(int h[])
{
int i;
cout<<"Array is \n";
for(i=0;i<10;i++)
{
cout<<h[i];
cout<<endl;}
}
void main()
{
int h[10],size,i,a,ch=0,key;
clrscr();
init(h);
do
{
cout<<" MENU \n";
cout<<"1. Insert\n";
cout<<"2. Search\n";
cout<<"3. Delete\n";
cout<<"4. Display\n";
cout<<"5. Quit\n ";
cout<<"Enter your choice\n";
cin>>ch;
switch(ch)

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 80
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{
case 1:
{
cout<<"enter the size\n";
cin>>size;
for(i=0;i<size;i++)
{cin>>a;
insert(h,a);
}break;}
case 2:
{
cout<<"Enter the element to be searched\n";
cin>>key;
search(h,key);
break;
}
case 3:
{
cout<<"Enter the element to be deleted\n";
cin>>a;
Delete(h,a);
break;
}
case 4:
{
disp(h);
break;
}
default: cout<<"Sorry\n";
}}while(ch!=5);
getch();
}

OUTPUT:

MENU
1. Insert
2. Search
3. Delete
4. Display
5. Quit
Enter your choice
1
enter the size
2
13

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 81
HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

12

MENU
1. Insert
2. Search
3. Delete
4. Display
5. Quit
Enter your choice
4
Array is
0
0
12
13
0
0
0
0
0
0
MENU
1. Insert
2. Search
3. Delete
4. Display
5. Quit
Enter your choice
1
enter the size
1
14
MENU
1. Insert
2. Search
3. Delete
4. Display
5. Quit
Enter your choice

For more visit www.hitamtech.blogspot.com


For cool softwares and gadgets visit www.opensofty.blogspot.com Page 82

You might also like