0% found this document useful (0 votes)
64 views61 pages

Dslab Own Record

The document describes a C++ program to implement an AVL tree with insert and delete operations. The algorithm involves starting by defining functions, inserting elements to the tree, checking if the tree is balanced by calculating the balance factor which can be 0, 1 or -1, and performing rotations to rebalance the tree if unbalanced during insertions or deletions. The program is executed to insert and delete nodes from the AVL tree while maintaining its balanced property.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views61 pages

Dslab Own Record

The document describes a C++ program to implement an AVL tree with insert and delete operations. The algorithm involves starting by defining functions, inserting elements to the tree, checking if the tree is balanced by calculating the balance factor which can be 0, 1 or -1, and performing rotations to rebalance the tree if unbalanced during insertions or deletions. The program is executed to insert and delete nodes from the AVL tree while maintaining its balanced property.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 61

EX.

NO:1 DATE:

MIN HEAP

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.

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

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

OUTPUT:
MINHEAP: Enter size of heap: 5 Enter element1: 2 Enter element2: 6 Enter element3: 5 Enter element4: 3 Enter 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: 2 Element deleted is: 2 Enter your choice: 3 Min heap: 3 6 5 7 4 Enter your choice: 4

RESULT:
Thus the C++ program for Min heap with insert and delete minimum operation implemented and executed successfully.
5

has been

EX.NO:2 DATE:

DEAPS

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.

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

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

{ 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

d.arr[3]=d.arr[d.size--]; d.removemax(3); break; case 4: d.display(); break; } } while(ch<5); getch(); }

10

OUTPUT:
DEAPS CREATE DEAPS Enter the no. of elements to be inserted: 6 Enter 6 elements: 25 45 10 8 5 40 OPERATIONS: 1. INSERT 2. REMOVE MIN 3. REMOVE MAX 4. DISPLAY 5. EXIT Enter your choice: 4 Deap Elements: null 5 45 8 10 25 40 Enter your choice: 1 Enter the element to be insert: 15 Enter your choice: 4 Deap Elements: null 5 45 8 10 25 40 15 Enter your choice: 2 The Minimum element removed is: 5 Enter your choice: 4 Deap Elements: null 8 45 15 10 25 40 Enter your choice: 3 The Maximum Element removed is: 45 Enter your choice: 4 Deap Element: null 8 40 15 10 25 Enter your choice: 5

11

RESULT :
Thus the C++ program for deaps structure with insert and delete operations has been implemented and executed successfully.
12

EX.NO.3 DATE:

LEFTIST HEAP

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

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

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

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

OUTPUT:
LEFTIST HEAP 1. CREATE MINLEFISTIST HEAP 2. INSERT 3. DELETE 4. MERGE 5. DISPLAY 6. EXIT ENTER YOUR CHOICE: 1 (Enter 0 to stop insertion) Enter value: 2 Enter value: 7 Enter value: 50 Enter value: 11 Enter value: 13 Enter value: 80 Enter value: 0 ENTER YOUR CHOICE: 5 The leftist heap is: 2 11 50 13 7 80 ENTER YOUR CHOICE: 2 Enter an element to be insert: 15 ENTER YOUR CHOICE: 5 The leftist heap is: 2 11 50 13 7 80 15 ENTER YOUR CHOICE: 3 Deleted element is: 2 ENTER YOUR CHOICE: 5 The leftist heap is: 7 11 50 13 15 80 ENTER YOUR CHOICE: 4 Enter heap to merge: (Enter 0 to stop insertion) Enter value: 5 Enter value: 9 Enter value: 8 Enter value: 12 Enter value: 10 Enter value: 20 Enter value: 0 ENTER YOUR CHOICE: 5 The leftist heap is: 5 7 11 50 13 15 9 20 80 8 12 10 ENTER YOUR CHOICE: 6

17

RESULT:
Thus the C++ program for leftist heap with insert and deletemin operation has been implemented and executed successfully.
18

EX.NO:4 DATE:

AVL TREE

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

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:; cinn; T1 x; for (i=1;in;i++) { cout\nEnter the element to insert:; cinx; 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:Rootht); } template<class T1> void AVL<T1>insert(T1 x,AVLnode<T1>*&Root)
20

