Top Banner
EX.NO:1 MIN HEAP DATE: AIM : To write a C++ program for the implementing the min heap structure with insert and delete minimum operation. ALGORITHM: Step 1: Start the program by creating function with min heap property. Step 2: Two functions namely insert () and deletemin() are created. Step 3: The insert () is used to insert new element in the tree structure with heap property. Step 4: The deletemin() is used to delete the minimum element which is usually a root node. Step 5: The two operations are performed satisfying heapness and completeness property. Step 6: End of the program. 1
72

dslab own record

Apr 13, 2015

Download

Documents

record
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: dslab own record

EX.NO:1 MIN HEAP

DATE:

AIM :

To write a C++ program for the implementing the min heap structure with insert and delete

minimum operation.

ALGORITHM:

Step 1: Start the program by creating function with min heap property.

Step 2: Two functions namely insert () and deletemin() are created.

Step 3: The insert () is used to insert new element in the tree structure with heap property.

Step 4: The deletemin() is used to delete the minimum element which is usually a root node.

Step 5: The two operations are performed satisfying heapness and completeness property.

Step 6: End of the program.

1

Page 2: dslab own record

PROGRAM:

#include<iostream.h>#include<conio.h>class minheap{

int size,l;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<<"\n";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+l;int minidx=t;if(l<size && arr[l]<arr[minidx])

minidx=l;if(r<size && arr[r]<arr[minidx])

minidx=r;if(minidx!=t){

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

}}void minheap::display(){

cout<<"\n Min heap:";for(int i=1;i<=size;i++)

cout<<arr[i]<<" ";}void minheap::insert(){

2

Page 3: dslab own record

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(); cout<<"MINHEAP:"; h.createheap(); cout<<"\n CHOICE:"; cout<<"\n 1.Insert"<<"\n 2.Remove minimum"<<"\n 3.Display"<<"\n 4.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();

}

3

Page 4: dslab own record

OUTPUT:

MINHEAP:Enter size of heap: 5Enter element1: 2Enter element2: 6Enter element3: 5Enter element4: 3Enter element5: 4

CHOICE: 1. Insert 2. Remove minimum 3. Display 4. Exit Enter your choice: 3 Min heap: 2 3 5 6 4

Enter your choice: 2Element deleted is: 2

Enter your choice: 3Min heap: 3 6 5 7 4

Enter your choice: 4

4

Page 5: dslab own record

RESULT:

Thus the C++ program for Min heap with insert and delete minimum operation has been implemented and executed successfully.

5

Page 6: dslab own record

EX.NO:2 DEAPS

DATE:

AIM:

To write a C++ program for implementing deaps structure with insert and delete operations.

ALGORITHM:

Step 1: Start the program by creating Deap Structure.

Step 2: Perform insert and delete functions.

Step 3: The insert () is done with 2 methods namely maxinsert () and mininsert ().

Step 4: The delete () is done with 2 methods namely deletemax () and deletemin ().

Step 5: The left Child and right Child are compared and the appropriate element is

placed in the root node.

Step 6: After the insert and delete operation Deap elements are displayed.

Step 7: End of the program.

6

Page 7: dslab own record

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

}7

Page 8: dslab own record

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

break;p/=2;}

}void deaps::maxinsert(int p){

while(p/2>1){

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<<"\n Enter the element to be inserted:";cin>>arr[++size];deap(size);

}void deaps::removemin(int i)

8

Page 9: dslab own record

{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;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.REMOVEMIN\n3.REMOVEMAX\n4.DISPLAY\n5.EXIT";do{

cout<<"\nEnter your choice:";cin>>ch;switch(ch){

case 1:d.insert();break;

case 2:cout<<"\nThe minimum element removed is:"<<d.arr[2];d.arr[2]=d.arr[d.size--];d.removemin(2);break;

case 3:cout<<"\nThe maximum element removed is:"<<d.arr[3];

9

Page 10: dslab own record

d.arr[3]=d.arr[d.size--];d.removemax(3);break;

case 4:d.display();break;

}}while(ch<5);getch();

}

