ads-lab manual

105
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

Upload: raghu-teja

Post on 29-Mar-2015

921 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

ADVANCED DATA STRUCTURES LAB

MANUAL

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 1

Page 2: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

INDEX

S.NO

NAME OF THE EXERCISEPAGE

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.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 2

Page 3: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 3

Page 4: ADS-LAB MANUAL

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. start2. read n3. if top greater than (n-1) then4. print “overflow”5. else6. toptop+17. data[top]=d8. print “element pushed to stack”9. stop.

2. Pop( )

1. start2. if top< 0 then3. print “underflow”4. else5. ddata[top]6. toptop-17. print “element is popped from the stack”8. stop.

PROGRAM:

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 4

Page 5: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

}; template<class T> stack<T>::stack() {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.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 5

Page 6: ADS-LAB MANUAL

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 14elements out of stack

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 6

Page 7: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 7

Page 8: ADS-LAB MANUAL

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. start2. read n3. if rear== (n-1) then4. print “overflow”5. print “enter a number”6. read num7. if front == -1 then8. front=rear=09. else10. rear=rear+111. a[rear]=num12. print”number is inserted”13. stop

2. Deletion( )

1. start2. if front == -1 then3. print “underflow”4. print “deleted element is”5. print a[front]6. if front==rear then7. front=rear=-18. else9. front=front+110. print “number is deleted”11. stop.

PROGRAM:

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 8

Page 9: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

#include <iomanip.h>const int size=10;int rear=-1,front=0;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() {

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 9

Page 10: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

if(front>rear) cout<<"queue is empty"<<endl; else {for(int i=rear;i>=front;i--) 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.QuitEnter Ur Choice1Enter size3Enter elements121314

*************MENU************ 1.Insert 2.Delete 3.Display 4.QuitEnter Ur Choice2

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 10

Page 11: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

12 13 14*************MENU************ 1.Insert 2.Delete 3.Display 4.QuitEnter Ur Choice3

deleted element is 12*************MENU************ 1.Insert 2.Delete 3.Display 4.QuitEnter Ur Choice3

deleted element is 13

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 11

Page 12: ADS-LAB MANUAL

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. start2. *n is a new node3. read num4. nd = num5. n next = top6. top=n7. print num8. print “is pushed to stack”9. stop.

2. Pop ( )

1. start2. if top == NULL then3. print “underflow”4. num= topd5. *d is the new node6. d=top7. top=topnext8. deleted9. stop.

PROGRAM:

#include <iostream.h>#include <conio.h>#include <process.h>class stak

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 12

Page 13: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{ struct stks { int data; stks *next; }; stks *p,*temp,*top;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)

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 13

Page 14: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<"stack is empty\n"; else { p=top; cout<<"stack contains \n"; while(p!=NULL) { 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.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 14

Page 15: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

OUTPUT:

stack operations using Linked list

1. PUSH2.POP3.DISPLAY4.quitenter Ur choice1enter a value12the element inserted = 12

1. PUSH2.POP3.DISPLAY4.quitenter Ur choice1enter a value13the element inserted = 13

1. PUSH2.POP3.DISPLAY4.quitenter Ur choice3stack contains13 12 1. PUSH2.POP3.DISPLAY

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 15

Page 16: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

4.quitenter Ur choice2element iremoved = 13 1. PUSH2.POP3.DISPLAY4.quitenter Ur choice2

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. start2. *link is a new node3. read num4. *new is a new node5. ndata=num //storing the data6. nlink = NULL //storing the address7. If front == NULL then

7.1 Front = rear=n7.2 Print “number is inserted”7.3 Return

8. rearlink =n9. rear=n10. print “number is inserted”

2. Deletion ( )

1. start2. *d is a new node3. if front == NULL then

3.1 print “underflow”3.2 return

4. if front is equal to rear then4.1 front = rear = NULL4.2 print “ no. is deleted”

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 16

Page 17: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

4.3 return5. d=front6. front=frontlink7. print ddata8. print “is deleted”9. deleted10. stop.

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)

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 17

Page 18: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{ re=fr=temp; return; } re->link=temp; re=re->link; }int rque::delq(){ if(fr==NULL) { 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;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 18

Page 19: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 19

Page 20: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

1.Insert 2.Display 3.Delete 4.QuitEnter Ur Choice1

enter the size of the queue4Enter elements12131415

*************MENU*********** 1.Insert 2.Display 3.Delete 4.QuitEnter Ur Choice2Display list12131415

*************MENU*********** 1.Insert 2.Display 3.Delete 4.QuitEnter Ur Choice3Delete process

Queue is emptyExtracted element is 0 *************MENU*********** 1.Insert 2.Display 3.Delete 4.QuitEnter Ur Choice

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 20

Page 21: ADS-LAB MANUAL

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. start2. if front == 0 & rear==-1

a. d[front]==xb. rear++

3. if front == 0 & rear != -1a. print “insertion not possible”

4. elsea. f--b. dq[f]=x

5. print “ no. is inserted”

2.Deletion_Front()

1. start2. if rear = = -13. print “queue is empty”4. else5. if(front = = rear)

a. rear=-1;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 21

Page 22: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

b. front=0;6. else7. front++;8. print “ deleted”

3.Insertion_Rear()

1. start2. if(r==n-1)3. print “Queue Is Full”4. else

a. r++;b. dq[r]=x;

5. print “inserted”

4.Deletion_Rear ()

1. start2. if rear == -13. print “Queue is empty”4. else5. if front == rear

a. front =0b. rear=-1

6. print “deleted”

PROGRAM:

// Program De queue#include<iostream.h>#include<conio.h>#define n 3template<class t>class dqueue{

private:t dq[n],x;int i,j,f,r;

public:dqueue(){

f=0;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 22

Page 23: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 23

Page 24: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

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;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 24

Page 25: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

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;

}

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 25

Page 26: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

}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;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.INT2.CHAR3.EXITENTER THE CHOICE:1

1.INSERT_REAR2.INSERT_FRONT3.DELETE_REAR4.DELETE_FRONT5.SHOW

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 26

Page 27: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

6.EXITENTER THE CHOICE:1ENTER THE DATA:12 ANY MORE DATA(y/n):yENTER THE DATA:13 ANY MORE DATA(y/n):n

1.INSERT_REAR2.INSERT_FRONT3.DELETE_REAR4.DELETE_FRONT5.SHOW6.EXITENTER THE CHOICE:5

THE ELEMENTS ARE:

12 131.INSERT_REAR2.INSERT_FRONT3.DELETE_REAR4.DELETE_FRONT5.SHOW6.EXITENTER THE CHOICE:3

THE ELEMENTS ARE:

121.INSERT_REAR2.INSERT_FRONT3.DELETE_REAR4.DELETE_FRONT5.SHOW6.EXITENTER THE CHOICE:

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 27

Page 28: ADS-LAB MANUAL

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. start2. * link is a new node3. read num4. * n is a new node 5. n data = num6. n link = NULL7. if front == NULL then

7.1 front = rear = n7.2 print “no is inserted7.3 return

8. rear link = n

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 28

Page 29: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

9. rear = n10. print “no is inserted”

II. Deletion ( ):

1. start2. * d is a new node3. if front is equal to NULL then

3.1 print “underflow”3.2 return

4. front is equal to rear then4.1 front = rear = NULL4.2 print “no is deleted4.3 return

5. d = front6. front = front link7. print d data8. print “ is deleted”9. delete d10. stop

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 29

Page 30: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 30

Page 31: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 31

Page 32: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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();

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 32

Page 33: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

7) Write a C++ program to perform the following operations.a. Insert an element into binary search treeb. Delete an element from binary search treec. 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. start2. if ptr ! = NULL then3. in order (ptr LC)4. print data (ptr)5. in order (ptr RC)6. stop

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 33

Page 34: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

PROGRAM:

#include <iostream.h>#include <conio.h>#define TRUE 1#define FALSE 0class 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(){ 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)

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 34

Page 35: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 35

Page 36: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{ 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; } *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)

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 36

Page 37: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{ 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"; 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();}

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 37

Page 38: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

OUTPUT:

Output

Enter size of the tree 3enter elements 3423 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

8) Write a C++ program that uses non-recursive functions to traverse the given binary treea. Preorderb. Postorderc. Inorder

AIM: A c++ program to create a binary tree and traverse it

inorder, preorder, and postorder.

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 38

Page 39: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

ALGORITHM:

I. build ( ):

1. start2. read num3. if num is equal to zero then

3.1 root = NULL4. root = new node5. root data = num6. root lptr = root rptr = NULL7. read num8. while num ! = 0 then

8.1 n = new node8.2 n data = num8.3 n lptr = n rptr = NULL8.4 s = root8.5 while s! = NULL8.6 c = s

8.6.1 if num > s data8.6.2 s = s rptr8.6.3 else 8.6.4 s = s lptr8.6.5 if num > c data8.6.6 c rptr = n8.6.7 else8.6.8 c lptr = n8.6.9 read num

9. stop

II.Inorder ( node * r) :

1. start2. if ptr ! = NULL then3. in order (ptr LC)4. print data (ptr)5. in order (ptr RC)6. stop

III.Preorder ( node * r) :

1. start2. if ptr ! = NULL then3. print data (ptr)

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 39

Page 40: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

5. pre order (ptr LC)4. pre order (ptr RC)5. stop

IV.Postorder ( node * r) :

1. start2. if ptr ! = NULL then3. 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); ~btre(); };btre::btre(){ root=NULL;}void btre::ctree(int num){ insert(&root,num); }void btre::insert(bnode **sr,int num)

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 40

