71
1Page 1 of 71 Syllabus CS9215 DATA STRUCTURES LAB L T P C 0 0 3 2 1. Min Heap 2. Deaps 3. Leftist Heap 4. AVL Tree 5. B-Tree 6. Tries 7. Quick Sort 8. Convex hull 9. 0/1 Knapsack using Dynamic Programming 10. Graph coloring using backtracking

DS Lab pgm-M.E

Embed Size (px)

DESCRIPTION

ME 1ST SEM LAB

Citation preview

Page 1: DS Lab pgm-M.E

1Page 1 of 58

Syllabus

CS9215 DATA STRUCTURES LAB L T P C 0 0 3 2

1. Min Heap2. Deaps3. Leftist Heap4. AVL Tree5. B-Tree6. Tries7. Quick Sort8. Convex hull9. 0/1 Knapsack using Dynamic Programming10. Graph coloring using backtracking

Page 2: DS Lab pgm-M.E

1Page 2 of 58

1. MINHEAP

AIM:To implement the MIN HEAP structure with insert and delete minimum operating using

C++ program.

PROGRAM:

#include<iostream.h>#include<conio.h>class minheap{int size;int arr[50];public:void createheap();void insert();void removemin();void heap(int);void display();};

void minheap::createheap(){cout<<"\n Enter size of heap:";cin>>size;for(int i=1;i<=size;i++){cout<<"Enter element"<<i<<":";cin>>arr[i];}for(int j=(size/2);j>0;j--)heap(j);}

void minheap::heap(int t){int l=2*t;int r=2*t+1;int minidx=t;if(l<=size&&arr[l]<arr[minidx])minidx=l;if(r<=size&&arr[r]<arr[minidx])minidx=r;if(minidx!=t){

Page 3: DS Lab pgm-M.E

1Page 3 of 58

int temp=arr[minidx];arr[minidx]=arr[t];arr[t]=temp;heap(minidx);}}

void minheap::display(){cout<<"\nmin heap:";for(int i=1;i<=size;i++)cout<<arr[i]<<" ";}void minheap::insert(){cout<<"enter the element to be insert:";cin>>arr[++size];int node=size;while(node>0){if(arr[node/2]<arr[node])break;else{int temp=arr[node/2];arr[node/2]=arr[node];arr[node]=temp;}node=node/2;}}

void minheap::removemin(){cout<<"Element deleted is:"<<arr[1];arr[1]=arr[size--];heap(1);}

void main(){minheap h;int ch;clrscr();

Page 4: DS Lab pgm-M.E

1Page 4 of 58

cout<<"minheap:";h.createheap();cout<<"\n choice:";cout<<"\n 1.insert"<<"\n2.remove minimun"<<"\n 3.display"<<"\n4.exit";do{cout<<"\n\n Enter your choice:";cin>>ch;switch(ch){case 1:h.insert();break;case 2:h.removemin();break;case 3:h.display();break;}}while(ch<4);getch();}

Page 5: DS Lab pgm-M.E

1Page 5 of 58

OUTPUT:

MINHEAP:Enter size of heap:5Enter Element 1 :25Enter Element 2:63Enter Element 3:54Enter Element 4:39Enter Element 5:47

CHOICE:1. INSERT2. REMOVE MINIMUM3. DISPLAY4. EXIT

Enter your choice:3Min heap: 25 39 54 63 47

Enter your choice:1Enter the element to be insert:70

Enter your choice:3Min heap:25 39 54 63 47 70

Enter your choice:2Element deleted is:25

Enter your choice:3Min heap:39 63 54 70 47

Enter your choice:4

RESULT:Thus the program for MIN HEAP was written successfully.

Page 6: DS Lab pgm-M.E

1Page 6 of 58

2. DEAPSAIM:

To implement the DEAP structure with insert and delete minimum operating using C++ program

PROGRAM:

#include<iostream.h>#include<conio.h>#include<math.h>class deaps{public:int size;int arr[100];public:void createdeap();void deap(int);void swap(int,int);void mininsert(int);void maxinsert(int);void insert();void removemin(int);void removemax(int);void display();};void deaps::createdeap(){cout<<"\nEnter the no.of elements to be inserted:";cin>>size;cout<<"Enter "<<size<<" element(s):";size++;for(int i=2;i<=size;i++)cin>>arr[i];for(int j=3;j<=size;j++)deap(j);}void deaps::deap(int i){int power=0,key=i,partner,p=i;while(i>1){key=i;power++;

Page 7: DS Lab pgm-M.E

1Page 7 of 58