10

Page 11: dslab own record

OUTPUT:

DEAPSCREATE DEAPSEnter the no. of elements to be inserted: 6Enter 6 elements: 2545108540

OPERATIONS:1. INSERT2. REMOVE MIN3. REMOVE MAX4. DISPLAY5. EXIT

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

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

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

Enter your choice: 2The Minimum element removed is: 5

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

Enter your choice: 3The Maximum Element removed is: 45

Enter your choice: 4Deap Element: null 8 40 15 10 25

Enter your choice: 5

11

Page 12: dslab own record

RESULT :

Thus the C++ program for deaps structure with insert and delete operations has been implemented

and executed successfully.

12

Page 13: dslab own record

EX.NO.3 LEFTIST HEAP

DATE:

AIM:

To write a C++ program for implementing the leftist heap with insert and delete min operation.

ALGORITHM:

Step 1: Start the program by defining function

Step 2: We know heap as the root node with minimum element.

Step 3: The insert and delete operations are performed with the help of combining 2 trees

Step 4: The insert operation is performed by combining the two leftist trees.

Step 5: The deletemin () is used to delete the minimum element in the heap.

Step 6: After the insert and delete operations leftist heap elements are displayed.

Step 7: End of the program.

13

Page 14: dslab own record

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

root=merge(root,newnode);}node* leftist::merge(node*&l,node*&r){

node*temp;if(r!=NULL){

if(l->data>r->data){

14

Page 15: dslab own record

temp=l;l=r;r=temp;

}if(l->right==NULL)

l->right=r;else

l->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();leftist l;l.root=NULL;int val,ch=0;cout<<"\n\t\t LEFTIST HEAP";cout<<"\n1.CREATE MINLEFISTIST HEAP";cout<<"\n2.INSERT\n3.DELETE\n4.MERGE\n5.DISPLAY\n6.EXIT";do{

cout<<"\n\nENTER YOUR CHOICE:";cin>>ch;switch(ch){

case 1:l.insertheap();break;

case 2:

15

Page 16: dslab own record

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

}

16

Page 17: dslab own record

OUTPUT:

LEFTIST HEAP1. CREATE MINLEFISTIST HEAP2. INSERT3. DELETE4. 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

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

ENTER YOUR CHOICE: 6

17

Page 18: dslab own record

RESULT:

Thus the C++ program for leftist heap with insert and deletemin operation has been implemented

and executed successfully.

18

Page 19: dslab own record

EX.NO:4 AVL TREE

DATE:

AIM:

To write a C++ program for implementing the AVL tree with insert & delete operations.

ALGORITHM:

Step 1: Start the program by defining the functions.

Step 2: Insert the elements to the AVL tree.

Step 3: Check the tree if it is balanced or not.

Step 4: The balance factor is one of 0, 1 and -1.

Step 5: If it is not balanced, balance the tree using

(i) Left-left

(ii) Left-right

(iii) Right-left

(iv) Right-right

Balancing

Step 6: And if the tree is balanced and then the insert () and the delete () operations are performed.

Step 7: End of the program.

19

Page 20: dslab own record

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 no of elements to be inserted:”;cin≫n;T1 x;for (i=1;i≤n;i++){ cout≪”\nEnter the element to insert:”;

cin≫x;insert(x,Root);

}cout≪”\n\n AVL TREE (PREORDER):”;preorder(Root);

}template<class T1>int AVL<T1>∷height(AVLnode<T1>*&Root){

return(Root==NULL?-1:Root→ht);}template<class T1>

20

Page 21: dslab own record

void AVL<T1>∷insert(T1 x,AVLnode<T1>*&Root){

if(Root==NULL) {

Root=nsew 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≪”\n Unbalanced occur at”≪Root→element; cout≪”\n Performing left-left rotation\n”; rotatewithleftchild(Root); } else { cout≪”\n Unbalanced occur at”≪Root→element; cout≪”\n Performing 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) { cout≪”\n Unbalanced occur at”≪Root→element; cout≪”\n Performing right-right rotation\n”; rotatewithrightchild(Root); } else { cout≪”\n Unbalanced occur at”≪Root→element; cout≪”\n Performing 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)

21

Page 22: dslab own record

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

22

Page 23: dslab own record

a.build();getch();}

OUTPUT:

AVL TREE*************Enter the no of elements to be inserted: 8

Enter the element to insert: 10Enter the element to insert: 2Enter the element to insert: 8Unbalanced occur at 10

Performing Double rotation (Left-Right)

Enter the element to insert: 12Enter the element to insert: 11Unbalanced occur at 10

Performing Double rotation (Right-Left)

Enter the element to insert: 15Unbalanced occur at 8

Performing Right-Right rotation

Enter the element to insert: 7Enter the element to insert: 9

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

23

Page 24: dslab own record

RESULT:

24

Page 25: dslab own record

Thus the C++ program for AVL tree with insert and delete operations has been implemented and executed successfully.

EX.NO:5 BTREE

DATE:

AIM:

To write a C++ program for implementing of the B-tree with insert and delete operations.

ALGORITHM:

Step 1: Start the program by defining function.

Step 2: Declare the class B-tree.

Step 3: The insert and delete operations are performed.

Step 4: To insert, check if root is empty, if it is empty insert the element as root.

Step 5: If it is greater insert it into right sub tree.

Step 6: Otherwise, insert it into left sub tree.

Step 7: Use the function split, to split the nodes.

Step 8: Call the function display to display data1, data2, address and parent.

Step 9: End of the program.

25

Page 26: dslab own record

PROGRAM:

#include<iostream.h>#include<stdlib.h>#include<alloc.h>#include<conio.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, btode *c, btnode *n, int k); void split(int val, btode *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(); static void display(btnode *root);

static void deltree(btnode *root); ~btree();};btree∷btree(){ root=null;}void btree∷insert(int val){

int i;btnode *c,*n;int flag;flag=setval (val,root,&i,&c);if(flag)

26

Page 27: dslab own record

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

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

return search(val,root->child[pos],pos);}

}int btree::searchnode(int val,btnode *n,int *pos){

if(val<n->value[1])

27

Page 28: dslab own record

{*pos=0;return 0;

}else{

*pos=n->count;while((val<n->value[*pos])&&*pos>1)

(*pos)--;if(val==n->value[*pos])

return 1;else

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

fillnode(val,c,*newnode,k-mid);*y=n->value[n->count];(*newnode)->child[0]=n->child[n->count];n->count--;

}void btree::del(int val){

btnode *temp; if(!delhelp(val,root))

28

Page 29: dslab own record

cout<<end1<<”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;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<<end1<<”value”<<val<<”not found”;}else

clear(root,i);}else

