pgm1 8
pgm1 8
#include <iostream.h>
void towerOfHanoi(int n, char from_rod, char to_rod,
char aux_rod)
{
if (n == 0) {
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod
<< " to rod " << to_rod << endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main()
{
int N = 3;
towerOfHanoi(N, 'A', 'C', 'B');
return 0;
}
Output
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
Time complexity: O(2N), There are two possibilities for every disk. Therefore, 2
* 2 * 2 * . . . * 2(N times) is 2 N
Auxiliary Space: O(N), Function call stack space
2. write a program to traverse through binary search tree using
traversals in C++
#include<iostream>
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *createNode(int val) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
struct node* insertNode(struct node* node, int val) {
if (node == NULL) return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}
int main() {
struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
insertNode(root, 3);
cout<<"In-Order traversal of the Binary Search Tree is: ";
inorder(root);
return 0;
}
Output
In-Order traversal of the Binary Search Tree is: 1 2 3 4 5 9
if (front == rear) {
front = -1;
rear = -1;
} else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<" ";
f++;
}
} else {
while (f <= n - 1) {
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r) {
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}
Output
The output of the above program is as follows −
1)Insert
2)Delete
3)Display
4)Exit
Enter choice : 1
Input for insertion:
Enter choice : 1
Input for insertion:
Enter choice : 1
Input for insertion:
Enter choice : 1
Input for insertion:
Enter choice : 1
Input for insertion:
Enter choice : 2
Element deleted from queue is : 5
Enter choice : 2
Element deleted from queue is : 3
Enter choice : 2
Element deleted from queue is : 2
Enter choice : 1
Input for insertion: 6
Enter choice : 3
Queue elements are :
796
Enter choice : 4
Exit
#include<bits/stdc++.h>
using namespace std;
struct Queue
{
// Circular Queue
int size;
int *arr;
};
else{
rear++;
arr[rear] = value;
}
}
if (front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return data;
else
{
for (int i = front; i < size; i++)
cout<<arr[i]<<" ";
int main ()
{
Queue q (5);
q.displayQueue ();
q.enQueue (50);
q.enQueue (60);
q.enQueue (70);
q.displayQueue ();
return 0;
}
Output:
Deleted element = 10
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
return pivotIndex;
}
void quickSort(int arr[], int start, int end)
{
// base case
if (start >= end)
return;
int main()
{
int arr[] = { 9, 3, 4, 2, 1, 8 };
int n = 6;
quickSort(arr, 0, n - 1);
return 0;
}
Output
123489
6. write a program to solve number of elements in ascending order
using heapsort
#include <iostream>
// Driver program
int main()
{
int arr[] = { 60 ,20 ,40 ,70, 30, 10};
int n = sizeof(arr) / sizeof(arr[0]);
//heapify algorithm
// the loop must go reverse you will get after analyzing manually
// (i=n/2 -1) because other nodes/ ele's are leaf nodes
// (i=n/2 -1) for 0 based indexing
// (i=n/2) for 1 based indexing
for(int i=n/2 -1;i>=0;i--){
heapify(arr,n,i);
}
heapSort(arr, n);
cout << "Sorted array is \n";
printArray(arr, n);
return 0;
}
Output
After heapifying array is
70 60 40 20 30 10
Sorted array is
10 20 30 40 60 70
note
Time complexity : O(N*logN)
Auxiliary space: O(1)
7. Write a program to solve the knap sack problem using greedy method
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int v;
int w;
float d;
} Item;
void input(Item items[],int sizeOfItems) {
cout << "Enter total "<< sizeOfItems <<" item's values and weight" <<
endl;
for(int i = 0; i < sizeOfItems; i++) {
cout << "Enter "<< i+1 << " V ";
cin >> items[i].v;
cout << "Enter "<< i+1 << " W ";
cin >> items[i].w;
}
}
void display(Item items[], int sizeOfItems) {
int i;
cout << "values: ";
for(i = 0; i < sizeOfItems; i++) {
cout << items[i].v << "\t";
}
cout << endl << "weight: ";
for (i = 0; i < sizeOfItems; i++) {
cout << items[i].w << "\t";
}
cout << endl;
}
bool compare(Item i1, Item i2) {
return (i1.d > i2.d);
}
float knapsack(Item items[], int sizeOfItems, int W) {
int i, j;
float totalValue = 0, totalWeight = 0;
for (i = 0; i < sizeOfItems; i++) {
items[i].d = (float)items[i].v / items[i].w; //typecasting done (v is int
and w is also int so we get final value of d as int)
}
sort(items, items+sizeOfItems, compare);
/*
uncomment if u need to check the data after sortingis done
cout << "values : ";
for(i = 0; i < sizeOfItems; i++) {
cout << items[i].v << "\t";
}
cout << endl << "weights: ";
for (i = 0; i < sizeOfItems; i++) {
cout << items[i].w << "\t";
}
cout << endl << "ratio : ";
for (i = 0; i < sizeOfItems; i++) {
cout << items[i].d << "\t";
}
cout << endl;
*/
for(i=0; i<sizeOfItems; i++) {
if(totalWeight + items[i].w<= W) {
totalValue += items[i].v ;
totalWeight += items[i].w;
} else {
int wt = W-totalWeight;
totalValue += (wt * items[i].d);
totalWeight += wt;
break;
}
}
cout << "Total weight in bag " << totalWeight<<endl;
return totalValue;
}
int main() {
int W;
Item items[4];
input(items, 4);
cout << "Entered data \n";
display(items,4);
cout<< "Enter Knapsack weight \n";
cin >> W;
float mxVal = knapsack(items, 4, W);
cout << "Max value for "<< W <<" weight is "<< mxVal;
}
7.. Write a program to solve the knap sack problem using greedy method
#include <bits/stdc++.h>
using namespace std;
// if remaining_weight is greater
// than the weight of i'th item
if (N >= 0
&& N >= weight[it->second]) {
// Driver Code
int main()
{
// Size of list
int size = 4;
// Profit of items
profit[0] = 100, profit[1] = 280,
profit[2] = 120, profit[3] = 120;
// Weight of items
weight[0] = 10, weight[1] = 40,
weight[2] = 20, weight[3] = 24;
// Capacity of knapsack
int N = 60;
// Function Call
maxProfit(profit, weight, N);
}
Output:
Maximum profit earned is:440
Note
Time Complexity: O(N)
Auxiliary Space: O(N)
Write a program to search for an element in a tree using divide and conquer
#include<bits/stdc++.h>
{
if(l<=r)
{
int mid = l+((r-l)/2);
if(arr[mid]==key)
return 1;
else if(arr[mid]>key)
return Binary_search(arr,l,mid-1,key);
else
return Binary_search(arr,mid+1,r,key);
}
return 0;
}
int main()
{
int arr[]={3,4,5,6,7,8,4,5,2,8,9,13,14,17,19};
int n=sizeof(arr)/sizeof(arr[0]);
int key=79;
sort(arr,arr+n);
if(Binary_search(arr,0,n-1,key))
cout<<"key is found in given array"<<endl;
else
cout<<"key is not found in given array"<<endl;
}
Output
2 3 4 4 5 5 6 7 8 8 9 13 14 17 19