i/=2;}if(key==2){partner=p+(pow(2,power-1));partner/=2;if(arr[p]<arr[partner])mininsert(p);else{swap(p,partner);maxinsert(partner);}}if(key==3){partner=p-(pow(2,power-1));if(arr[p]>arr[partner])maxinsert(p);else{swap(p,partner);mininsert(partner);}}}void deaps::swap(int t1,int t2){int temp=arr[t1];arr[t1]=arr[t2];arr[t2]=temp;}void deaps::mininsert(int p){while(p/2>1){if(arr[p/2]>arr[p])swap(p,p/2);elsebreak;p/=2;}}void deaps::maxinsert(int p){while(p/2>1)

Page 8: DS Lab pgm-M.E

1Page 8 of 58

{if(arr[p/2]<arr[p])swap(p,p/2);elsebreak;p/=2;}}void deaps::display(){cout<<"\n\nDeap Elements: null";for(int i=2;i<=size;i++)cout<<" "<<arr[i];}void deaps::insert(){cout<<"\nEnter the element to be insert:";cin>>arr[++size];deap(size);

}void deaps::removemin(int i){int minidx=i;int lchild=2*i;int rchild=2*i+1;if(lchild<=size && arr[lchild]<arr[minidx])minidx=lchild;if(rchild<=size && arr[rchild]<arr[minidx])minidx=rchild;if(minidx!=i){swap(minidx,i);removemin(minidx);}

}void deaps::removemax(int i){int maxidx=i;int lchild=2*i;int rchild=2*i+1;if(lchild<=size && arr[lchild]>arr[maxidx])maxidx=lchild;if(rchild<=size && arr[rchild]>arr[maxidx])maxidx=rchild;

Page 9: DS Lab pgm-M.E

1Page 9 of 58

if(maxidx!=i){swap(maxidx,i);removemin(maxidx);}}void main(){deaps d;int ch;clrscr();cout<<"\n\t\tDEAPS"<<"\nCREATE DEAP";d.createdeap();cout<<"\n\nOPERATIONS:";cout<<"\n1.INSERT\n2.REMOVE MIN\n3.REMOVE MAX\n4.DISPLAY\n5.EXIT";do{cout<<"\nEnter your choice:";cin>>ch;switch(ch){case 1:d.insert();break;case 2:cout<<"\nRemoved Minimum Element:"<<d.arr[2];d.arr[2]=d.arr[d.size--];d.removemin(2);break;case 3:cout<<"\nRemoved Maximum Element:"<<d.arr[3];d.arr[3]=d.arr[d.size--];d.removemax(3);break;case 4:d.display();break;}}while(ch<5);getch();}

Page 10: DS Lab pgm-M.E

1Page 10 of 58

OUTPUT:

DEAPSCREATE DEAPEnter the no.of elements to be inserted:6Enter 6 element(s):2545108540

OPERATIONS:1.INSERT2.REMOVE MIN3.REMOVE MAX4.DISPLAY5.EXITEnter your choice:4

Deap Elements: null 5 45 8 10 25 40Enter your choice:1

Enter the element to be insert:15Enter your choice:4

Deap Elements: null 5 45 8 10 25 40 15Enter your choice:2

Removed Minimum Element:5Enter your choice:4

Deap Elements: null 8 45 15 10 25 40Enter your choice:3

Removed Maximum Element:45

RESULT:Thus the program for DEAP was written successfully.

Page 11: DS Lab pgm-M.E

1Page 11 of 58

3. LEFTIST HEAPAIM:

To implement the Leftist Heap using C++ program.

PROGRAM:

#include<iostream.h >#include<conio.h>struct node{int data;int npl;struct node*left;struct node*right;};class leftist{public:struct node*root;void insertheap();void insert(int);node* merge(node*&,node*&);void display(node*&);};void leftist::insertheap(){int val;cout<<"(Enter 0 to stop insertion)\n\n";do{cout<<"Enter value:";cin>>val;if(val!=0)insert(val);}while(val);}void leftist::insert(int val){node*newnode=new node;newnode->data=val;newnode->left=NULL;newnode->right=NULL;newnode->npl=0;if(root==NULL)root=newnode;elseroot=merge(root,newnode);

Page 12: DS Lab pgm-M.E

1Page 12 of 58

}node* leftist::merge(node*&l,node*&r){node*temp;if(r!=NULL){if(l->data>r->data){temp=l;l=r;r=temp;}if(l->right==NULL)l->right=r;elsel->right=merge(l->right,r);if(l->left==NULL||l->left->npl<l->right->npl){temp=l->left;l->left=l->right;l->right=temp;}if(l->right==NULL)l->npl=0;else{if(l->left->npl<l->right->npl)l->npl=l->left->npl+1;elsel->npl=l->right->npl+1;}}return l;}void leftist::display(node *&t){if(t!=NULL){cout<<t->data<<" ";display(t->left);display(t->right);}}void main(){clrscr();

Page 13: DS Lab pgm-M.E

1Page 13 of 58

leftist l;l.root=NULL;int val,ch=0;cout<<"\n\t\tLEFTIST HEAP";cout<<"\n1.CREATE MINLEFTIST HEAP";cout<<"\n2.INSERT\n3.DELETEMIN\n4.MERGE\n5.DISPLAY\n6.EXIT";do{cout<<"\n\nENTER YOUR CHOICE:";cin>>ch;switch(ch){case 1:l.insertheap();break;case 2:cout<<"\nEnter an element to be insert:";cin>>val;l.insert(val);break;case 3:cout<<"\nDeleted element is:"<<l.root->data;l.root=l.merge(l.root->left,l.root->right);break;case 4:cout<<"\nEnter heap to merge:";leftist l1;l1.root=NULL;l1.insertheap();l.root=l.merge(l.root,l1.root);break;case 5:cout<<"\nThe leftist heap is:";l.display(l.root);break;case 6:cout<<"\nEXIT";break;default:cout<<"\nInvalid choice:";break;}}while(ch!=6);getch();}

Page 14: DS Lab pgm-M.E

1Page 14 of 58

OUTPUT:

LEFTIST HEAP

1.CREATE MINLEFTIST HEAP2.INSERT3.DELETEMIN4.MERGE5.DISPLAY6.EXIT

ENTER YOUR CHOICE:1(Enter 0 to stop insertion)Enter value:2Enter value:7Enter value:50Enter value:11Enter value:13Enter value:80Enter value:0

ENTER YOUR CHOICE:5The leftist heap is:2 11 50 13 7 80