{ if(Root==NULL) { Root=nsew AVLnode<T1>; Rootelement=x; Rootleft=NULL; Rootright=NULL; Rootht=0; } else if(x<(Root)element) { insert(x,Rootleft); if(height(Rootleft)- height(Rootright)==2) { if(x<Rootleftelement) { cout\n Unbalanced occur atRootelement; cout\n Performing left-left rotation\n; rotatewithleftchild(Root); } else { cout\n Unbalanced occur atRootelement; cout\n Performing double rotation( Left-Right)\n; doublerotationwithleft(Root); } } } else if(x>Rootelement) { insert(x,Rootright); if(height(Rootright)- height(Rootleft)==2) { if(x<Rootrightelement) { cout\n Unbalanced occur atRootelement; cout\n Performing right-right rotation\n; rotatewithrightchild(Root); } else { cout\n Unbalanced occur atRootelement; cout\n Performing double rotation( Right-Left)\n; doublerotationwithright(Root); } } } Rootht=max(height(Rootleft),height(Rootright))+1; } template<class T1> void AVL<T1>rotatewithleftchild(AVLnode<T1>*&k2) { AVLnode<T1>*k1=k2left; K2left=k1right;
21

K1->right=K2; K2ht=max(height(k2left),height(k2right))+1; K1ht=max(height(k1left),height(k1right))+1; K2=k1; } template<class T1> void AVL<T1>doublerotationwithleft(AVLnode<T1>*&k3) { rotatewithrightchild(k3left); rotatewithleftchild(k3); } template<class T1> void AVL<T1>doublerotationwithright(AVLnode<T1>*&k3) { rotatewithleftchild(k3right); rotatewithrightchild(k3); } template<class T1> void AVL<T1>rotatewithrightchild(AVLnode<T1>*&k2) { AVLnode<t1>*k1=k2right; k2 right =k1left; k1left=k2; k2ht=max(height(k2left),height(k2right))+1; k1ht=max(height(k1left),height(k1right))+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 { coutRootelement ; preorder(Rootleft); preorder(Rootright); } } void main() { AVL <int> a; clrscr(); coutAVL TREE\n; cout**************\n; a.build(); getch(); }
22

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 10 Performing Double rotation (Left-Right) Enter the element to insert: 12 Enter the element to insert: 11 Unbalanced occur at 10 Performing Double rotation (Right-Left) Enter the element to insert: 15 Unbalanced occur at 8 Performing 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

23

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

24

EX.NO:5 DATE:

BTREE

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

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(); }; btreebtree() { root=null; } void btreeinsert(int val) { int i; btnode *c,*n; int flag; flag=setval (val,root,&i,&c); if(flag) { n=new btnode; ncount=1; nvalue[1]=i;
26

nchild[0]=root; nchild[1]=c; root=n; } } int btreesetval(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)) coutendlkey value already existsendl; if(setval(val, nchild[k],p,c)) { if(ncount<MAX) { fillnode(*p, *c,n,k); return 0; } else { split(*p,*c,n,k,p,c); return 1; } } return 0; } } btnode *btreesearch(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]) { *pos=0; return 0; }
27

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; else mid=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)) cout<<end1<<value<<val<<not found; else { if(root->count==0)
28

{ 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) { btnode*temp=root->child[i]; while(temp->child[0]) temp=temp->child[0]; root->value[i]=temp->value[1];
29

} 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); else merge(i); } else { if(root-> child[i-1]->count>min) rightshift(i); else { if(root-> child[i+1]->count>min) leftshift(i+1); else merge(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--; } void btree::leftshift(int k) { btnode*temp;
30

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]); } } void btree::deltree(btnode*root) {
31

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); } goto getchoice; getch(); }
32

OUTPUT:
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1 Enter the number of elements: 6 45 23 36 15 54 32 B-tree of order: 6 1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3 15 23 32 36 45 54 1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1 Enter the number of elements: 1 63 B-tree of order: 1 1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3 15 23 32 36 45 54 63 1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 2 Enter the number to be deleted: 45 1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3 15 23 32 36 54 63 1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 4
33

RESULT:
Thus the C++ program for B-tree with insert and delete operations has been implemented and executed successfully.
34

Ex.No:6 Date:

TRIES

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

