ADSA LAB PROGRAMS LIST
1) Write
a C++ Program for stacks using linked list ?
ans:
/*Program for implementing stack using linked list.*/
#include<iostream>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
class node *next;
int data;
};
class stack : public node
{
node *head;
int tos;
public:
stack()
{
tos=-1;
}
void push(int x)
{
if (tos < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
tos ++;
}
else
{
node *temp,*temp1;
temp=head;
if(tos >= 4)
{
cout <<"stack over flow";
return;
}
tos++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}
void display()
{
node *temp;
temp=head;
if (tos < 0)
{
cout <<" stack under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( tos < 0 )
{
cout <<"stack under flow";
return;
}
tos--;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
};
main()
{
stack s1;
int ch;
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ur choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"\n enter a element";
cin >> ch;
s1.push(ch);
break;
case 2: s1.pop();break;
case 3: s1.display();
break;
case 4: exit(0);
}
}
return (0);
}
/*
OUTPUT
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element67
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23 67
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
stack under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:4
*/
2) Write a C++ Program for queues using linked
list ?
#include<conio.h>
#include<iostream.h>
#include<process.h>
#include<malloc.h>
// Creating a NODE Structure
struct node
{
int data;
struct node *next;
};
// Creating a class QUEUE
class queue
{
struct node *frnt,*rear;
public:
queue() // constructure
{
frnt=rear=NULL;
}
void insert(); // to insert an element
void del(); // to delete an element
void show(); // to show the stack
};
// Insertion
void queue::insert()
{
int value;
struct node *ptr;
cout<<"\nInsertion\n";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(frnt==NULL)
frnt=ptr;
else
rear->next=ptr;
rear=ptr;
cout<<"\nNew item is inserted to the Queue!!!";
getch();
}
// Deletion
void queue::del()
{
if(frnt==NULL)
{
cout<<"\nQueue is empty!!";
getch();
return;
}
struct node *temp;
temp=frnt;
frnt=frnt->next;
cout<<"\nDeletion Operation........\nDeleted value is "<<temp->data;
delete temp;
getch();
}
// Show Queue
void queue::show()
{
struct node *ptr1=frnt;
if(frnt==NULL)
{
cout<<"The Queue is empty!!";
getch();
return;
}
cout<<"\nThe Queue is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"END";
getch();
}
void main()
{
clrscr();
queue q;
int choice;
while(1)
{
cout<<"\n";
cout<<"\n\tQUEUE USING LINKED LIST\n\n";
cout<<"1:INSERTION\n2:DELETION\n3:DISPLAY QUEUE\n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
q.insert();
break;
case 2:
q.del();
break;
case 3:
q.show();
break;
case 4:
exit(0);
break;
default:
cout<<"Please enter correct choice(1-4)!!";
getch();
break;
}
}
getch();
}
3) Write a C++ Program for Implementation of
i) BFS ii) DFS
ans:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"Visitied vertices\n";
cout << v;
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
/*
OUTPUT
enterno of vertices9
ente no of edges9
EDGES
1 2
2 3
1 5
1 4
4 7
7 8
8 9
2 6
5 7
enter initial vertex1
Visited vertices
12 4 5 3 6 7 8 9
*/
/* Write C++ programs for the implementation of Depth-first search(DFS) for a given graph */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"ORDER OF VISITED VERTICES";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
/*
OUTPUT
enterno of vertices9
ente no of edges9
EDGES
1 2
2 3
2 6
1 5
1 4
4 7
5 7
7 8
8 9
enter initial vertex1
ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5
*/
ans:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"Visitied vertices\n";
cout << v;
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
/*
OUTPUT
enterno of vertices9
ente no of edges9
EDGES
1 2
2 3
1 5
1 4
4 7
7 8
8 9
2 6
5 7
enter initial vertex1
Visited vertices
12 4 5 3 6 7 8 9
*/
/* Write C++ programs for the implementation of Depth-first search(DFS) for a given graph */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"ORDER OF VISITED VERTICES";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
/*
OUTPUT
enterno of vertices9
ente no of edges9
EDGES
1 2
2 3
2 6
1 5
1 4
4 7
5 7
7 8
8 9
enter initial vertex1
ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5
*/
4) Write a C++ Program for Implemenations of
i)
Bubble Sort ii) Quick sort iii) Merge Sort
ans:(i)
#include <iostream>
#include<conio.h>
template <class X>
void bubble(X *data, int size)
{
register int a, b;
X t;
for(a=1; a < size; a++)
for(b=size-1; b >= a; b--)
if(data[b-1] > data[b]) {
t = data[b-1];
data[b-1] = data[b];
data[b] = t;
}
}
void main()
{
int i[] = {3, 2, 5, 6, 1, 8, 9, 3, 6, 9};
double d[] = {1.2, 5.5, 2.2, 3.3};
int j;
bubble(i, 10); // sort ints
bubble(d, 4); // sort doubles
for(j=0; j<10; j++)
cout << i[j] << ' ';
cout << endl;
for(j=0; j<4; j++)
cout << d[j] << ' ';
cout << endl;
getch()
}
(ii)
#include<process.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int Partition(int low,int high,int arr[]);
void Quick_sort(int low,int high,int arr[]);
void main()
{
int *a,n,low,high,i;
clrscr();
cout<<"/**************************Quick Sort Algorithm
Implementation*****************/
";
cout<<"Enter number of elements:
";
cin>>n;
a=new int[n];
/* cout<<"enter the elements:
";
for(i=0;i<n;i++)
cin>>a;*/
for(i=0;i<n;i++)
a[i]=rand()%100;
clrscr();
cout<<"
Initial Order of elements
";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<"
";
high=n-1;
low=0;
Quick_sort(low,high,a);
cout<<"
Final Array After Sorting:
";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
/*Function for partitioning the array*/
int Partition(int low,int high,int arr[])
{ int i,high_vac,low_vac,pivot/*,itr*/;
pivot=arr[low];
while(high>low)
{ high_vac=arr[high];
while(pivot<high_vac)
{
if(high<=low) break;
high--;
high_vac=arr[high];
}
arr[low]=high_vac;
low_vac=arr[low];
while(pivot>low_vac)
{
if(high<=low) break;
low++;
low_vac=arr[low];
}
arr[high]=low_vac;
}
arr[low]=pivot;
return low;
}
void Quick_sort(int low,int high,int arr[])
{
int Piv_index,i;
if(low<high)
{
Piv_index=Partition(low,high,arr);
Quick_sort(low,Piv_index-1,arr);
Quick_sort(Piv_index+1,high,arr);
}
}
//merge sort
#include<stdio.h>
#include<conio.h>
void getdata(int arr[],int n)
{
int i;
printf("enter the data:\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
}
void display(int arr[],int n)
{
int i;
printf("
");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getchar();
}
void sort(int arr[],int low,int mid,int high)
{
int i,j,k,l,b[20];
l=low;
i=low;
j=mid+1;
while((l<=mid)&&(j<=high))
{
if(arr[l]<=arr[j])
{
b[i]=arr[l];
l++;
}
else
{
b[i]=arr[j];
j++;
}
i++;
}
if(l>mid)
{
for(k=j;k<=high;k++)
{
b[i]=arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
b[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=b[k];
}
}
void partition(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
sort(arr,low,mid,high);
}
}
void main()
{
int arr[20];
int n;
printf("Enter number of data:");
scanf("%d",&n);
getdata(arr,n);
partition(arr,0,n-1);
display(arr,n);
getch();
}
ans:(i)
#include <iostream>
#include<conio.h>
template <class X>
void bubble(X *data, int size)
{
register int a, b;
X t;
for(a=1; a < size; a++)
for(b=size-1; b >= a; b--)
if(data[b-1] > data[b]) {
t = data[b-1];
data[b-1] = data[b];
data[b] = t;
}
}
void main()
{
int i[] = {3, 2, 5, 6, 1, 8, 9, 3, 6, 9};
double d[] = {1.2, 5.5, 2.2, 3.3};
int j;
bubble(i, 10); // sort ints
bubble(d, 4); // sort doubles
for(j=0; j<10; j++)
cout << i[j] << ' ';
cout << endl;
for(j=0; j<4; j++)
cout << d[j] << ' ';
cout << endl;
getch()
}
(ii)
#include<process.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int Partition(int low,int high,int arr[]);
void Quick_sort(int low,int high,int arr[]);
void main()
{
int *a,n,low,high,i;
clrscr();
cout<<"/**************************Quick Sort Algorithm
Implementation*****************/
";
cout<<"Enter number of elements:
";
cin>>n;
a=new int[n];
/* cout<<"enter the elements:
";
for(i=0;i<n;i++)
cin>>a;*/
for(i=0;i<n;i++)
a[i]=rand()%100;
clrscr();
cout<<"
Initial Order of elements
";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<"
";
high=n-1;
low=0;
Quick_sort(low,high,a);
cout<<"
Final Array After Sorting:
";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
/*Function for partitioning the array*/
int Partition(int low,int high,int arr[])
{ int i,high_vac,low_vac,pivot/*,itr*/;
pivot=arr[low];
while(high>low)
{ high_vac=arr[high];
while(pivot<high_vac)
{
if(high<=low) break;
high--;
high_vac=arr[high];
}
arr[low]=high_vac;
low_vac=arr[low];
while(pivot>low_vac)
{
if(high<=low) break;
low++;
low_vac=arr[low];
}
arr[high]=low_vac;
}
arr[low]=pivot;
return low;
}
void Quick_sort(int low,int high,int arr[])
{
int Piv_index,i;
if(low<high)
{
Piv_index=Partition(low,high,arr);
Quick_sort(low,Piv_index-1,arr);
Quick_sort(Piv_index+1,high,arr);
}
}
//merge sort
#include<stdio.h>
#include<conio.h>
void getdata(int arr[],int n)
{
int i;
printf("enter the data:\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
}
void display(int arr[],int n)
{
int i;
printf("
");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getchar();
}
void sort(int arr[],int low,int mid,int high)
{
int i,j,k,l,b[20];
l=low;
i=low;
j=mid+1;
while((l<=mid)&&(j<=high))
{
if(arr[l]<=arr[j])
{
b[i]=arr[l];
l++;
}
else
{
b[i]=arr[j];
j++;
}
i++;
}
if(l>mid)
{
for(k=j;k<=high;k++)
{
b[i]=arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
b[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=b[k];
}
}
void partition(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
sort(arr,low,mid,high);
}
}
void main()
{
int arr[20];
int n;
printf("Enter number of data:");
scanf("%d",&n);
getdata(arr,n);
partition(arr,0,n-1);
display(arr,n);
getch();
}
5)
Write a C++ Program for BST Operations
i) Insertion ii) Deletion
/*C++ program to implement Binary Search Tree(BST) and its Operations*/
#include<iostream.h>
#include<conio.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 ) ;
}
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 } ;
int r[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 } ;
int sz = sizeof ( a ) ;
clrscr();
btree bt ( a, l, r, sz ) ;
bt.insert( 0 ) ;
cout << "\nIn-order Traversal: " << endl ;
bt.display( ) ;
getch();
}
/*C++ program to implement Binary Search Tree(BST) and its Operations*/
#include<iostream.h>
#include<conio.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 ) ;
}
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 } ;
int r[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 } ;
int sz = sizeof ( a ) ;
clrscr();
btree bt ( a, l, r, sz ) ;
bt.insert( 0 ) ;
cout << "\nIn-order Traversal: " << endl ;
bt.display( ) ;
getch();
}
6)
Write a C++ Program for Prims algorithm
?
Ans:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u;
void main()
{
int m,c;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES Cost\n";
for(k=1;k<=m;k++)
{
cin >>i>>j>>c;
cost[i][j]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;
cout <<"ORDER OF VISITED VERTICES";
k=1;
while(k<n)
{
m=31999;
if(k==1)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(cost[i][j]<m)
{
m=cost[i][j];
u=i;
}
}
else
{
for(j=n;j>=1;j--)
if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
m=cost[v][j];
u=j;
}
}
cost[v][u]=31999;
v=u;
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
OUTPUT
enterno of vertices7
ente no of edges9
EDGES Cost
1 6 10
6 5 25
5 4 22
4 3 12
3 2 16
2 7 14
5 7 24
4 7 18
1 2 28
ORDER OF VISITED VERTICES1 6 5 4 3 2
7) Write a C++ Program for an AVL Tree Implementation ?
ans:
#include <stdlib.h>
#include<constream.h>
#define FALSE 0
#define TRUE 1
struct AVLNode
{
int data ;
int balfact ;
AVLNode *left ;
AVLNode *right ;
} ;
class avltree
{
private :
AVLNode *root ;
public :
avltree( ) ;
AVLNode* insert ( int data, int *h ) ;
static AVLNode* buildtree ( AVLNode *root, int data, int *h ) ;
void display( AVLNode *root ) ;
AVLNode* deldata ( AVLNode* root, int data, int *h ) ;
static AVLNode* del ( AVLNode *node, AVLNode* root, int *h ) ;
static AVLNode* balright ( AVLNode *root, int *h ) ;
static AVLNode* balleft ( AVLNode* root, int *h ) ;
void setroot ( AVLNode *avl ) ;
~avltree( ) ;
static void deltree ( AVLNode *root ) ;
} ;
avltree :: avltree( )
{
root = NULL ;
}
AVLNode* avltree :: insert ( int data, int *h )
{
root = buildtree ( root, data, h ) ;
return root ;
}
AVLNode* avltree :: buildtree ( AVLNode *root, int data, int *h )
{
AVLNode *node1, *node2 ;
if ( root == NULL )
{
root = new AVLNode ;
root -> data = data ;
root -> left = NULL ;
root -> right = NULL ;
root -> balfact = 0 ;
*h = TRUE ;
return ( root ) ;
}
if ( data < root -> data )
{
root -> left = buildtree ( root -> left, data, h ) ;
// If left subtree is higher
if ( *h )
{
switch ( root -> balfact )
{
case 1 :
node1 = root -> left ;
if ( node1 -> balfact == 1 )
{
cout << "\nRight rotation." ;
root -> left = node1 -> right ;
node1 -> right = root ;
root -> balfact = 0 ;
root = node1 ;
}
else
{
cout << "\nDouble rotation, left then right." ;
node2 = node1 -> right ;
node1 -> right = node2 -> left ;
node2 -> left = node1 ;
root -> left = node2 -> right ;
node2 -> right = root ;
if ( node2 -> balfact == 1 )
root -> balfact = -1 ;
else
root -> balfact = 0 ;
if ( node2 -> balfact == -1 )
node1 -> balfact = 1 ;
else
node1 -> balfact = 0 ;
root = node2 ;
}
root -> balfact = 0 ;
*h = FALSE ;
break ;
case 0 :
root -> balfact = 1 ;
break ;
case -1 :
root -> balfact = 0 ;
*h = FALSE ;
}
}
}
if ( data > root -> data )
{
root -> right = buildtree ( root -> right, data, h ) ;
if ( *h )
{
switch ( root -> balfact )
{
case 1 :
root -> balfact = 0 ;
*h = FALSE ;
break ;
case 0 :
root -> balfact = -1 ;
break ;
case -1 :
node1 = root -> right ;
if ( node1 -> balfact == -1 )
{
cout << "\nLeft rotation." ;
root -> right = node1 -> left ;
node1 -> left = root ;
root -> balfact = 0 ;
root = node1 ;
}
else
{
cout << "\nDouble rotation, right then left." ;
node2 = node1 -> left ;
node1 -> left = node2 -> right ;
node2 -> right = node1 ;
root -> right = node2 -> left ;
node2 -> left = root ;
if ( node2 -> balfact == -1 )
root -> balfact = 1 ;
else
root -> balfact = 0 ;
if ( node2 -> balfact == 1 )
node1 -> balfact = -1 ;
else
node1 -> balfact = 0 ;
root = node2 ;
}
root -> balfact = 0 ;
*h = FALSE ;
}
}
}
return ( root ) ;
}
void avltree :: display ( AVLNode* root )
{
if ( root != NULL )
{
display ( root -> left ) ;
cout << root -> data << "\t" ;
display ( root -> right ) ;
}
}
AVLNode* avltree :: deldata ( AVLNode *root, int data, int *h )
{
AVLNode *node ;
if ( root -> data == 13 )
cout << root -> data ;
if ( root == NULL )
{
cout << "\nNo such data." ;
return ( root ) ;
}
else
{
if ( data < root -> data )
{
root -> left = deldata ( root -> left, data, h ) ;
if ( *h )
root = balright ( root, h ) ;
}
else
{
if ( data > root -> data )
{
root -> right = deldata ( root -> right, data, h ) ;
if ( *h )
root = balleft ( root, h ) ;
}
else
{
node = root ;
if ( node -> right == NULL )
{
root = node -> left ;
*h = TRUE ;
delete ( node ) ;
}
else
{
if ( node -> left == NULL )
{
root = node -> right ;
*h = TRUE ;
delete ( node ) ;
}
else
{
node -> right = del ( node -> right, node, h ) ;
if ( *h )
root = balleft ( root, h ) ;
}
}
}
}
}
return ( root ) ;
}
AVLNode* avltree :: del ( AVLNode *succ, AVLNode *node, int *h )
{
AVLNode *temp = succ ;
if ( succ -> left != NULL )
{
succ -> left = del ( succ -> left, node, h ) ;
if ( *h )
succ = balright ( succ, h ) ;
}
else
{
temp = succ ;
node -> data = succ -> data ;
succ = succ -> right ;
delete ( temp ) ;
*h = TRUE ;
}
return ( succ ) ;
}
AVLNode* avltree :: balright ( AVLNode *root, int *h )
{
AVLNode *temp1, *temp2 ;
switch ( root -> balfact )
{
case 1 :
root -> balfact = 0 ;
break ;
case 0 :
root -> balfact = -1 ;
*h = FALSE ;
break ;
case -1 :
temp1 = root -> right ;
if ( temp1 -> balfact <= 0 )
{
cout << "\nLeft rotation." ;
root -> right = temp1 -> left ;
temp1 -> left = root ;
if ( temp1 -> balfact == 0 )
{
root -> balfact = -1 ;
temp1 -> balfact = 1 ;
*h = FALSE ;
}
else
{
root -> balfact = temp1 -> balfact = 0 ;
}
root = temp1 ;
}
else
{
cout << "\nDouble rotation, right then left." ;
temp2 = temp1 -> left ;
temp1 -> left = temp2 -> right ;
temp2 -> right = temp1 ;
root -> right = temp2 -> left ;
temp2 -> left = root ;
if ( temp2 -> balfact == -1 )
root -> balfact = 1 ;
else
root -> balfact = 0 ;
if ( temp2 -> balfact == 1 )
temp1 -> balfact = -1 ;
else
temp1 -> balfact = 0 ;
root = temp2 ;
temp2 -> balfact = 0 ;
}
}
return ( root ) ;
}
AVLNode* avltree :: balleft ( AVLNode *root, int *h )
{
AVLNode *temp1, *temp2 ;
switch ( root -> balfact )
{
case -1 :
root -> balfact = 0 ;
break ;
case 0 :
root -> balfact = 1 ;
*h = FALSE ;
break ;
case 1 :
temp1 = root -> left ;
if ( temp1 -> balfact >= 0 )
{
cout << "\nRight rotation." ;
root -> left = temp1 -> right ;
temp1 -> right = root ;
if ( temp1 -> balfact == 0 )
{
root -> balfact = 1 ;
temp1 -> balfact = -1 ;
*h = FALSE ;
}
else
{
root -> balfact = temp1 -> balfact = 0 ;
}
root = temp1 ;
}
else
{
cout << "\nDouble rotation, left then right." ;
temp2 = temp1 -> right ;
temp1 -> right = temp2 -> left ;
temp2 -> left = temp1 ;
root -> left = temp2 -> right ;
temp2 -> right = root ;
if ( temp2 -> balfact == 1 )
root -> balfact = -1 ;
else
root -> balfact = 0 ;
if ( temp2-> balfact == -1 )
temp1 -> balfact = 1 ;
else
temp1 -> balfact = 0 ;
root = temp2 ;
temp2 -> balfact = 0 ;
}
}
return ( root ) ;
}
void avltree :: setroot ( AVLNode *avl )
{
root = avl ;
}
avltree :: ~avltree( )
{
deltree ( root ) ;
}
void avltree :: deltree ( AVLNode *root )
{
if ( root != NULL )
{
deltree ( root -> left ) ;
deltree ( root -> right ) ;
}
delete ( root ) ;
}
void main( )
{
avltree at ;
AVLNode *avl = NULL ;
int h ;
clrscr();
avl = at.insert ( 20, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 6, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 29, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 5, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 12, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 25, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 32, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 10, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 15, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 27, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 13, &h ) ;
at.setroot ( avl ) ;
cout << endl << "AVL tree:\n" ;
at.display ( avl ) ;
avl = at.deldata ( avl, 20, &h ) ;
at.setroot ( avl ) ;
avl = at.deldata ( avl, 12, &h ) ;
at.setroot ( avl ) ;
cout << endl << "AVL tree after deletion of a node:\n" ;
at.display ( avl ) ;
getch();
}
#include<iostream>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u;
void main()
{
int m,c;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES Cost\n";
for(k=1;k<=m;k++)
{
cin >>i>>j>>c;
cost[i][j]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;
cout <<"ORDER OF VISITED VERTICES";
k=1;
while(k<n)
{
m=31999;
if(k==1)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(cost[i][j]<m)
{
m=cost[i][j];
u=i;
}
}
else
{
for(j=n;j>=1;j--)
if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
m=cost[v][j];
u=j;
}
}
cost[v][u]=31999;
v=u;
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}
OUTPUT
enterno of vertices7
ente no of edges9
EDGES Cost
1 6 10
6 5 25
5 4 22
4 3 12
3 2 16
2 7 14
5 7 24
4 7 18
1 2 28
ORDER OF VISITED VERTICES1 6 5 4 3 2
7) Write a C++ Program for an AVL Tree Implementation ?
ans:
/*C++ program to implement AVL Tree & its Operations*/
#include <iostream.h>#include <stdlib.h>
#include<constream.h>
#define FALSE 0
#define TRUE 1
struct AVLNode
{
int data ;
int balfact ;
AVLNode *left ;
AVLNode *right ;
} ;
class avltree
{
private :
AVLNode *root ;
public :
avltree( ) ;
AVLNode* insert ( int data, int *h ) ;
static AVLNode* buildtree ( AVLNode *root, int data, int *h ) ;
void display( AVLNode *root ) ;
AVLNode* deldata ( AVLNode* root, int data, int *h ) ;
static AVLNode* del ( AVLNode *node, AVLNode* root, int *h ) ;
static AVLNode* balright ( AVLNode *root, int *h ) ;
static AVLNode* balleft ( AVLNode* root, int *h ) ;
void setroot ( AVLNode *avl ) ;
~avltree( ) ;
static void deltree ( AVLNode *root ) ;
} ;
avltree :: avltree( )
{
root = NULL ;
}
AVLNode* avltree :: insert ( int data, int *h )
{
root = buildtree ( root, data, h ) ;
return root ;
}
AVLNode* avltree :: buildtree ( AVLNode *root, int data, int *h )
{
AVLNode *node1, *node2 ;
if ( root == NULL )
{
root = new AVLNode ;
root -> data = data ;
root -> left = NULL ;
root -> right = NULL ;
root -> balfact = 0 ;
*h = TRUE ;
return ( root ) ;
}
if ( data < root -> data )
{
root -> left = buildtree ( root -> left, data, h ) ;
// If left subtree is higher
if ( *h )
{
switch ( root -> balfact )
{
case 1 :
node1 = root -> left ;
if ( node1 -> balfact == 1 )
{
cout << "\nRight rotation." ;
root -> left = node1 -> right ;
node1 -> right = root ;
root -> balfact = 0 ;
root = node1 ;
}
else
{
cout << "\nDouble rotation, left then right." ;
node2 = node1 -> right ;
node1 -> right = node2 -> left ;
node2 -> left = node1 ;
root -> left = node2 -> right ;
node2 -> right = root ;
if ( node2 -> balfact == 1 )
root -> balfact = -1 ;
else
root -> balfact = 0 ;
if ( node2 -> balfact == -1 )
node1 -> balfact = 1 ;
else
node1 -> balfact = 0 ;
root = node2 ;
}
root -> balfact = 0 ;
*h = FALSE ;
break ;
case 0 :
root -> balfact = 1 ;
break ;
case -1 :
root -> balfact = 0 ;
*h = FALSE ;
}
}
}
if ( data > root -> data )
{
root -> right = buildtree ( root -> right, data, h ) ;
if ( *h )
{
switch ( root -> balfact )
{
case 1 :
root -> balfact = 0 ;
*h = FALSE ;
break ;
case 0 :
root -> balfact = -1 ;
break ;
case -1 :
node1 = root -> right ;
if ( node1 -> balfact == -1 )
{
cout << "\nLeft rotation." ;
root -> right = node1 -> left ;
node1 -> left = root ;
root -> balfact = 0 ;
root = node1 ;
}
else
{
cout << "\nDouble rotation, right then left." ;
node2 = node1 -> left ;
node1 -> left = node2 -> right ;
node2 -> right = node1 ;
root -> right = node2 -> left ;
node2 -> left = root ;
if ( node2 -> balfact == -1 )
root -> balfact = 1 ;
else
root -> balfact = 0 ;
if ( node2 -> balfact == 1 )
node1 -> balfact = -1 ;
else
node1 -> balfact = 0 ;
root = node2 ;
}
root -> balfact = 0 ;
*h = FALSE ;
}
}
}
return ( root ) ;
}
void avltree :: display ( AVLNode* root )
{
if ( root != NULL )
{
display ( root -> left ) ;
cout << root -> data << "\t" ;
display ( root -> right ) ;
}
}
AVLNode* avltree :: deldata ( AVLNode *root, int data, int *h )
{
AVLNode *node ;
if ( root -> data == 13 )
cout << root -> data ;
if ( root == NULL )
{
cout << "\nNo such data." ;
return ( root ) ;
}
else
{
if ( data < root -> data )
{
root -> left = deldata ( root -> left, data, h ) ;
if ( *h )
root = balright ( root, h ) ;
}
else
{
if ( data > root -> data )
{
root -> right = deldata ( root -> right, data, h ) ;
if ( *h )
root = balleft ( root, h ) ;
}
else
{
node = root ;
if ( node -> right == NULL )
{
root = node -> left ;
*h = TRUE ;
delete ( node ) ;
}
else
{
if ( node -> left == NULL )
{
root = node -> right ;
*h = TRUE ;
delete ( node ) ;
}
else
{
node -> right = del ( node -> right, node, h ) ;
if ( *h )
root = balleft ( root, h ) ;
}
}
}
}
}
return ( root ) ;
}
AVLNode* avltree :: del ( AVLNode *succ, AVLNode *node, int *h )
{
AVLNode *temp = succ ;
if ( succ -> left != NULL )
{
succ -> left = del ( succ -> left, node, h ) ;
if ( *h )
succ = balright ( succ, h ) ;
}
else
{
temp = succ ;
node -> data = succ -> data ;
succ = succ -> right ;
delete ( temp ) ;
*h = TRUE ;
}
return ( succ ) ;
}
AVLNode* avltree :: balright ( AVLNode *root, int *h )
{
AVLNode *temp1, *temp2 ;
switch ( root -> balfact )
{
case 1 :
root -> balfact = 0 ;
break ;
case 0 :
root -> balfact = -1 ;
*h = FALSE ;
break ;
case -1 :
temp1 = root -> right ;
if ( temp1 -> balfact <= 0 )
{
cout << "\nLeft rotation." ;
root -> right = temp1 -> left ;
temp1 -> left = root ;
if ( temp1 -> balfact == 0 )
{
root -> balfact = -1 ;
temp1 -> balfact = 1 ;
*h = FALSE ;
}
else
{
root -> balfact = temp1 -> balfact = 0 ;
}
root = temp1 ;
}
else
{
cout << "\nDouble rotation, right then left." ;
temp2 = temp1 -> left ;
temp1 -> left = temp2 -> right ;
temp2 -> right = temp1 ;
root -> right = temp2 -> left ;
temp2 -> left = root ;
if ( temp2 -> balfact == -1 )
root -> balfact = 1 ;
else
root -> balfact = 0 ;
if ( temp2 -> balfact == 1 )
temp1 -> balfact = -1 ;
else
temp1 -> balfact = 0 ;
root = temp2 ;
temp2 -> balfact = 0 ;
}
}
return ( root ) ;
}
AVLNode* avltree :: balleft ( AVLNode *root, int *h )
{
AVLNode *temp1, *temp2 ;
switch ( root -> balfact )
{
case -1 :
root -> balfact = 0 ;
break ;
case 0 :
root -> balfact = 1 ;
*h = FALSE ;
break ;
case 1 :
temp1 = root -> left ;
if ( temp1 -> balfact >= 0 )
{
cout << "\nRight rotation." ;
root -> left = temp1 -> right ;
temp1 -> right = root ;
if ( temp1 -> balfact == 0 )
{
root -> balfact = 1 ;
temp1 -> balfact = -1 ;
*h = FALSE ;
}
else
{
root -> balfact = temp1 -> balfact = 0 ;
}
root = temp1 ;
}
else
{
cout << "\nDouble rotation, left then right." ;
temp2 = temp1 -> right ;
temp1 -> right = temp2 -> left ;
temp2 -> left = temp1 ;
root -> left = temp2 -> right ;
temp2 -> right = root ;
if ( temp2 -> balfact == 1 )
root -> balfact = -1 ;
else
root -> balfact = 0 ;
if ( temp2-> balfact == -1 )
temp1 -> balfact = 1 ;
else
temp1 -> balfact = 0 ;
root = temp2 ;
temp2 -> balfact = 0 ;
}
}
return ( root ) ;
}
void avltree :: setroot ( AVLNode *avl )
{
root = avl ;
}
avltree :: ~avltree( )
{
deltree ( root ) ;
}
void avltree :: deltree ( AVLNode *root )
{
if ( root != NULL )
{
deltree ( root -> left ) ;
deltree ( root -> right ) ;
}
delete ( root ) ;
}
void main( )
{
avltree at ;
AVLNode *avl = NULL ;
int h ;
clrscr();
avl = at.insert ( 20, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 6, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 29, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 5, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 12, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 25, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 32, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 10, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 15, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 27, &h ) ;
at.setroot ( avl ) ;
avl = at.insert ( 13, &h ) ;
at.setroot ( avl ) ;
cout << endl << "AVL tree:\n" ;
at.display ( avl ) ;
avl = at.deldata ( avl, 20, &h ) ;
at.setroot ( avl ) ;
avl = at.deldata ( avl, 12, &h ) ;
at.setroot ( avl ) ;
cout << endl << "AVL tree after deletion of a node:\n" ;
at.display ( avl ) ;
getch();
}
/*
*/
No comments:
Post a Comment