ENTER YOUR CHOICE:2Enter an element to be insert:15

ENTER YOUR CHOICE:5The leftist heap is:2 11 50 13 7 80 15

ENTER YOUR CHOICE:3Deleted element is:2

ENTER YOUR CHOICE:5The leftist heap is:7 11 50 13 15 80

ENTER YOUR CHOICE:4Enter heap to merge:(Enter 0 to stop insertion)Enter value:5Enter value:9Enter value:8Enter value:12Enter value:10Enter value:20Enter value:0

Page 15: DS Lab pgm-M.E

1Page 15 of 58

ENTER YOUR CHOICE:5The leftist heap is:5 7 11 50 13 15 9 20 80 8 12 10

ENTER YOUR CHOICE:6

RESULT:Thus the program for Leftist Heap was written successfully.

Page 16: DS Lab pgm-M.E

1Page 16 of 58

4. AVL TREEAIM:

To implement the AVL structure with insert and delete operating using C++ program.

PROGRAM:

#include<iostream.h>#include<conio.h>#include<math.h>template<class T1>class AVLnode{public:T1 element;AVLnode *left;AVLnode *right;int ht;friend class AVL<T1>;};template<class T1>class AVL{public:int height(AVLnode<T1>*&);void insert(T1 x,AVLnode<T1>*&);void build();void rotatewithleftchild(AVLnode<T1>*&);void rotatewithrightchild(AVLnode<T1>*&);T1 max(T1,T1);void traversal(AVLnode<T1>*&);void preorder(AVLnode<T1>*&);void doublerotationwithleft(AVLnode<T1>*&k3);void doublerotationwithright(AVLnode<T1>*&k3);};template<class T1>void AVL<T1>::build(){AVLnode<T1>*Root=NULL;int n,i;cout<<"\nEnter the no of elements to be inserted:";cin>>n;T1 x;for(i=1;i<=n;i++){cout<<"\nEnter the element to insert:";cin>>x;

Page 17: DS Lab pgm-M.E

1Page 17 of 58

insert(x,Root);}cout<<"\n\nAVL TREE(PREORDER):";preorder(Root);}template<class T1>int AVL<T1>::height(AVLnode<T1>*&Root){return(Root==NULL?-1:Root->ht);}template<class T1>void AVL<T1>::insert(T1 x,AVLnode<T1>*&Root){if(Root==NULL){Root=new AVLnode<T1>;Root->element=x;Root->left=NULL;Root->right=NULL;Root->ht=0;}else if(x<(Root)->element){insert(x,Root->left);if(height(Root->left)-height(Root->right)==2){if(x<Root->left->element){cout<<"\nUnbalanced occur at "<<Root->element;cout<<"\nPerforming Left-Left rotation\n";rotatewithleftchild(Root);}else{cout<<"\nUnbalanced occur at "<<Root->element;cout<<"\nPerforming Double rotation(Left-Right)\n";doublerotationwithleft(Root);}}}else if(x>Root->element){insert(x,Root->right);if(height(Root->right)-height(Root->left)==2){if(x>Root->right->element)

Page 18: DS Lab pgm-M.E

1Page 18 of 58

{cout<<"\nUnbalanced occur at "<<Root->element;cout<<"\nPerforming Right-Right rotation\n";rotatewithrightchild(Root);}else{cout<<"\nUnbalanced occur at "<<Root->element;cout<<"\nPerforming Double rotation(Right-Left)\n";doublerotationwithright(Root);}}}Root->ht=max(height(Root->left),height(Root->right))+1;}template<class T1>void AVL<T1>::rotatewithleftchild(AVLnode<T1>*&k2){AVLnode<T1>*k1=k2->left;k2->left=k1->right;k1->right=k2;k2->ht= max(height(k2->left),height(k2->right))+1;k1->ht= max(height(k1->left),height(k1->right))+1;k2=k1;}template<class T1>void AVL<T1>::doublerotationwithleft(AVLnode<T1>*&k3){rotatewithrightchild(k3->left);rotatewithleftchild(k3);}template<class T1>void AVL<T1>::doublerotationwithright(AVLnode<T1>*&k3){rotatewithleftchild(k3->right);rotatewithrightchild(k3);}template<class T1>void AVL<T1>::rotatewithrightchild(AVLnode<T1>*&k2){AVLnode<T1>*k1=k2->right;k2->right =k1-> left;k1-> left=k2;k2->ht= max(height(k2->left),height(k2->right))+1;k1->ht= max(height(k1->left),height(k1->right))+1;k2=k1;

Page 19: DS Lab pgm-M.E

1Page 19 of 58

}template<class T1>T1 AVL<T1>::max(T1 t1,T1 t2){return((t1>t2)?t1:t2);}template<class T1>void AVL<T1>::preorder(AVLnode<T1>*&Root){if(Root==NULL)return;else{cout<<Root->element<<" ";preorder(Root->left);preorder(Root->right);}}void main(){AVL <int> a;clrscr();cout<<"AVL TREE\n";cout<<"**********\n";a.build();getch();}

\

Page 20: DS Lab pgm-M.E

1Page 20 of 58

OUTPUT:

AVL TREE**********

Enter the no of elements to be inserted:8

Enter the element to insert:10

Enter the element to insert:2

Enter the element to insert:8

Unbalanced occur at 10Performing Double rotation(Left-Right)

Enter the element to insert:12Enter the element to insert:11

Unbalanced occur at 10Performing Double rotation(Right-Left)

Enter the element to insert:15

Unbalanced occur at 8Performing Right-Right rotation

