Professional Documents
Culture Documents
ADA LAB MANUAL - For Reference
ADA LAB MANUAL - For Reference
Course Details
Branch : CSE
Semester : 4th
12 2 Write a program to create binary tree and also perform following tree
traversals.
• In-order
• Pre-Order
• Post-Order
13 1 Write a program for 15 puzzle problem.
#include<stdio.h>
#include<conio.h>
#define SIZE 10
int n;
void main()
{ int A[SIZE],KEY,i,flag,low,high;
int BinSearch(int A[SIZE],int KEY,int low,int high);
clrscr();
printf("\n How Many elements in an array?");
scanf("%d",&n);
printf("\n Enter The Elements");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("\n Enter the element which is to be searched");
scanf("%d",&KEY);
low=0;
high=n-1;
flag=BinSearch(A,KEY,low,high);
printf("\n The element is at A[%d] location",flag);
getch();}
int BinSearch(int A[SIZE],int KEY,int low,int high)
{ int m;
m=(low+high)/2; //mid of the array is obtained
if(KEY==A[m])
return m;
else if(KEY<A[m])
BinSearch(A,KEY,low,m-1);//search the left sub list
else
BinSearch(A,KEY,m+1,high);//search the right sub list
}
PROGRAM 2: Write a program for Insertion Sort.
#include<stdio.h>
#include<conio.h>
int main()
{int a[100],n,i;
int j,temp; //Iterate start from first element
printf("Enter the number of element you want to Sort : ");
scanf("%d",&n);
printf("Enter Elements in the list : ");
for(i = 0; i < n; i++)
{ scanf("%d",&a[i]); }
}
for(i=1; i<n; i++) //staring from the second element.
{ temp= a[i]; //selecting the next element to be inserted.
for(j = i-1; j>=0;j--)//inserting the element in previously sorted sub array
{
If(a[j]>temp)// check whether second element is greater than
{ first element
a[j+1] = a[j]; //insert first element at second
}
a[j+1] = temp; }//insert the element at identified location
}
Printf("Sorted list : "); //print sorted array
for(i = 0; i < n; i++ )
{
printf("%d\t",a[i]);
}}
PROGRAM 3: Write a program for Merge Sort.
# include<conio.h>
# include<stdio.h>
# include<stdlib.h>
int n;
void main()
{ int A[10], int i, low, high;
void MergeSort(int A[10],int low,int high);
void Display(int A[10]);
clrscr();
printf("\n\t\t Merge Sort \n"); printf("\n Enter the length of list :");
scanf("%d",&n);
printf("\n Enter list elements :");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
low=0;
high=n-1;
MergeSort(A,low,high);
Display(A);
getch();
}/* This function is to split the list into sublists */
void MergeSort(int A[10],int low,int high)
{ int mid;
void Combine(int A[10],int low,int mid,int high);
if(low < high)
{ mid = (low+high)/2;//split the list at mid
MergeSort(A,low,mid);//first sublist
MergeSort(A,mid+1,high);//second sublist
Combine(A,low,mid,high);//merging of two sublists
}} /* This function is for merging the two sublists */
void Combine(int A[10],int low,int mid,int high)
{ int i,j,k;
int temp[10];
k=low;
i=low;
j=mid+1;
while(i <= mid && j <= high)
{
if(A[i]<=A[j])
{ temp[k]=A[i];
i++;
k++;
}
else
{temp[k]=A[j];
j++;
k++;
} }
While (i<=mid)
{temp[k]=A[i];
i++;
k++;
}
while(j<=high)
{temp[k]=A[j];
j++;
k++;
}
//copy the elements from temp array to A
for(k=low;k<=high;k++)
A[k]=temp[k];
}/* function to display sorted array */
void Display(int A[10])
{ int i;
printf("\n\n The Sorted Array Is ...\n");
for(i=0;i<n;i++)
printf("%d\t",A[i]);
}
PROGRAM 4: Write a program for Quick Sort.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 10
void Quick(int A[SIZE],int,int);
int Partition(int A[SIZE],int,int);
void swap(int A[SIZE],int *,int *);
int n;
int main()
{ int i;
int A[SIZE];
clrscr();
printf("\n\t\t Quick Sort Method \n");
printf("\n Enter Total numbers to sort : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter %dth number : ",i+1);
scanf("%d",&A[i]);
}
Quick(A,0,n-1);
printf("\n\n\t Sorted Array Is: \n");
for(i=0;i<n;i++)
printf("\t%d ",A[i]);
getch();
return 0;
}/* This function is to sort the elements in a sublist */
void Quick(int A[SIZE],int low,int high)
{ int m,i;
if(low<high)
{ m=Partition(A,low,high);//setting pivot element
Quick(A,low,m-1);//splitting of list
Quick(A,m+1,high);//splitting of list }}
#include <iostream.h>
#include <conio.h>
class heap
{int k[11],size;
public:
void getdata(void);
friend void create_heap(heap &);
friend void sort_heap(heap &);
void showdata(void);
}; void heap :: getdata(void)
{ clrscr();
cout<<"Enter the size of Array :-";
cin>>size;
cout<<"\nEnter "<<size<<" Elements\n";
for(int i=1;i<=size;i++) //Creating heap from index1 instead of index0
cin>>k[i];
} void heap :: showdata(void)
{ clrscr();
cout<<"\n\nHeap Function Output\n\n";
for(int i=1;i<=size;i++)
cout<<k[i]<<endl;
} void create_heap(heap &a)
{ int q,i,j,key;
for(q=2;q<=a.size;q++)
{ i=q;
key=a.k[i];
j=i/2;
while(i>1 && key>a.k[j])
{ a.k[i]=a.k[j];
i=j;
j=i/2;
if(j<1)
j=1;
} a.k[i]=key;
}} void sort_heap(heap &a)
{ int q,i,j,key,temp;
for(q=a.size;q>=1;q--)
{ temp=a.k[1];
a.k[1]=a.k[q];
a.k[q]=temp;
i=1;
key=a.k[i];
j=2*i;
if(j+1 < q)
{ if(a.k[j+1] > a.k[j])
j++;
} while(j<=q-1 && key<a.k[j])
{ a.k[i]=a.k[j];
i=j;
j=2*i;
if(j+1 < q)
{ if(a.k[j+1] > a.k[j])
j++;
} else break;
} a.k[i]=key;
}} void main()
{ heap o1;
o1.getdata();
create_heap(o1);
sort_heap(o1);
o1.showdata();
getch();
}
PROGRAM 6: Write a program for solving greedy knapsack problem on a
given set of items.
#include <queue>
#include <iostream>
using namespace std;
struct node
{ int level; int profit; int weight; int bound;
};
int bound(node u, int n, int W, vector<int>pVa, vector<int>wVa)
{ int j = 0, k = 0; int totweight = 0; int result = 0;
if (u.weight>= W)
{ return 0;
} else
{ result = u.profit;
j = u.level + 1;
totweight = u.weight;
while ((j < n) && (totweight + wVa[j] <= W))
{ totweight = totweight + wVa[j];
result = result + pVa[j];
j++;
} k = j;
if (k < n)
{ result = result + (W - totweight) * pVa[k]/wVa[k];
} return result; }}
int knapsack(int n, int p[], int w[], int W)
{ queue<node> Q;
node u, v;
vector<int>pV;
vector<int>wV;
Q.empty();
for (int i = 0; i < n; i++)
{ pV.push_back(p[i]);
wV.push_back(w[i]);
} v.level = -1; v.profit = 0; v.weight = 0; int maxProfit = 0;
//v.bound = bound(v, n, W, pV, wV);
Q.push(v);
while (!Q.empty())
{ v = Q.front();
Q.pop();
if (v.level == -1)
{ u.level = 0;
} else if (v.level != (n - 1))
{ u.level = v.level + 1;
} u.weight = v.weight + w[u.level];
u.profit = v.profit + p[u.level];
u.bound = bound(u, n, W, pV, wV);
if(u.weight<= W &&u.profit>maxProfit)
{maxProfit = u.profit;
} if(u.bound>maxProfit)
{ Q.push(u);
} u.weight = v.weight;
u.profit = v.profit;
u.bound = bound(u, n, W, pV, wV);
if(u.bound>maxProfit)
{Q.push(u);} }
return maxProfit;}
int main()
{ int n ; int W,p[10],w[10];
cout<<"Enter no. of objects\n"; cin>>n;
cout<<"Enter weights and profits of objects\n";
for(int i=0;i<n;i++)
{ cout<<"Object "<<i+1<<"\n Weight : " cin>>w[i];
cout<<" Profit : "; cin>>p[i]; }
cout<<"\nEnter knapsack capacity\n"; cin>>W;
cout<<"\nMaximum profit : ";
cout<< knapsack(n, p, w, W) <<endl;
return 0; }
/* By this function we try the next free slot and check for proper positioning of queen */
PROGRAM 11: Write a program for graph traversals BFS & DFS
BFS
#include<iostream>
#include <list>
using namespace std;
// This class represents a directed graph using
// adjacency list representation
class Graph
{ int V; // No. of vertices
// Pointer to an array containing adjacency
// lists
list<int> *adj;
public:
Graph(int V); // Constructor
// function to add an edge to graph
void addEdge(int v, int w);
// prints BFS traversal from a given source s
void BFS(int s);
};
Graph::Graph(int V)
{ this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{ adj[v].push_back(w); // Add w to v’s list.
}
void Graph::BFS(int s)
{ // Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
int main()
{
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Depth First Traversal" " (starting from vertex 2) \n";
g.DFS(2);
return 0;
}
PROGRAM 12: Write a program to create binary tree and also perform
following tree traversals.
In-order, Pre-Order and Post-Order.
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child and a pointer to right child */
struct node
{ int data; struct node* left; struct node* right;
};
/* Helper function that allocates a new node with the given data and NULL left and
right pointers. */
struct node* newNode(int data)
{ struct node* node = (struct node*) malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* Given a binary tree, print its nodes according to the "bottom-up" postorder traversal.
*/ void printPostorder(struct node* node)
{ if (node == NULL)
return;
// first recur on left subtree
printPostorder(node->left);
// then recur on right subtree
printPostorder(node->right);
// now deal with the node
printf("%d ", node->data);
} /* Given a binary tree, print its nodes in inorder*/
void printInorder(struct node* node)
{ if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);