flag=delhelp(val,root->child[i]);if(root->child[i]!=null){

if(root->child[i]->count<min)restore(root,i);

}return flag;}

}void btree::clear(bnode *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){

29

Page 30: dslab own record

btnode*temp=root->child[i];while(temp->child[0])

temp=temp->child[0];root->value[i]=temp->value[1];

}void btree::restore(btnode*root,int i){

if(i==0){if(root->child[1]->count>min)

leftshift(1);else

merge(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->child[i+1]=temp->child[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];root->child[k]->child[0]=temp->child[temp->count];temp->count--;

30

Page 31: dslab own record

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

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

31

Page 32: dslab own record

}}void btree::deltree(btnode*root){

if(root!=null){

for(int i=0;i<root->count;i++){

delete(root->child[i]);delete(root->child[i]);

}deltree(root->child[i]);deltree(root->child[i]);

}}btree::~btree(){

deltree(root);}void main(){

btree b;int i,n,m,choice;clrscr();

getchoice:cout<<”enter the choice:\n1.insert\n2.delete\n3.display\n4.exit”;cin>>choice;switch(choice)case 1:{

cout<<”enter the number of elements”;cin>>n;

for(i=0;i<n;i++){cin>>m;b.insert(m);}//b.insert(arr[i]);cout<<”b-tree of order:”<<n<<endl;break;’case 2:{cout<<”enter the number to be deleted”;cin>>m;b.del(m);break;}case 3:{b.show();break;}case 4:exit(1);

32

Page 33: dslab own record

}goto getchoice;getch();}

OUTPUT:

1. Insert2. Delete3. Display4. ExitEnter the choice: 1Enter the number of elements: 645 23 36 15 54 32B-tree of order: 6 1. Insert2. Delete3. Display4. ExitEnter the choice: 315 23 32 36 45 54

1. Insert2. Delete3. Display4. ExitEnter the choice: 1Enter the number of elements: 163B-tree of order: 1

1. Insert2. Delete3. Display4. ExitEnter the choice: 315 23 32 36 45 54 63

1. Insert2. Delete3. Display4. ExitEnter the choice: 2Enter the number to be deleted: 45

1. Insert2. Delete3. Display4. ExitEnter the choice: 315 23 32 36 54 63

1. Insert33

Page 34: dslab own record

2. Delete3. Display4. Exit

Enter the choice: 4

34

Page 35: dslab own record

RESULT:

Thus the C++ program for B-tree with insert and delete operations has been implemented and

executed successfully.

Ex.No:6 TRIES

Date:

AIM:

To write the C++ program for implementing tries with insert and delete operations.

ALGORITHM:

Step 1: Start the program by defining the functions.

Step 2: First initialize the node to null.

Step 3: To find the particular element use function find check the element to root node, if it is not

found check for left or right side of the root. If it’s found return the element.

Step 4: To insert the particular element read that element and insert the element with tag as 0 and

Level as 1.

Step 5: To display the elements, display if root as null, print as empty, otherwise call empty.

Step 6: Print the current node in left sub tree in the format as current node data level and tag.

Step 7: Display current node in the right sub tree.

Step 8: End the program.

35

Page 36: dslab own record

PROGRAM:

#include<iostream.h>#include<conio.h>#include<alloc.h>#define MAXBITS 32#define MAXNODES 512#define MAXSYMBS 256struct codetype{

int bits[MAXBITS];int startpos;

};struct nodetype{

int freq;int father;int isleft;

};typedef struct hlist{

int pos;int hfreq;struct hlist*next;

}hnode;struct list{

char alph;int freq;

};class tries{

public:hnode*hroot,*traversal;struct list a[256];int hmin(void);void hinsert(int,int);

};int tries::hmin(){

int p;p=hroot->pos;traversal=hroot;hroot=traversal->next;free(traversal);return(p);

}void tries::hinsert(int p,int freq){

36

Page 37: dslab own record

hnode*new1=(hnode*)malloc(sizeof(hnode));new1->pos=p;new1->hfreq=freq;traversal=hroot;if(hroot==NULL){

hroot=new1;hroot->next=NULL;return;

}if(hroot->next==NULL;{

(hroot->hfreq>new1->hfreq){

new1->next=hroot;hroot=new1;traversal->next=NULL;return;

}else{

hroot->next=new1;new1->next=NULL;return;

}}if(hroot->hfreq>=new1->hfreq){

new1->next=hroot;hroot=new1;return;

}while(traversal->next->hfreq<new1->hfreq){

traversal=traversal->next;if(traversal->next==NULL)break;

}if(traversal->next->hfreq>new1->hfreq){

new1->next=traversal->next;traversal->next=new1;return;

}new1->next=NULL;traversal->next=new1;

}void main(){

tries t;t.hroot=NULL;struct codetype cd,code[MAXSYMBS];struct nodetype node[MAXNODES];int i,k,p,p1,p2,root,n;

37

Page 38: dslab own record

char symb,alph[MAXSYMBS];clrscr();for(i=0;i<MAXSYMBS;i++)

alph[i]=' ';cout<<"\n Tries using Huffman Algorithm";cout<<"\n\n*************************";cout<<"\n\nEnter the number of unique symbols:";cin>>n;for(i=0;i<n;i++){

cout<<"Enter"<<i+first<<"symbol& its frequency:\n";cin>>symb>>node[i].freq;t.hinsert(i,node[i].freq);alph[i]=symb;

}for(p=n;p<(2*n-1);p++){

p1=t.hmin();p2=t.hmin();node[p1].father=p;node[p1].isleft=1;node[p2].father=p;node[p2].isleft=0;node[p].freq=node[p1].freq+node[p2].freq;t.hinsert(p,node[p].freq);

}root=t.hmin();for(i=0;i<n;i++){

cd.startpos=MAXBITS;p=i;while(p!=root)

{--cd.startpos;if(node[p].isleft)cd.bits[cd.startpos]=0;

elsecd.bits[cd.startpos]=1;

p=node[p].father;}for(k=cd.startpos;k<MAXBITS;k++)

code[i].bits[k]=cd.bits[k];code[i].startpos=cd.startpos;

}cout<<"\n\nHuffman codes in tries:";cout<<"\n\n*******************";cout<<"\n\nSYMBOLS\tCODE";for(i=0;i<n;i++){

cout<<"\n\n"<<alph[i]<<"\t";for(k=code[i].startpos;k<MAXBITS;k++)

cout<<code[i].bits[k];}getch();

38

Page 39: dslab own record

}

OUTPUT:

Tries using Huffman Algorithm: ************************Enter the number of unique symbols: 2Enter first symbol& its frequency: a 2Enter second symbol& its frequency: b 1

Huffman codes in tries:******************SYMBOLS CODEa 0b 11

39

Page 40: dslab own record

40

Page 41: dslab own record

RESULT:

Thus the tries with insert and delete operation using C++ has been implemented and executed

successfully.

Ex.No:7 QUICK SORT

DATE:

AIM:

To write the C++ program for implementing the quick sort.

ALGORITHM:

Step1: start the program

Step2: Declare and initialize the array size

Step3: Enter the number of elements to be quick sorted.

Step4: Enter the elements using for loop

Step5: call the function quick (1, noe)

Void quick (int first,int last)

Step6: If the first element is less than the last

(a) Then the first element is taken as the pivot & i=first, & j=last

(b)The condition is checked for i<j if true

Step7: set a loop to check the elements

(a) While (a[pivot]>=a[i]&&i<last)i++;

(b) while (a[pivot]>=a[j]&&j>first)j--;

Step8: if (i>j) Swap (I,j)

Step9: sort the elements and display the sorted values.

41

Page 42: dslab own record

PROGRAM:

#include<iostream.h>#include<conio.h>template<class T>class List{T *a;int size;public:void getdata(int);void qsort(int,int);void putdata();};template<class T>void List<T>::getdata(int n){

size=n; a=new T[n]; for(int i=0;i<n;i++)

cin>>a[i]; a[n]=9999;

} template<class T> void List<T>::putdata() {

cout<<"\n The sorted elements are:"; for(int i=0;i<size;i++)

cout<<" "<<a[i]; } template<class T> void List<T>::qsort(int low,int high) {

T temp; int flag=1; if(low<high)

{ T pivot=a[low]; int i=low; int j=high+1; while(flag)

{ i++; while(a[i]<pivot)

i++; j--;

while(a[j]>pivot)

42

Page 43: dslab own record

j--; if(i<j)

{ temp=a[i]; a[i]=a[j]; a[j]=temp;

} else

flag=0; }

temp=a[low]; a[low]=a[j]; a[j]=temp; qsort(low,j-1); qsort(j+1,high);

} } void main() {

List<int>l; int n; clrscr(); cout<<"\n\t\t QUICKSORT"; cout<<"\n\t\t**************"; cout<<"\n\nEnter the number of elements:"; cin>>n; cout<<"\nEnter"<<n<<"elements:\n"; l.getdata(n); l.qsort(0,n); l.putdata(); getch();

}