PROGRAM:
#include<iostream.h> #include<conio.h> #include<alloc.h> #define MAXBITS 32 #define MAXNODES 512 #define MAXSYMBS 256 struct 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) { hnode*new1=(hnode*)malloc(sizeof(hnode)); new1->pos=p; new1->hfreq=freq; traversal=hroot; if(hroot==NULL)
36

{ 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; char symb,alph[MAXSYMBS]; clrscr(); for(i=0;i<MAXSYMBS;i++) alph[i]=' '; cout<<"\n Tries using Huffman Algorithm";
37

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

OUTPUT:
Tries using Huffman Algorithm: ************************ Enter the number of unique symbols: 2 Enter first symbol& its frequency: a 2 Enter second symbol& its frequency: b 1 Huffman codes in tries: ****************** SYMBOLS CODE a 0 b 11

39

RESULT:
Thus the tries with insert and delete operation using C++ has been implemented and executed successfully.
40

Ex.No:7 DATE:

QUICK SORT

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

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) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; }
42

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

QUICKSORT ************** Enter the number of elements: 8 Enter 8 elements: 50 30 10 90 80 20 40 70 The sorted elements are: 10 20 30 40 50 70 80 90

44

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

45

EX.NO:8 DATE:

CONVEX HULL

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

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]; y[i]=y[j]; y[j]=temp; } void convexhull::display() { cout<<"Boundary points are"; for(int i=0;i<n;i++)
47

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

OUTPUT:
CONVEX HULL *********** Enter number of points: 7 Enter x and y coordinates for point: 1 3 6 Enter x and y coordinates for point: 2 2 3 Enter x and y coordinates for point: 3 3 4 Enter x and y coordinates for point: 4 3 3 Enter x and y coordinates for point: 5 2 2 Enter x and y coordinates for point: 6 6 1 Enter x and y coordinates for point: 7 3 1 Boundary points are: (2,2)(2,3)(3,1)(3,6)(6,1)

49

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

50

EX.NO:9 DATE:

0/1 KNAPSACK USING DYNAMIC PROGRAMMING

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

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++) { cout<<"\n"<<i; for(j=1;j<=w;j++) cout<<"\t"<<p[i][j]; cout<<"\n";
52

} 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

OUTPUT:
KNAPSACK USING DYNAMIC PROGRAMMING ********************************** Enter the number of objects: 4 Enter the maximum weight sack can take: 9 For object1: Enter profit: 40 Enter Weight: 2 For object2: Enter profit: 30 Enter Weight: 5 For object3: Enter profit: 50 Enter Weight: 10 For object4: Enter profit: 10 Enter Weight: 5 Profit table: ************ Capacity 1 Item 1 0 2 0 3 0 4 0 Profit=70 Item Weight 2 5 1 2

2 40 40 40 40 Profit 30 40

3 40 40 40 40

4 40 40 40 40

5 40 40 40 40

6 40 40 40 40

7 40 70 70 70

8 40 70 70 70

9 40 70 70 70

54

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

55

EX.NO:10 DATE:

GRAPH COLOURING USING BACKTRACKING

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

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); if(x[k]==0) break; if(k==n) { for(int i=1;i<=n;i++)
57

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

OUTPUT:
GRAPH COLORING ****************** Enter no. of vertices: 5 Enter the no. of colors: 3 (Enter 1 if there is an edge otherwise 0) Between 1 and 1: 0 Between 1 and 2: 1 Between 1 and 3: 1 Between 1 and 4: 0 Between 1 and 5: 0 Between 2 and 1: 1 Between 2 and 2: 0 Between 2 and 3: 1 Between 2 and 4: 0 Between 2 and 5: 1 Between 3 and 1: 1 Between 3 and 2: 1 Between 3 and 3: 0 Between 3 and 4: 1 Between 3 and 5: 1 Between 4 and 1: 0 Between 4 and 2: 0 Between 4 and 3: 1 Between 4 and 4: 0 Between 4 and 5: 1 Between 5 and 1: 0 Between 5 and 2: 1 Between 5 and 3: 1 Between 5 and 4: 1 Between 5 and 5: 0 ADJACENCY MATRIX: Node 1 Node 1 Node 2 Node 3 Node 4 Node 5 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

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

60

RESULT :
Thus the C++ program for graph coloring using Backtracking has been implemented and executed successfully.
61

You might also like