ADVANCED DATA STRUCTURES – LAB MANUAL
ADVANCED DATA STRUCTURES LAB MANUAL
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
1
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
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
2
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
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
2
ADVANCED DATA STRUCTURES – LAB MANUAL
B-Tree- Insertion & Deletion
AVL Tree- Insertion & Deletion
54
58
Kruskal’s Algorithm
70
Prim’s Algorithm
74
Dictionary ADT using hashing
78
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
3
ADVANCED DATA STRUCTURES – LAB MANUAL
1)
Writ Wr ite e a C++ C++ pro progr gram am to impl implem emen entt sta stack ck ADT ADT usi using ng arra arrays ys.. AIM: To write a C++ program to implement stack ADT using arrays. ALGORITHM: 1. Push ( ) 1. 2. 3. 4. 5.
start read n if top top grea greater ter than than (n-1) (n-1) then then prin printt “ove “overf rflo low” w” else 6. toptop+1 7. data ata[top [top]= ]=d d 8. print print “elem “element ent pushe pushed d to stack” stack” 9. stop.
2. Pop( ) 1. 2. 3. 4.
start if top< top< 0 then then prin printt “un “unde derf rflo low” w” else 5. ddata[top] 6. toptop-1 7. print print “elem “element ent is popped popped from the stack” stack” 8. stop.
PROGRAM: #include
#include #define size 15 template class stack { T top,stk[size]; top,stk[size]; public: stack(); void push(T n); T pop(); }; template stack::stack() { top=0; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
4
ADVANCED DATA STRUCTURES – LAB MANUAL
} template void stack::push(T n) { if (top==size) {cout<<"\n stack is full"; } stk[top]=n; top++; } template stack::pop() { if(top==NULL) { cout<<"\n stack is empty\n"; } top--; return(stk[top]); } void main() { stack ob; int i=0; clrscr(); cout<<"elements of stack"<
OUTPUT: E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
5
ADVANCED DATA STRUCTURES – LAB MANUAL
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
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
6
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 #include #include const int size=10; int rear=-1,front=0; template E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
7
ADVANCED DATA STRUCTURES – LAB MANUAL
class queue { T qa[size]; T temp; public: int empty(void); int full(void); void insert(T x); T delet(void); void disp(); }; template int queue::empty(void) queue::empty(void) { if(front>rear) return(0); else return 1; } template int queue::full(void) queue::full(void) { if(rear>size-1) return 0; else return 1; } template void queue::insert(T x) { qa[++rear]=x; cout< T queue::delet() queue::delet() { return (qa[front++]); (qa[front++]); } template void queue::disp() { if(front>rear) cout<<"queue is empty"<=front;i--) i=rear;i>=front;i--) cout<
Page
8
ADVANCED DATA STRUCTURES – LAB MANUAL
} void main() { int p; int v; queue 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 4.Quit Enter Ur Choice 3 E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
9
ADVANCED DATA STRUCTURES – LAB MANUAL
deleted element is 12 *************MENU************ 1.Insert 2.Delete 3.Display 4.Quit Enter Ur Choice 3 deleted element is 13
3) Writ Wr ite e a C++ C++ Prog Progra ram m to to imp imple leme ment nt a Sta Stack ck ADT ADT usin using g a sin singl gly y linked list AIM:
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
10
ADVANCED DATA STRUCTURES – LAB MANUAL
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 new node node 3. read num 4. nd = num 5. n next = top 6. top=n 7. print nu num 8. print print “is “is push pushed ed to to stack stack”” 9. stop. 2. Pop ( ) 1. start 2. if top top == == NUL NULL L the then n 3. prin printt “un “unde derf rflo low” w” 4. num= topd 5. *d is the the new new node node 6. d=top 7. top=topnext 8. deleted 9. stop. PROGRAM: #include #include #include class stak { struct stks { int data; stks *next; }; stks *p,*temp,*top; public: stak() { top = NULL; } push(); pop(); E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
11
ADVANCED DATA STRUCTURES – LAB MANUAL
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 = "<data<next; cout<<"element iremoved = "<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) { cout<data<<" "; p=p->next; } }return 0; } void main() { stak s1; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
12
ADVANCED DATA STRUCTURES – LAB MANUAL
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); }}}
OUTPUT: stack operations using Linked list 1. PUSH E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
13
ADVANCED DATA STRUCTURES – LAB MANUAL
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
4)
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 2 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.
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
14
ADVANCED DATA STRUCTURES – LAB MANUAL
ALGORITHM: 1. Insertion ( ) 1.1.start 1.2. *link is a new node 1.3. read num 1.4. *new is a new node ndata=num //storing the data 1.5. 1.6. nlink = NULL //storing the address 1.7.If front == NULL then 1.7.1 Front = rear=n 1.7.2 Print “number is inserted” 1.7.3 Return 1.8. rearlink =n 1.9. rear=n 1.10. print “number is inserted” 2. Deletion ( )
.1 .2 .1 .2 .3
.1start .2 *d is a new node .3 if front == NULL then print “underflow” return .4 if front is equal to rear then front = rear = NULL print “ no. is deleted” return .5 d=front .6 front=front link print ddata .7 .8 print “is deleted” .9 deleted .10 stop.
PROGRAM: #include #include #include
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
15
ADVANCED DATA STRUCTURES – LAB MANUAL
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) { cout<<"\nQueue is empty\n"; return NULL; } else {node *temp; int item; item=fr->data; temp=fr; fr=fr->link; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
16
ADVANCED DATA STRUCTURES – LAB MANUAL
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<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) { case 1: { cout<<"\n enter the size of the queue\n"; cin>>size; cout<<"Enter elements\n"; for(i=0;i>a[i]; rq.addq(a[i]); } break; } E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
17
ADVANCED DATA STRUCTURES – LAB MANUAL
case 2: { cout<<"Display list\n"; rq.disp();break; } case 3: { cout<<"Delete process\n"; int j=rq.delq(); cout<<"Extracted element is "<
Page
18
ADVANCED DATA STRUCTURES – LAB MANUAL
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
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
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
19
ADVANCED DATA STRUCTURES – LAB MANUAL
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. 2. 3. 4. 5.
start if rear = = -1 print “queue is empty” else if(front = = rear) a. rear=-1; b. front=0; 6. else 7. front++; 8. print “ deleted” 3.Insertion_Rear() 1. 2. 3. 4.
start if(r==n-1) print “Queue Is Full” else a. r++; b. dq[r]=x; 5. print “inserted”
4.Deletion_Rear () 1. 2. 3. 4. 5.
start if rear == -1 print “Queue is empty” else if front == rear a. front =0 b. rear=-1 6. print “deleted” PROGRAM: E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
20
ADVANCED DATA STRUCTURES – LAB MANUAL
// Program De queue #include #include #define n 3 template 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 void dqueue::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 void dqueue::insert_front() { E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
21
ADVANCED DATA STRUCTURES – LAB MANUAL
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 void dqueue::del_rear() { if(r==-1) cout<<"\t\t\tQUEUE IS EMPTY"; else { if(f==r) { f=0; r=-1; show(); } else { r--; show(); } E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
22
ADVANCED DATA STRUCTURES – LAB MANUAL
} } template void dqueue::del_front() { if(r==-1) cout<<"\t\t\tQUEUE IS EMPTY"; else { if(f==r) { r=-1; f=0; show(); } else { f++; show(); } } } template void dqueue::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< void dq_op(dqueue 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:"; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
23
ADVANCED DATA STRUCTURES – LAB MANUAL
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 i; int x1; dq_op(i); break; case 2: dqueue c; char x2; dq_op(c); break; case 3:break; } }while(ch!=3); } OUTPUT: 1.INT 2.CHAR E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
24
ADVANCED DATA STRUCTURES – LAB MANUAL
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 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:
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
25
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
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
26
ADVANCED DATA STRUCTURES – LAB MANUAL
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
PROGRAM: #include #include template class node { node *prev; T data; node *next; public: friend class Doub; }; template class Doub { node *fr; public: Doub(){fr = 0;} ~Doub(); E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
27
ADVANCED DATA STRUCTURES – LAB MANUAL
void create(); void insert(int , T); void Delete(int, T &); void disp(); }; template Doub::~Doub() { node *p=fr; node *q; while(p) { q=p; p=p->next; delete q; cout<<"object destsroyed"< void Doub:: create() { T data; char ch; do {cout<<"Enter the data element"; cin>>data; if (fr==0) { fr=new node; fr->prev=0; fr->data=data; fr->next=0; } else { node *q=fr,*r; while(q->next!=0) { q=q-> next; } r=new node; 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'); } E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
28
ADVANCED DATA STRUCTURES – LAB MANUAL
template void Doub::insert(int pos,T data) { node *q,*temp; if(pos==0) { temp=new node; temp->prev=0; temp->data=data; temp->next=fr; fr=temp; } else { node *p=fr; for(int i=0;inext; if(p) { temp=new node; temp->data=data; p->prev->next=temp; temp->next=p; p->prev=temp; } else cout<<"Invalid position entered"; } } template void Doub::Delete(int pos,T &data) { node *p=fr; if (pos==0) { fr=fr->next; fr->prev=0; data=p->data; delete p; } else { for(int k=0; knext; data=p->data; if(p->next==NULL) p->prev->next=NULL; else { E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
29
ADVANCED DATA STRUCTURES – LAB MANUAL
p->next->prev=p->prev; p->prev->next=p->next; } delete p; } } template void Doub:: disp() { node *curr=fr; cout<data<<"->"; curr=curr->next; }} void main() { Doub obj; int pos,data; clrscr(); cout<<"create method is invocked"<>pos>>data; obj.insert(pos,data); cout<<"Display"<>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 E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
30
ADVANCED DATA STRUCTURES – LAB MANUAL
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 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 #include #define TRUE 1 #define FALSE 0
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
31
ADVANCED DATA STRUCTURES – LAB MANUAL
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() { 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) E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
32
ADVANCED DATA STRUCTURES – LAB MANUAL
{ 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) { 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) E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
33
ADVANCED DATA STRUCTURES – LAB MANUAL
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) { inorder(sr->left); cout<data<<"\t"; inorder(sr->right); }} btree::~btree() { del(root); } void btree::del(btnode *sr) { if(sr!=NULL) { del(sr->left); del(sr->right); E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
34
ADVANCED DATA STRUCTURES – LAB MANUAL
} 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(); }
OUTPUT: Output Enter size of the tree 3 enter elements 34 23 12 56
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
35
ADVANCED DATA STRUCTURES – LAB MANUAL
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 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
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
36
ADVANCED DATA STRUCTURES – LAB MANUAL
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 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 #include E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
37
ADVANCED DATA STRUCTURES – LAB MANUAL
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) { 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"; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
38
ADVANCED DATA STRUCTURES – LAB MANUAL
preorder(root); cout<<"\npost order \n"; postorder(root); } void btre::inorder(bnode *sr) { if(sr!=NULL) { inorder(sr->lc); cout<<"\t"<data; inorder(sr->rc); } else return; } void btre::preorder(bnode *sr) { if(sr!=NULL) { cout<<"\t"<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"<data; } else return; } btre::~btre() { del(root); } void btre::del(bnode *sr) { if(sr!=NULL) { del(sr->lc); del(sr->rc); } delete sr; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
39
ADVANCED DATA STRUCTURES – LAB MANUAL
} 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(); }
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 pre order 12 11 post order 11 45
34
45
23
34
45
34
23
12
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
40
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 #include #define TRUE 1 #define FALSE 0 const int MAX=8; struct node { int data; node *next; }; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
41
ADVANCED DATA STRUCTURES – LAB MANUAL
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;idata-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; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
42
ADVANCED DATA STRUCTURES – LAB MANUAL
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; 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<
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
43
ADVANCED DATA STRUCTURES – LAB MANUAL
dfs format is :1 2 4
8
5
6
3
7
Graph after deletion
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 #include #include #define TRUE 1 #define FALSE 0 const int MAX=8; struct node { E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
44
ADVANCED DATA STRUCTURES – LAB MANUAL
int data; node*next; }; class graph { private: int visited[MAX]; int q[8]; int front, rear; 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;idata-1]==FALSE) { addqueue(q,u->data,&front,&rear); visited[u->data-1]=TRUE; cout<data<<"\t"; } u=u->next; } } } node *graph::getnode_write(int val) E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
45
ADVANCED DATA STRUCTURES – LAB MANUAL
{ node *newnode=new node; newnode->data=val; return newnode; } void graph::addqueue(int *a,int vertex, int *f, int *r) { if(*r==MAX-1) { 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; } E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
46
ADVANCED DATA STRUCTURES – LAB MANUAL
} void main() { node *arr[MAX]; node *v1,*v2,*v3,*v4; graph g; 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<
Page
47
ADVANCED DATA STRUCTURES – LAB MANUAL
getch(); } OUTPUT: Graph before deletion
11)
1 2 3 4 5 6 7 8 Graph after deletion 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) 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
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
48
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 #include 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;ja[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
Page
49
ADVANCED DATA STRUCTURES – LAB MANUAL
if(a[i]b[j]) { c[k]=b[j]; j++; k++; } else { c[k]=a[i]; i++;j++;k++; } cout<>n; cout<<"\nenter elements \n"; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
50
ADVANCED DATA STRUCTURES – LAB MANUAL
for(i=0;i>a[i]; ms.bsort(n,a); cout<<"\nenter m value\n"; cin>>m; cout<<"\nenter elements\n"; for(i=0;i>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 E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
51
ADVANCED DATA STRUCTURES – LAB MANUAL
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) 4.
swap(a[end], a[0]) decrease the size of the heap by one so that the previous max value
5. 6. 7.
stay in its proper placement) end := end - 1 (put the heap back in max-heap order) siftDown(a, 0, end)
will
II.heapify(a,count) 1. 2. 3. 4.
5. 6. 7.
(start is assigned the index in a of the last parent node) start := (count - 1) / 2 while start ≥ 0 do (sift down the node at index start to the proper place such that all nodes below the start index are in heap order) siftDown(a, start, count-1) start := start – 1 (after sifting down the root all nodes/elements are in heap order)
III.siftDown(a, start, end) 1. 2.
3. 4. 5.
6. 7.
root := start while root * 2 + 1 ≤ end do (While the root has at least one child) child := root * 2 + 1 (root*2+1 points to the left child) (If the child has a sibling and the child's value is less than its sibling's...) if child < end and a[child] < a[child + 1] then child := child + 1 (... then point to the right child instead)
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
52
ADVANCED DATA STRUCTURES – LAB MANUAL
if a[root] < a[child] then (out of max-heap order) 9. swap(a[root], a[child]) (repeat to continue sifting down the child 10. root := child now) 11. else 12. return 8.
PROGRAM: #include #include void hsort(int x[],int n) { int i,elt,s,f,ivalue; for(i=1;i0 && (x[f]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) && (ivaluei-1) s=-1; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
53
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>a[i]; hsort(a,size); cout<<"sorted array \n"; for(i=0;i
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
54
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 BTree 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
if k is in x then 3. delete k from x and return true 4. else return false //k is not in subtree 2.
5.
else //x is an internal node
if k is in x then 7. y = the child of x that precedes k if y has at least t keys then a. b. k' = the predecessor of k (use B-TreeFindLargest) 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 6.
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
55
ADVANCED DATA STRUCTURES – LAB MANUAL
PROGRAM: #include #include 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;ileft=create(a1,l,r,*(l+index)); temp->data=*(a1+index); temp->right=create(a1,l,r,*(r+index)); } E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
56
ADVANCED DATA STRUCTURES – LAB MANUAL
return temp; } void btre::display() { inorder(root); } void btre::inorder(node *sr) { if(sr!=NULL) { inorder(sr->left); cout<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>a1[i];} btre bt(a1,l,r,sz); bt.insert(0); cout<<"\n in-order traversal : "<
Page
57
ADVANCED DATA STRUCTURES – LAB MANUAL
OUTPUT: Enter size of the tree 6 enter elements 12 56 232 45 56 67 78 Btree before deletion 12 45 56 56 67 67
78
Binary tree after delete 12 45 56 56 78 n
232
14)
232
enter deleting elem
do you want delete ?
Write a C++ program to perform the following operations a. Insertion into an AVL Tree b. Deletion from an AVL Tree
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
58
ADVANCED DATA STRUCTURES – LAB MANUAL
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 node p, p's child q and p's grandchild r within the path from inserted node to p.
a. b. c.
3
If p is unbalanced, do one of the following rotations: a. b. c. d.
if (p.left == q) and (p.left.left == r ), single rotation right in p; if (p.right == q ) and (p.right.right == r ), single rotation left in p; if (p.left == q) and (p.left.right == r ), LR-double rotation in p; or if (p.right == q ) and (p.right.left == r ), RL-double rotation in p.
II. DELETE: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
Let ptr, p be a reference to a Node. ptr = find(X If ptr is not null, Decrement elementCount; If ptr == root Set root to null and return If ptr is a leaf node, p = ptr.parent Set p's left/right child to null. Else If ptr is a node with 1 child (left/right), p = ptr.parent; Set p's left/right child to be ptr's left/right child. Else Let ptr2 be a Node*. ptr2 = findMin(ptr.right); ptr.element = ptr2.element; p = ptr2.parent Set p's left child to be ptr2's right child.
PROGRAM:
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
59
ADVANCED DATA STRUCTURES – LAB MANUAL
#include #include #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 ; } // inserts an element in a binary tree by calling buildtree AVLNode* avltree :: insert ( int data, int *h ) { root = buildtree ( root, data, h ) ; return root ; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
60
ADVANCED DATA STRUCTURES – LAB MANUAL
} // 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 { cout << "\nDouble rotation, left then right." ; node2 = node1 -> right ; node1 -> right = node2 -> left ; node2 -> left = node1 ; root -> left = node2 -> right ; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
61
ADVANCED DATA STRUCTURES – LAB MANUAL
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 ) { case 1 : root -> balfact = 0 ; *h = FALSE ; break ; case 0 :
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
62
ADVANCED DATA STRUCTURES – LAB MANUAL
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 ; } } } return ( root ) ; } // prints data void avltree :: display ( AVLNode* root ) { if ( root != NULL ) { E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
63
ADVANCED DATA STRUCTURES – LAB MANUAL
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 { node = root ; if ( node -> right == NULL ) { root = node -> left ; *h = TRUE ; delete ( node ) ; } else { if ( node -> left == NULL ) E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
64
ADVANCED DATA STRUCTURES – LAB MANUAL
{ 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 ; } 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 ) { E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
65
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 root -> balfact = 0 ; if ( temp2 -> balfact == 1 ) E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
66
ADVANCED DATA STRUCTURES – LAB MANUAL
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 ) { 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 ; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
67
ADVANCED DATA STRUCTURES – LAB MANUAL
} 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 ; } // calls deltree to deallocate memory avltree :: ~avltree( ) { deltree ( root ) ; } // deletes the tree void avltree :: deltree ( AVLNode *root ) { if ( root != NULL ) E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
68
ADVANCED DATA STRUCTURES – LAB MANUAL
{ 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 ) ; 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 ) ; } E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
69
ADVANCED DATA STRUCTURES – LAB MANUAL
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 32 AVL tree after deletion of a node: 5 6 10 13 15 25 27 Left rotation. AVL tree: 5 6 10 12 13 15 20
25
27
29
32
25
27
29
29
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 ( )
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
70
ADVANCED DATA STRUCTURES – LAB MANUAL
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 #include 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; 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; } E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
71
ADVANCED DATA STRUCTURES – LAB MANUAL
cout<<"Edges= "<temp.wt) { ed[j]=ed[j-1]; j--;} ed[j]=temp;}} initi(int A[20],int v) { int i; {for(i=1;i0) { i=A[i];} while(A[j]>0) { j=A[j];} if (i!=j) { A[j]=i; cout<<"\n"; cout<
Page
72
ADVANCED DATA STRUCTURES – LAB MANUAL
i=ed[edind].v1; j=ed[edind].v2; findunion(A,i,j); } while(eds>v; e=getedges(ed); sorted(ed,e); initi(A,v); cout<<"\nKruskals Spanning Tree Edge=\n"; krusk(ed,v); getch(); }
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 E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
73
ADVANCED DATA STRUCTURES – LAB MANUAL
2 Enter v1,v2,wt 34 35 5 Read n to stop n Edges= 3 Kruskals Spanning Tree Edge= 12to13 34to35 2to13
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 E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
74
ADVANCED DATA STRUCTURES – LAB MANUAL
9. end for 10. selected [i] = true, ne = 1 11. while (ne 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 #include const int MAX=5; struct lledge { 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
Page
75
ADVANCED DATA STRUCTURES – LAB MANUAL
for(i=0;inext; 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; } 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]
Page
76
ADVANCED DATA STRUCTURES – LAB MANUAL
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; 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"<
E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
77
ADVANCED DATA STRUCTURES – LAB MANUAL
OUTPUT: stree 1j->4 stree 2j->4 stree 3j->4 stree 4j->4 the min cost of spanning tree is 7 after deleting
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 #include #include 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; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
78
ADVANCED DATA STRUCTURES – LAB MANUAL
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"; } } 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"; E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
79
ADVANCED DATA STRUCTURES – LAB MANUAL
for(i=0;i<10;i++) { cout<>ch; switch(ch) { case 1: { cout<<"enter the size\n"; cin>>size; for(i=0;i>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); E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
80
ADVANCED DATA STRUCTURES – LAB MANUAL
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 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 E. Vijaya Kumar, M. Tech, M.I.S.T.E.
Page
81