OUTPUT:

43

Page 44: dslab own record

QUICKSORT **************Enter the number of elements: 8Enter 8 elements:5030109080204070

The sorted elements are: 10 20 30 40 50 70 80 90

44

Page 45: dslab own record

45

Page 46: dslab own record

RESULT :

Thus the C++ program for quick sort has been implemented and executed successfully.

EX.NO:8 CONVEX HULL

DATE:

AIM:

To write a C++ program for implementing a convex hull.

ALGORITHM:

Step1: Start the program.

Step2: Create a class convex hull algorithm.

Step3: Read the number of points.

Step4: Get the x and y co-ordinate values.

Step5: Sort the values using sort function.

Step6: To sort two values swap the values of i and j.

Step7: Call the function display to display the boundary points.

Step8: The function check id used to check whether the point is angular or not (180▫).

Step9: End of the program.

46

Page 47: dslab own record

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

}void convexhull::swap(int i,int j){

int temp=x[i];x[i]=x[j];x[j]=temp;temp=y[i];

47

Page 48: dslab own record

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

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

}}

}if(next!=0)

check(next,c);}void main(){

clrscr();convexhull c;cout<<"\nCONVEX HULL";cout<<"\n***********\n";c.insert();getch();

}

48

Page 49: dslab own record

OUTPUT:

CONVEX HULL***********Enter number of points: 7Enter x and y coordinates for point: 136Enter x and y coordinates for point: 22 3Enter x and y coordinates for point: 334Enter x and y coordinates for point: 433Enter x and y coordinates for point: 522Enter x and y coordinates for point: 661Enter x and y coordinates for point: 731Boundary points are: (2,2)(2,3)(3,1)(3,6)(6,1)

49

Page 50: dslab own record

RESULT :

Thus the C++ program for convex hull has been implemented and executed successfully.

50

Page 51: dslab own record

EX.NO:9 0/1 KNAPSACK USING DYNAMIC PROGRAMMING

DATE:

AIM:

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

ALGORITHM:

Step1: start the program and define the function.

Step2: Initialize the weight and profit.

Step3: Read the number of objects that are given.

Step4: For each object, print the profit and weight.

Step5: Initializing is set to false.

Step6: Display and print the item weight and profit.

Step7: Display the total cost.

Step8: End of the program.

51

Page 52: dslab own record

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<<"\n\tEnter 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)

p[i][j]=option1;else

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

52

Page 53: dslab own record

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

53

Page 54: dslab own record

OUTPUT:

KNAPSACK USING DYNAMIC PROGRAMMING

********************************** Enter the number of objects: 4Enter the maximum weight sack can take: 9For object1:Enter profit: 40Enter Weight: 2For object2:Enter profit: 30Enter Weight: 5For object3:Enter profit: 50Enter Weight: 10For object4:Enter profit: 10Enter Weight: 5