Page 41: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{ 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; }void btre::preorder(bnode *sr){ if(sr!=NULL) { cout<<"\t"<<sr->data; preorder(sr->lc); preorder(sr->rc); } else

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 41

Page 42: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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; bt.ctree(num); } bt.traverse(); getch();}

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 42

Page 43: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

OUTPUT:

specify the no of items to be inserted5enter the data12enter the data23enter the data34enter the data11enter the data45Inorder 11 12 23 34 45

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

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 43

Page 44: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

ALGORITHM:

I. DFS (int i)1. start2. visited [i] = 13. print “Node visited”4. print i = i + 15. 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 0const 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++) visit[i]=FALSE; }void graph::dfs(int v,node **p){ node *t;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 44

Page 45: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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); arr[2]=v1; v1->next=v2=g.getn(6); v2->next=v3=g.getn(7); v3->next=NULL;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 45

Page 46: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 46

Page 47: ADS-LAB MANUAL

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 forthe given graph.

ALGORITHM: 1. bfs (int) :

1. start 2. visited [i] = 13. 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+18. stop

PROGRAM:

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 47

Page 48: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 48

Page 49: ADS-LAB MANUAL

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;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 49

Page 50: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

clrscr();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:

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 50

Page 51: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

Graph before deletion

1 2 3 4 5 6 7 8Graph 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 = firsts = secondi = 0