Enter the element to insert:7

Enter the element to insert:9

AVL TREE(PREORDER):11 8 2 7 10 9 12 15

RESULT:Thus the program for AVL structure with insert and delete was written successfully.

Page 21: DS Lab pgm-M.E

1Page 21 of 58

5. B TREE AIM:

To implement the B TREE with insert and delete minimum operating using C++ program.

PROGRAM:

#include<iostream.h>#include<conio.h>#include<alloc.h>#include<process.h>const int MAX=4;const int MIN=2;struct btnode{int count;int value[MAX+1];btnode *child[MAX+1];};class btree{private:btnode *root;public:btree();void insert(int val);int setval(int val,btnode *n,int *p,btnode **c);static btnode *search(int val,btnode *root,int *pos);static int searchnode(int val,btnode *n,int *pos);void fillnode(int val,btnode *c,btnode *n,int k);void split(int val,btnode *c,btnode *n,int k,int *y,btnode **newnode);void del(int val);int delhelp(int val,btnode *root);void clear(btnode *root,int k);void copysucc(btnode *root,int i);void restore(btnode *root,int i);void rightshift(int k);void leftshift(int k);void merge(int k);void show();void get();void input();static void display(btnode *root);static void deltree(btnode *root);~btree();

Page 22: DS Lab pgm-M.E

1Page 22 of 58

};

btree::btree(){root=NULL;}

void btree::input(){int arr[100],n1;cout<<"Enter the no. of values to be inserted";cin>>n1;cout<<"Enter the values";for(int i=0;i<n1;i++){cin>>arr[i];insert(arr[i]);}}

void btree::insert(int val){int i;btnode *c,*n;int flag;flag=setval(val,root,&i,&c);if(flag){n=new btnode;n->count=1;n->value[1]=i;n->child[0]=root;n->child[1]=c;root=n;}}int btree::setval(int val,btnode *n,int *p,btnode **c){int k;

if(n==NULL){*p=val;*c=NULL;return 1;

Page 23: DS Lab pgm-M.E

1Page 23 of 58

}else{if(searchnode(val,n,&k))cout<<endl<<"key value already exists"<<endl;if(setval(val,n->child[k],p,c)){if(n->count<MAX){fillnode(*p,*c,n,k);return 0;}else{split(*p,*c,n,k,p,c);return 1;}}

return 0;}}btnode *btree::search(int val,btnode *root,int *pos){if(root==NULL)return NULL;else{if(searchnode(val,root,pos))return root;elsereturn search(val,root->child[*pos],pos);}}

int btree::searchnode(int val,btnode *n,int *pos){if(val<n->value[1]){*pos=0;return 0;}else{*pos=n->count;while((val<n->value[*pos])&&*pos>1)

Page 24: DS Lab pgm-M.E

1Page 24 of 58

(*pos)--;if(val==n->value[*pos])return 1;elsereturn 0;}}void btree::fillnode(int val,btnode *c,btnode *n,int k){int i;for(i=n->count;i>k;i--){n->value[i+1]=n->value[i];n->child[i+1]=n->child[i];}n->value[k+1]=val;n->child[k+1]=c;n->count++;}void btree::split(int val,btnode *c,btnode *n,int k,int *y,btnode **newnode){int i,mid;if(k<=MIN)mid=MIN;elsemid=MIN+1;*newnode=new btnode;

for(i=mid+1;i<=MAX;i++){(*newnode)->value[i-mid]=n->value[i];(*newnode)->child[i-mid]=n->child[i];}(*newnode)->count=MAX-mid;n->count=mid;if(k<=MIN)fillnode(val,c,n,k);elsefillnode(val,c,*newnode,k-mid);*y=n->value[n->count];(*newnode)->child[0]=n->child[n->count];n->count--;}void btree::get(){

Page 25: DS Lab pgm-M.E

1Page 25 of 58

int ch,ch1;cout<<"\n\t\t Btree \n \n 1:insertion \n 2:deletion \n 3:exit";while(1){cout<<"\n enter the operation:";cin>>ch;switch(ch){case 1:input();cout<<"\n Elements in the Btree:";show();break;case 2:cout<<"\n Enter the value to be deleted:";cin>>ch1;del(ch1);cout<<"\n Elements in the Btree:";show();break;case 3:exit(1);break;default:break;}}}void btree::del(int val){btnode *temp;if(!delhelp(val,root))cout<<endl<<"\n value"<<val<<"not found";else{if(root->count==0){temp=root;root=root->child[0];delete temp;}}}int btree::delhelp(int val,btnode *root){int i;

Page 26: DS Lab pgm-M.E

1Page 26 of 58

int flag;if(root==NULL)return 0;else{flag=searchnode(val,root,&i);if(flag){if(root->child[i-1]){copysucc(root,i);flag=delhelp(root->value[i],root->child[i]);if(!flag)cout<<endl<<"value"<<val<<"not found";}

elseclear(root,i);}elseflag=delhelp(val,root->child[i]);if(root->child[i]!=NULL){if(root->child[i]->count<MIN)restore(root,i);}return flag;}}void btree::clear(btnode *root,int k){int i;for(i=k+1;i<=root->count;i++){root->value[i-1]=root->value[i];root->child[i-1]=root->child[i];}root->count--;}void btree::copysucc(btnode *root,int i){btnode *temp=root->child[i];while(temp->child[0])temp=temp->child[0];root->value[i]=temp->value[1];

Page 27: DS Lab pgm-M.E

1Page 27 of 58

}void btree::restore(btnode *root,int i){if(i==0){if(root->child[1]->count>MIN)leftshift(1);elsemerge(1);}

else{if(i==root->count){if(root->child[i-1]->count>MIN)rightshift(i);elsemerge(i);}else{if(root->child[i+1]->count>MIN)rightshift(i);else{if(root->child[i+1]->count>MIN)leftshift(i+1);elsemerge(i);}}}}void btree::rightshift(int k){int i;btnode *temp;temp=root->child[k];for(i=temp->count;i>0;i--){temp->value[i+1]=temp->value[i];temp->child[i+1]=temp->child[i];}temp->child[1]=temp->child[0];temp->count++;temp->value[1]=root->value[k];temp=root->child[k-1];root->value[k]=temp->value[temp->count];

Page 28: DS Lab pgm-M.E

1Page 28 of 58

root->child[k]->child[0]=temp->child[temp->count];temp->count--;}