Profit table:************Capacity 1 2 3 4 5 6 7 8 9Item 1 0 40 40 40 40 40 40 40 402 0 40 40 40 40 40 70 70 703 0 40 40 40 40 40 70 70 704 0 40 40 40 40 40 70 70 70

Profit=70Item Weight Profit2 5 301 2 40

54

Page 55: dslab own record

RESULT:

55

Page 56: dslab own record

Thus the C++ program of 0/1 knapsack using dynamic programming has been implemented and

executed successfully

EX.NO:10 GRAPH COLOURING USING BACKTRACKING

DATE:

AIM:

To implement the C++ program for graph coloring using Backtracking.

ALGORITHM:

Step 1: Start the program and define the function.

Step 2: Create a class coloring.

Step 3: Get the number of vertices in the graph.

Step 4: Enter one if there is an edge in the graph.

Step 5: And enter zero if there is no edge in the graph.

Step 6: Get the adjacency matrix of the given values.

Step 7: Perform all possible combinations that are given.

Step 8: Display all the combination.

Step 9: End of the program.

56

Page 57: dslab own record

PROGRAM:

#include<iostream.h>#include<conio.h>class Graphcoloring{

int a[10][10],x[10],m,n;public:

void read();void mcoloring(int);void nextvalue(int);

};void Graphcoloring::read(){

int i;cout<<"\nEnter no.of vertices:";cin>>n;cout<<"\nEnter no.of colors:";cin>>m;cout<<"\n(Enter 1 if there is an edge Otherwise 0)\n\n";for(i=1;i<=n;i++)for(int j=1;j<=n;j++){

cout<<"Between"<<i<<"and"<<j<<":";cin>>a[i][j];

}cout<<"\nADJACENCY MATRIX:\n\n";for(i=1;i<=n;i++)

cout<<"\tNode"<<i;for(i=1;i<=n;i++){cout<<"\n\nNODE"<<i<<"\t";for(int j=1;j<=n;j++)

cout<<a[i][j]<<"\t";}for(i=1;i<=n;i++)

x[i]=0;cout<<"\n\n\nPOSSIBLE COMBINATION FOR"<<m<<"COLORS:";cout<<"\n------------------\n\n";for(j=1;j<=n;j++)

cout<<"NODE"<<j<<"\t";cout<<"\n\n";mcoloring(1);

}void Graphcoloring::mcoloring(int k){

do{

nextvalue(k);

57

Page 58: dslab own record

if(x[k]==0)break;

if(k==n){

for(int i=1;i<=n;i++)cout<<x[i]<<”\t”;

cout<<"\n";}else

mcoloring(k+1);}while(1);

}void Graphcoloring::nextvalue(int k){

int j;do{

x[k]=(x[k]+1)%(m+1);if(x[k]==0)

return;for(j=1;j<=n;j++){

if((a[k][j]==1) && (x[k]==x[j]))break;

}if(j==n+1)

return;}while(1);

}void main(){

clrscr();Graphcoloring g;cout<<"\n\t\tGRAPH COLORING";cout<<"\n\t\t**************\n\n";g.read();getch();

}