2. Repeat while (f<second and s <= third)if (k[f] = k[s]) then{i = i + 1temp [i] = k[f]f = f + 1}else {i = i + 1temp [i] = k[s]s = s + 1}

3. store the elements which are not processed if (f>= second) then repeat while (s<=third){i = i +1temp [i] = k[s]s = s + 1}else repeat while (f< second){i = i + 1 temp[i] = k[f]f = f + 1}

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 51

Page 52: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

4. get back the elements from the temporary elements

repeat for i = 1 to n do k [first – 1 + i] = temp [i]5. return

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 52

Page 53: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

while((i<n) && (j<m)) { if(a[i]<b[j]) {c[k]=a[i]; i++; k++; } 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;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 53

Page 54: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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);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 value5

enter elements14231256

entered list is

ascending order 1 4 12 23 56enter m value3

enter elements13121

entered list is

ascending order 1 12 13MERGED ARRAY IS

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 54

Page 55: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

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) / 23. while start ≥ 0 do4. (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 – 17. (after sifting down the root all nodes/elements are in

heap order)

III.siftDown(a, start, end)

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 55

Page 56: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

1. root := start2. 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] then7. child := child + 1 (... then point to the right child

instead)8. if a[root] < a[child] then (out of max-heap order)9. swap(a[root], a[child])10. root := child (repeat to continue sifting down

the child now) 11. else12. 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;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 56