void btree::leftshift(int k){btnode *temp;temp=root->child[k-1];temp->count++;temp->value[temp->count]=root->value[k];temp->child[temp->count]=root->child[k]->child[0];temp=root->child[k];root->value[k]=temp->value[1];temp->child[0]=temp->child[1];temp->count--;for(int i=1;i<=temp->count;i++){temp->value[i]=temp->value[i+1];temp->child[i]=temp->child[i+1];}}

void btree::merge(int k){btnode *temp1,*temp2;temp1=root->child[k];temp2=root->child[k+1];temp2->count++;temp2->value[temp2->count]=root->value[k];temp2->child[temp2->count]=root->child[0];for(int i;i<=temp1->count;i++){temp2->count++;temp2->value[temp2->count]=temp1->value[i];temp2->child[temp2->count]=temp1->child[i];}for(i=k;i<root->count;i++){root->value[i]=root->value[i+1];root->child[i]=root->child[i+1];}root->count--;delete temp1;}

Page 29: DS Lab pgm-M.E

1Page 29 of 58

void btree::show(){display(root);}

void btree::display(btnode *root){if(root!=NULL){for(int i=0;i<root->count;i++){display(root->child[i]);cout<<root->value[i+1]<<"\t";}display(root->child[i]);}}void btree::deltree(btnode *root){if(root!=NULL){for(int i=0;i<root->count;i++){deltree(root->child[i]);deltree(root->child[i]);}deltree(root->child[i]);delete(root->child[i]);}}

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

void main(){clrscr();btree b;/*int sz=sizeof(arr)/sizeof(int);for(int i=0;i<sz;i++)b.insert(arr[i]);cout<<"B-tree of order 5:"<<endl;b.show();cout<<"\n\n B-tree after deletion of values:"<<endl;

Page 30: DS Lab pgm-M.E

1Page 30 of 58

b.show();*/b.get();cout<<"\n \n B-tree after deletion of values:"<<endl;b.show();getch();}

Page 31: DS Lab pgm-M.E

1Page 31 of 58

OUTPUT: Btree

1:insertion2:deletion3:exitenter the operation:1Enter the no. of values to be inserted4Enter the values3619

Elements in the Btree:1 3 6 9Enter the operation:2

Enter the value to be deleted:3

Elements in the Btree:1 6 9

Enter the operation:1Enter the no. of values to be inserted1

Enter the values7Elements in the Btree:1 6 7 9

Enter the operation:3

RESULT:Thus the program for B TREE was written successfully.

Page 32: DS Lab pgm-M.E

1Page 32 of 58

6. TRIESAIM:

To implement the Tries using C++ program.

PROGRAM:

#include<iostream.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#include<dos.h>#include<alloc.h>#include<conio.h>struct data{char word[20];char meaning[50];};struct trie{struct trie *child[26];struct data *ptr;};struct trie *insert(struct trie*node,char *val,int len,int count){if(node==NULL){node=(struct trie*)malloc(sizeof(struct trie));for(int i=0;i<26;i++){node->child[i]=NULL;}}if(count<len){int temp=(int)(*(val+count))-65;node->child[temp]=insert(node->child[temp],val,len,(count+1));node->ptr=NULL;}else{node->ptr=(struct data*)malloc(sizeof(struct data));strcpy(node->ptr->word,val);

Page 33: DS Lab pgm-M.E

1Page 33 of 58