58

Page 59: dslab own record

OUTPUT:

GRAPH COLORING******************

Enter no. of vertices: 5Enter the no. of colors: 3(Enter 1 if there is an edge otherwise 0)Between 1 and 1: 0Between 1 and 2: 1Between 1 and 3: 1Between 1 and 4: 0Between 1 and 5: 0Between 2 and 1: 1Between 2 and 2: 0Between 2 and 3: 1Between 2 and 4: 0Between 2 and 5: 1Between 3 and 1: 1Between 3 and 2: 1Between 3 and 3: 0Between 3 and 4: 1Between 3 and 5: 1Between 4 and 1: 0Between 4 and 2: 0Between 4 and 3: 1Between 4 and 4: 0Between 4 and 5: 1Between 5 and 1: 0Between 5 and 2: 1Between 5 and 3: 1Between 5 and 4: 1Between 5 and 5: 0

ADJACENCY MATRIX:

Node 1 Node 2 Node 3 Node 4 Node 5

Node 1 0 1 1 0 0

Node 2 1 0 1 0 1

Node 3 1 1 0 1 1

Node 4 0 0 1 0 1

Node 5 0 1 1 1 0

59

Page 60: dslab own record

POSSIBLE COMBINATIONS FOR 3 COLORS:--------------------------------------------------------Node 1 Node 2 Node 3 Node 4 Node 5

1 2 3 2 1

1 3 2 3 1

2 1 3 1 2

2 3 1 3 2

3 1 2 1 3

3 2 1 2 3

60

Page 61: dslab own record

RESULT :

61

Page 62: dslab own record

Thus the C++ program for graph coloring using Backtracking has been implemented and executed

successfully.

62