Page 57: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

elses=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; } 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 size4enter elements12214sorted array124

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 57

Page 58: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

12

13) Write a C++ program to perform the following operationsa. Insertion int a B-Treeb. 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] <- sc.leaf[s] <- FALSEd. n[s] <- 0e.c1 <- rf. 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 true4. else return false //k is not in subtree

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 58

Page 59: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

5. else //x is an internal node6. if k is in x then 7. y = the child of x that precedes k

a. if y has at least t keys thenb. 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 call8. else //y has t-1 keys 9. z = the child of x that follows k10. if z has at least t keys then11. k' = the successor of k12. Copy k' over k //i.e., replace k with k'13. B-Tree-Delete(z, k') //Note: recursive call14. stop

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 59

Page 60: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 60

Page 61: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

} 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); cout<<"\n in-order traversal : "<<endl; bt.display(); getch(); }

OUTPUT:

Enter size of the tree6enter elements125623245566778

Btree before deletion12 45 56 56 67 78 232 enter deleting elem67

Binary tree after delete12 45 56 56 78 232 do you want delete ?n

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 61

Page 62: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

14) Write a C++ program to perform the following operationsa. 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;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 62

Page 63: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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(X3. If ptr is not null,4. Decrement elementCount;5. If ptr == root6. Set root to null and return7. If ptr is a leaf node, 8. p = ptr.parent9. 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; 17. p = ptr2.parent18. 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 ;

} ;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 63

Page 64: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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 memberavltree :: avltree( ){

root = NULL ;}

// inserts an element in a binary tree by calling buildtreeAVLNode* avltree :: insert ( int data, int *h ){

root = buildtree ( root, data, h ) ;return root ;

}

// inserts an element into treeAVLNode* avltree :: buildtree ( AVLNode *root, int data, int *h ){

AVLNode *node1, *node2 ;

if ( root == NULL ){

root = new AVLNode ;root -> data = data ;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 64

Page 65: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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 higherif ( *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{

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 ;

elseroot -> balfact = 0 ;

if ( node2 -> balfact == -1 )node1 -> balfact = 1 ;

else

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 65

Page 66: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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 higherif ( *h ){

switch ( root -> balfact ){

case 1 :

root -> balfact = 0 ;*h = FALSE ;break ;

case 0 :

root -> balfact = -1 ;break ;

case -1 :

node1 = root -> right ;if ( node1 -> balfact == -1 )

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 66

Page 67: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

elseroot -> balfact = 0 ;

if ( node2 -> balfact == 1 )node1 -> balfact = -1 ;

elsenode1 -> balfact = 0 ;

root = node2 ;}root -> balfact = 0 ;*h = FALSE ;

}}

}return ( root ) ;

}

// prints datavoid avltree :: display ( AVLNode* root ){

if ( root != NULL ){

display ( root -> left ) ;cout << root -> data << "\t" ;display ( root -> right ) ;

}}

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 67

Page 68: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

// To delete an item from the treeAVLNode* 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{

node = root ;if ( node -> right == NULL ){

root = node -> left ;*h = TRUE ;delete ( node ) ;

}else{

if ( node -> left == NULL ){

root = node -> right ;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 68

Page 69: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

}return ( succ ) ;

}

// To balance the tree, if right sub-tree is higherAVLNode* avltree :: balright ( AVLNode *root, int *h ){