cout<<"Enter Meaning:";cin>>node->ptr->meaning;}return node;}struct trie *search(struct trie *node,char *val,int len,int count){if(node==NULL){cout<<"\n!!Word does not exist!!";}else if(count<len){int temp=(int)(*(val+count))-65;node->child[temp]=search(node->child[temp],val,len,(count+1));}else if(node->ptr==NULL){cout<<"\n!!Word does not exist!!";}else{cout<<"\n\!!Word Exists!!";cout<<"\nWord:"<<node->ptr->word;cout<<"\nMeaning:"<<node->ptr->meaning;}getch();return node;}struct trie *del(struct trie *node,char *val,int len,int count){if(node==NULL){cout<<"\n!!Word does not exist!!";}else if(count<len){int temp=(int)(*(val+count))-65;node->child[temp]=del(node->child[temp],val,len,(count+1));}else if(node->ptr==NULL){cout<<"\n!!Word does not exist!!";}else{delete node->ptr;

Page 34: DS Lab pgm-M.E

1Page 34 of 58

node->ptr=NULL;cout<<"\n!!Word Deleted!!";}getch();return node;}/*struct trie *display(struct trie *node){if(node->ptr!=NULL){cout<<"\nWord:"<<node->ptr->word;cout<<"\n Meaning:"<<node->ptr->meaning;return node;}else{clrscr();gotoxy(23,16);cout<<"DICTIONARY EMPTY";return node;}for(int i=0;i<26;i++){if(node->child[i]!=NULL)display(node->child[i]);else{clrscr();gotoxy(23,16);cout<<"DICTIONARY EMPTY";return node;}}return node;} */void prog(){char val[26],ch;int i;struct trie *root=NULL;do{clrscr();textmode(C80);gotoxy(30,5);textcolor(12);

Page 35: DS Lab pgm-M.E

1Page 35 of 58

cout<<"THE COMPUTER DICTIONARY";gotoxy(23,8);textcolor(15);cout<<"1.Insert an Element";gotoxy(23,10);cout<<"2.Search a Node";gotoxy(23,12);cout<<"3.Delete a Node";gotoxy(23,14);cout<<"4.Exit";gotoxy(23,18);cout<<"Enter your Choice:" ;cin>>ch;switch(ch){case'1':clrscr();cout<<"\n\nEnter Word:";cin>>val;i=strlen(val);root=insert(root,val,i-1,0);break;case'2':clrscr();cout<<"\n\nEnter the Word to Search:";cin>>val;i=strlen(val);root=search(root,val,i-1,0);break;case'3':clrscr();cout<<"\n\n Enter the Wordto Delete:";cin>>val;i=strlen(val);root=del(root,val,i-1,0);break;/*case'4':clrscr();cout<<"THE WORDS IN DICTIONARY ARE";root=display(root);break;*/case'4':clrscr();textcolor(2);gotoxy(26,10);cout<<"QUITING THE DICTIONARY";

Page 36: DS Lab pgm-M.E

1Page 36 of 58

for(int i=0;i<=3;i++){cout<<".";delay(250);}return;default:clrscr();cout<<"\n\n!!WRONG CHOICE!!\n";}}while(1);}void intro(){textmode(C80);int i;clrscr();for(i=0;i<=50;i++){if((15+i)%2==0){textcolor(4);gotoxy(30,5);cout<<"DICTIONARY LOADING";}else{gotoxy(30,5);cout<<".";}gotoxy(15+i,13);textcolor(5);cout<<"**>";gotoxy(32,22);cout<<2*i<<"%completed";delay(100);}delay(1000);clrscr();gotoxy(26,10);cout<<"DICTIONARY LOADING COMPLETED";delay(450);clrscr();}void main()

Page 37: DS Lab pgm-M.E

1Page 37 of 58

{clrscr();intro();prog();}

Page 38: DS Lab pgm-M.E

1Page 38 of 58

OUTPUT: THE COMPUTER DICTIONARY

1.Insert an Element

2.Search a Node

3.Delete a Node

4.Exit

Enter your Choice:1

Enter Word:exultantEnter Meaning:rejoicing

THE COMPUTER DICTIONARY

1.Insert an Element

2.Search a Node

3.Delete a Node

4.Exit

Enter your Choice:2

Enter the Word to Search:exultant

!!Word Exists!!Word:exultantMeaning:rejoicing

Page 39: DS Lab pgm-M.E

1Page 39 of 58

Enter the Word to Delete:exultant

!!Word Deleted!!

THE COMPUTER DICTIONARY

1.Insert an Element

2.Search a Node

3.Delete a Node

4.Exit

Enter your Choice:4

QUITING THE DICTIONARY....

RESULT:Thus the program for Tries was written successfully.

Page 40: DS Lab pgm-M.E

1Page 40 of 58

7. QUICK SORTAIM:

To implement the Quick Sort using C++ program.

PROGRAM:

#include<iostream.h>#include<conio.h>class sort{int arr[100];int num;public:void get_data();void quick(int x[],int p,int q);int partition(int x[],int lb,int ub);void display();};void sort::get_data(){clrscr();cout<<"number of elements";cin>>num;cout<<endl<<"elements";for(int i=0;i<num;i++)cin>>arr[i];quick(arr,0,num);}void sort::quick(int x[],int lb,int ub){int j;if(lb>=ub)return;j=partition(x,lb,ub);quick(x,lb,j-1);quick(x,j+1,ub);}int sort::partition(int x[],int lb,int ub){int a,down,up,temp;a=x[lb];up=ub;down=lb;while(down<up){while(x[down]<=a&&down<ub)

Page 41: DS Lab pgm-M.E

1Page 41 of 58

down++;while(x[up]>a)up--;if(down<up){temp=x[down];x[down]=x[up];x[up]=temp;}}x[lb]=x[up];x[up]=a;return up;}void sort::display(){cout<<endl<<"sorted array";for(int i=0;i<num;i++)cout<<arr[i]<<" ";}void main(){sort s;s.get_data();s.display();getch();}

Page 42: DS Lab pgm-M.E

1Page 42 of 58

OUTPUT:

Number of elements7elements44881201526476Sorted array01 124452647688

RESULT:Thus the program for Quick Sort was written successfully.

Page 43: DS Lab pgm-M.E

1Page 43 of 58

8. CONVEX HULLAIM:

To implement the using C++ program.

PROGRAM:

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

class convexhull{int x[50],y[50],n,status[50];public:void insert();void sort();void swap(int,int);void check(int,char);void display();};void convexhull::insert(){cout<<"\nEnter number of points:";cin>>n;cout<<"\nEnter x and y coordinates for ";for(int i=0;i<n;i++){cout<<"Point "<<i+1<<"\n";cin>>x[i];cin>>y[i];status[i]=0;}sort();check(0,'L');check(0,'H');display();}void convexhull::sort(){for(int i=0;i<n-1;i++){for(int j=i+1;j<n;j++)if((x[i]>x[j]) || ((x[i]==x[j]) && (y[i]>y[j])))swap(i, j);}

Page 44: DS Lab pgm-M.E

1Page 44 of 58

}void convexhull::swap(int i,int j){int temp=x[i];x[i]=x[j];x[j]=temp;temp=y[i];y[i]=y[j];y[j]=temp;}void convexhull::display(){cout<<"Boundary points are";for(int i=0;i<n;i++)if(status[i]==1)cout<<"("<<x[i]<<", "<<y[i]<<")";}void convexhull::check(int p,char c){double slope=0,degree=0,deg=0;int next=0;status[p]=1;for(int i=p+1;i<n;i++){if(y[i]-y[p]){slope=(double)(x[i]-x[p])/(double)(y[i]-y[p]);degree=atan(slope);if(degree < 0)degree+=180;}elsedegree=90;if(i==p+1){deg=degree;next=i;}else{if((c=='L' && deg>degree)||(c!='L' && deg<degree)||(degree==deg && x[i]<x[next])){deg=degree;next=i;}}

Page 45: DS Lab pgm-M.E

1Page 45 of 58

}if(next!=0)check(next,c);}void main(){clrscr();convexhull c;cout<<"\nCOVEX HULL";cout<<"\n**********\n";c.insert();getch();}

Page 46: DS Lab pgm-M.E

1Page 46 of 58

OUTPUT:

COVEX HULL**********

Enter number of points:7

Enter x and y coordinates for Point 136Point 223Point 334Point 433Point 522Point 661Point 731Boundary points are(2, 2)(2, 3)(3, 1)(3, 6)(6, 1)

RESULT:Thus the program for Covex Hull hs written successfully.

Page 47: DS Lab pgm-M.E

1Page 47 of 58

9. 0/1 KNAPSACK USING DYNAMIC PROGRAMMINGAIM:

To implement the 0/1 knapsack using dynamic programming using C++ program.

PROGRAM:

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

class knapsack{public:int weight;int profit;};void main(){clrscr();int n,w,i,j;int p[10][100]={0};knapsack *k=new knapsack[n+1];cout<<"\n\tKNAPSACK USING DYNAMIC PROGRAMMING";cout<<"\n\t**********************************";cout<<"\nEnter the number of objects:";cin>>n;cout<<"\nEnter the maximum weight sack can take:";cin>>w;for(i=1;i<=n;i++){cout<<"\nFor object "<<i;cout<<":\nEnter profit: ";cin>>k[i].profit;cout<<"Enter Weight: ";cin>>k[i].weight;}for(i=1;i<=n;i++)for(j=1;j<=w;j++){int option1=p[i-1][j];int option2=-1;if(k[i].weight<=j)option2=k[i].profit+p[i-1][j-k[i].weight];if(option1>option2)

Page 48: DS Lab pgm-M.E

1Page 48 of 58

p[i][j]=option1;elsep[i][j]=option2;}cout<<"\n\nProfit table:";cout<<"\n*************";cout<<"\n\t\tCapacity\n";for(i=1;i<=w;i++)cout<<"\t"<<i;cout<<"\nItem";for(i=1;i<=n;i++){cout<<"\n"<<i;for(j=1;j<=w;j++)cout<<"\t"<<p[i][j];cout<<"\n";}i=n;cout<<"\n\nprofit="<<p[n][w];cout<<"\n\nItem\tWeight\tProfit";while(w>0){while(p[i][w]==p[i][w-1])w--;while(p[i][w]==p[i-1][w])i--;cout<<"\n"<<i<<"\t"<<k[i].weight<<"\t"<<k[i].profit;w=w-k[i].weight;i--;}getch();}

Page 49: DS Lab pgm-M.E

1Page 49 of 58

OUTPUT: KNAPSACK USING DYNAMIC PROGRAMMING **********************************Enter the number of objects:4Enter the maximum weight sack can take:16For object 1:Enter profit: 40Enter Weight: 2

For object 2:Enter profit: 30Enter Weight: 5

For object 3:Enter profit: 50Enter Weight: 10

For object 4:Enter profit: 10Enter Weight: 5Profit table:*************Capacity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Iitem1 0 40 40 40 40 40 40 40 40 40 40 40 40 40 40 402 0 40 40 40 40 40 70 70 70 70 70 70 70 70 70 703 0 40 40 40 40 40 70 70 70 70 70 90 90 90 90 904 0 40 40 40 40 40 70 70 70 70 70 90 90 90 90 90

profit=90

Item Weight Profit3 10 501 2 40

RESULT:Thus the program for 0/1 knapsack using dynamic programmingwas written

successfully.

Page 50: DS Lab pgm-M.E

1Page 50 of 58

10. GRAPH COLORING USING BACKTRACKINGAIM:

To implement the Graph Coloring using Backtracking with C++ program.

PROGRAM:

#include<iostream.h>#include<conio.h>#include<math.h>#define N 5#define M 3int G[N][N];int x[N];void NextValue(int k){do{x[k]=(x[k]+1)%(M+1);if(x[k]==0)return;for(int j=0;j<N;j++){if((G[k][j]!=0)&&(x[k]==x[j])){break;}}if(j==N)return;}while(!0);}

void display(){for(int i=0;i<N;i++)cout<<x[i]<<"\t";cout<<"\n";}