AVLNode *temp1, *temp2 ;

switch ( root -> balfact ){

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 69

Page 70: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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{

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 70

Page 71: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

cout << "\nRight rotation." ;root -> left = temp1 -> right ;temp1 -> right = root ;

if ( temp1 -> balfact == 0 ){

root -> balfact = 1 ;temp1 -> balfact = -1 ;*h = FALSE ;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 71

Page 72: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

elseroot -> balfact = 0 ;

if ( temp2-> balfact == -1 )temp1 -> balfact = 1 ;

elsetemp1 -> balfact = 0 ;

root = temp2 ;temp2 -> balfact = 0 ;

}}return ( root ) ;

}

// sets new the root nodevoid avltree :: setroot ( AVLNode *avl ){

root = avl ;}

// calls deltree to deallocate memoryavltree :: ~avltree( ){

deltree ( root ) ;}

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 72

Page 73: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

// 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 ) ;at.setroot ( avl ) ;avl = at.insert ( 13, &h ) ;at.setroot ( avl ) ;

cout << endl << "AVL tree:\n" ;at.display ( avl ) ;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 73

Page 74: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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 choice1enter the size114 MENU1. Insert2. Search3. Delete4. Display5. Quit Enter your choice

5Sorry

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 74

Page 75: ADS-LAB MANUAL

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. start2. set s[0] = 1, dist[0] = 03. repeat steps v = 1 to v = nov

i. steps set s[v] = 0ii. set dist [v] = c[0] [v]

4. repeat steps i = 1 to i < novi. set min = 999ii. repeat step w = 1 to w < noviii. a. if (s[w] == 0) then

i. if (dist [w] < min) set v = w min = dist[w].

b. s[v] = 1iv. 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]){char c='y';int p,v1,v2,wt;e=0;

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 75

Page 76: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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){i=A[i];}while(A[j]>0)

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 76

Page 77: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

{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.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 77

Page 78: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

OUTPUT:

Enter the no. of vertices3Enter value of edges12Enter v1,v2,wt12134Read n to stop12Enter v1,v2,wt1314Read n to stop2Enter v1,v2,wt34355Read n to stopnEdges= 3Kruskals Spanning Tree Edge=

12to1334to352to13

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 78

Page 79: ADS-LAB MANUAL

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. start2. for i equal to 0 to no do3. selected [i] = false4. end5. for i is equal to 1 to n do6. for j is equal to 1 to n do7. tree [i][j] = q8. end for9. end for10. selected [i] = true, ne = 111. while (ne<n)

1. min = 2. for is equal to n do3. if (selected [i] = true)4. for j is equal to n do5. if (selected [j] = false)6. if (min > cptr [i][j])7. min = cptr [i][j]8. x< - i, y <-j9. end if

12. tree[x][y] = 113. selected [y] = true14. 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.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 79

Page 80: ADS-LAB MANUAL

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 80

Page 81: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 81

Page 82: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

temp=temp->next; temp->v1=4; 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.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 82

Page 83: ADS-LAB MANUAL

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

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 83

Page 84: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

r--; if(h[r]!=key) cout<<"not found"; } }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";

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 84

Page 85: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

cout<<"4. Display\n"; cout<<"5. Quit\n "; cout<<"Enter your choice\n"; cin>>ch; switch(ch) { 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:

MENU1. Insert2. Search3. Delete4. Display

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 85

Page 86: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

5. Quit Enter your choice1enter the size21312

MENU1. Insert2. Search3. Delete4. Display5. Quit Enter your choice4Array is001213000000 MENU1. Insert2. Search3. Delete4. Display5. Quit Enter your choice1enter the size114 MENU1. Insert2. Search3. Delete4. Display

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 86

Page 87: ADS-LAB MANUAL

HITAM ADVANCED DATA STRUCTURES – LAB MANUAL

5. Quit Enter your choice

For more visit www.hitamtech.blogspot.comFor cool softwares and gadgets visit www.opensofty.blogspot.com Page 87