void M_coloring(int k){do{NextValue(k);if(x[k]==0)return;

Page 51: DS Lab pgm-M.E

1Page 51 of 58

if(k==N-1)display();elseM_coloring(k+1);}while(!0);}

void main(){clrscr();cout<<"\nEnter the Graph(if edge exists|yes=1.no=0)\n";for(int i=0;i<N;i++)for(int j=0;j<N;j++){if((i!=j)&&(i<j)){cout<<i+1<<"and"<<j+1<<":";cin>>G[i][j];G[j][i]=G[i][j];}if(i==j){G[i][j]=0;}}cout<<"\n **********************************\n";cout<<"\n\n Various possibilities of graph coloring";cout<<"\n\nc(v1)\tc(v2)\tc(v3)\tc(v4)\tc(v5)\n";M_coloring(0);cout<<"\n ***********************************\n";getch();}

Page 52: DS Lab pgm-M.E

1Page 52 of 58

OUTPUT:

Enter the Graph(if edge exists|yes=1.no=0)1and2:11and3:11and4:11and5:02and3:02and4:12and5:13and4:13and5:14and5:0

**********************************

Various possibilities of graph coloring

c(v1) c(v2) c(v3) c(v4) c(v5)1 2 2 3 11 2 2 3 31 3 3 2 11 3 3 2 22 1 1 3 22 1 1 3 3

RESULT:Thus the program for Graph Coloring using Backtracking was written successfully.

Page 53: DS Lab pgm-M.E

1Page 53 of 58

8 – QUEENS PROBLEM

Aim:To implement the 8 – Queens problem using Backtracking with C++ program.

PROGRAM :

#include<iostream.h>#include<conio.h>#include<math.h>#include<stdio.h>#include<graphics.h>#include<stdlib.h>int *x;int place(int k,int i){for(int j=1;j<k;j++){if((x[j]==i)||((abs(x[j]-i))==abs(j-k)))return 0;}return 1;}void nqueens(int k,int n){for(int i=1;i<=n;i++){if(place(k,i)){x[k]=i;if(k==n){clrscr();int j,k,m,s,u[20][20],v[20][20];m=100;s=100;cout<<n<<" QUEEN'S PROBLEM";for(j=1;j<=n;j++){for(k=1;k<=n;k++){m=m+40;rectangle(m,s,m+40,s+40);u[k][j]=m+20;v[k][j]=s+20;}

Page 54: DS Lab pgm-M.E

1Page 54 of 58

m=100;s=s+40;}for(j=1;j<=n;j++){for(k=1;k<=n;k++){if(x[j]==k)outtextxy(u[j][k],v[j][k],"Q"); //To display text}}getch();}else{nqueens(k+1,n);}}}}void main(){int k,n;clrscr();int gdriver = DETECT, gmode, errorcode;int xmax, ymax;initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}cout<<"Enter the no. of queens :";cin>>n;k=1;nqueens(k,n);getch();}

Page 55: DS Lab pgm-M.E

1Page 55 of 58

OUTPUT :

Enter the no. of queens: 4

4 QUEENS PROBLEM

Q

Q

Q

Q

RESULT:Thus the program for 8 Queens problem using Backtracking was written successfully.

Page 56: DS Lab pgm-M.E

1Page 56 of 58

BINARY SEARCH TREE

AIM:To implement the Binary Search Tree using C++ program.

PROGRAM :

#include <iostream.h>class btree{private :struct node{node *left ;char data ;node *right ;} *root ;char *arr ;int *lc ;int *rc ;public :btree ( char *a, int *l, int *r, int size ) ;void insert ( int index ) ;static node* buildtree ( char *a, int *l, int *r, int index ) ;void display( ) ;static void inorder ( node *sr ) ;~btree( ) ;static void del ( node *sr ) ;} ;btree :: btree ( char *a, int *l, int *r, int size ){root = NULL ;arr = new char[size] ;lc = new int[size] ;rc = new int[size] ;for ( int i = 0 ; i < size ; i++ ){* ( arr + i ) = * ( a + i ) ;* ( lc + i ) = * ( l + i ) ;* ( rc + i ) = * ( r + i ) ;}}void btree :: insert ( int index ){

root = buildtree ( arr, lc, rc, index ) ;

Page 57: DS Lab pgm-M.E

1Page 57 of 58

}node* btree :: buildtree ( char *a, int *l, int *r, int index ){node *temp = NULL ;if ( index != -1 ){

temp = new node ;temp -> left = buildtree ( a, l, r, * ( l + index ) ) ;temp -> data = * ( a + index ) ;temp -> right = buildtree ( a, l, r, * ( r + index ) ) ;}return temp ;

}void btree :: display( ){inorder(root) ;}void btree :: inorder(node *sr){

if ( sr != NULL ){inorder ( sr -> left ) ;cout << sr -> data << "\t" ;inorder ( sr -> right ) ;}

}btree :: ~btree( ){

delete arr ;delete lc ;delete rc ;del ( root ) ;

}void btree :: del ( node *sr ){

if ( sr != NULL ){

del ( sr -> left ) ;del ( sr -> right ) ;

}delete sr ;

}void main( ){

char a[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' } ;int l[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 } ;

Page 58: DS Lab pgm-M.E

1Page 58 of 58

int r[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 } ;int sz = sizeof ( a ) ;btree bt ( a, l, r, sz ) ;bt.insert( 0 ) ;cout << "\nIn-order Traversal: " << endl ;bt.display( ) ;

}

OUTPUT :

In Order Traversal

D B H E A F C G

Result : Thus the program for binary search tree was written successfully.