Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Name: Muhammad Muneeb

Roll Number: RP21-EE-407


SUBMITTED TO: Dr Kamal Shahid
Program 1
Implement a Heap class in C++. Your implementation should include functions for inserting
a node, deleting a node, searching for a value in the tree, and printing the tree in order

CODE:
#include <iostream>

#include <climits> // for INT_MAX


using namespace std;
// Class for Min Heap
class MinHeap {
int *heapArray;
int heapSize;
int capacity;
public:
// Constructor
MinHeap(int cap) {
heapSize = 0;
capacity = cap;
heapArray = new int[cap];
}
// Helper functions to get the parent, left child, and right child indices
int parent(int i) { return (i - 1) / 2; }
int leftChild(int i) { return (2 * i + 1); }
int rightChild(int i) { return (2 * i + 2); }
// Function to insert a key into the heap
void insertKey(int key) {
if (heapSize == capacity) {
cout << "Heap overflow!" << endl;
return; }
heapSize++;
int i = heapSize - 1;
heapArray[i] = key;
// Fix the Min Heap property if violated
while (i != 0 && heapArray[parent(i)] > heapArray[i]) {
swap(heapArray[i], heapArray[parent(i)]);
i = parent(i);
}
}
// Function to decrease the value of a key at a given index
void decreaseKey(int i, int new_val) {
heapArray[i] = new_val;
while (i != 0 && heapArray[parent(i)] > heapArray[i]) {
swap(heapArray[i], heapArray[parent(i)]);
i = parent(i);
}
}
// Function to extract the minimum element from the heap
int extractMin() {
if (heapSize <= 0)
return INT_MAX;
if (heapSize == 1) {
heapSize--;
return heapArray[0];
}
int root = heapArray[0];
heapArray[0] = heapArray[heapSize - 1];
heapSize--;
minHeapify(0);
return root;
}
// Function to heapify a subtree with root at given index
void minHeapify(int i) {
int l = leftChild(i);
int r = rightChild(i);
int smallest = i;
if (l < heapSize && heapArray[l] < heapArray[i])
smallest = l;
if (r < heapSize && heapArray[r] < heapArray[smallest])
smallest = r;
if (smallest != i) {
swap(heapArray[i], heapArray[smallest]);
minHeapify(smallest);
}
}
// Function to search for a key in the heap
int linearSearch(int key) {
for (int i = 0; i < heapSize; i++) {
if (heapArray[i] == key)
return i;
}
return -1;
}
// Function to get the minimum element in the heap
int getMin() {
if (heapSize <= 0)
return INT_MAX;
return heapArray[0];
}
// Function to print the heap
void printHeap() {
cout << "Current Heap: ";
for (int i = 0; i < heapSize; ++i) {
cout << heapArray[i] << " ";
}
cout << endl;
}
// Function to get the height of the heap
int height(int i) {
if (i >= heapSize)
return -1;
int leftHeight = height(leftChild(i));
int rightHeight = height(rightChild(i));

if (leftHeight > rightHeight)


return leftHeight + 1;
else
return rightHeight + 1;
}
// Function to delete a key at a given index
void deleteKey(int i) {
decreaseKey(i, INT_MIN);
extractMin();
}
};
int main() {
MinHeap minHeap(10);
int option;
int key;
do {
cout << "\n\nChoose an option:\n\n";
cout << "1. Insert a key\n";
cout << "2. Decrease the value of a key\n";
cout << "3. Extract the minimum element\n";
cout << "4. Search for a key\n";
cout << "5. Delete a key\n";
cout << "6. Get the minimum element\n";
cout << "7. Print the heap\n";
cout << "8. Get the height of the heap\n";
cout << "9. Exit\n\n";
cin >> option;
switch(option) {
case 1:
cout << "Enter key to insert: ";
cin >> key;
minHeap.insertKey(key);
break;
case 2:
int index, value;
cout << "Enter index and new value: ";
cin >> index >> value;
minHeap.decreaseKey(index, value);
break;
case 3:
minHeap.extractMin();
break;
case 4:
cout << "Enter key to search for: ";
cin >> key;
cout << "Index of key: " << minHeap.linearSearch(key) << endl;
break;
case 5:
cout << "Enter index of key to delete: ";
cin >> index;
minHeap.deleteKey(index);
break;
case 6:
cout << "Minimum element: " << minHeap.getMin() << endl;
break;
case 7:
cout << "Heap: ";
minHeap.printHeap();
break;
case 8:
cout << "Heap height: " << minHeap.height(0) << endl;
break;
case 9:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid option.\n";
}
} while (option != 9);
}

Program 2
Write a program that reads a list of integers provided by the user and builds a Heap tree
from them. Then, prompt the user to enter a value and search for it in the tree. If the value is
found, print its path in the tree; otherwise, print a message indicating that the value is not in
the tree.

CODE:
#include <iostream>
#include <vector>

using namespace std;

void heapify(vector<int>& arr, int i) {


int n = arr.size();
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest]) {


largest = l;
}

if (r < n && arr[r] > arr[largest]) {


largest = r;
}

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, largest);
}
}

void buildHeap(vector<int>& arr) {


int n = arr.size();
for (int i = (n / 2) - 1; i >= 0; i--) {
heapify(arr, i);
}
}

bool search(vector<int>& arr, int x, int& index) {


int n = arr.size();
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
index = i;
return true;
}
}
return false;
}

void printPath(vector<int>& arr, int index) {


while (index > 0) {
cout << arr[index] << " <- ";
index = (index - 1) / 2;
}
cout << arr[0] << endl;
}

int main() {
vector<int> arr;
int n;
cout << "Enter the number of integers: ";
cin >> n;
cout << "Enter " << n << " integers:\n";
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr.push_back(x);
}

buildHeap(arr);

int x;
cout << "Enter a value to search for: ";
cin >> x;

int index;
if (search(arr, x, index)) {
cout << x << " is in the tree.\n";
cout << "Path to " << x << ": ";
printPath(arr, index);
} else {
cout << x << " is not in the tree.\n";
}

return 0;
}

Program 3
Implement a function to determine if a binary tree is a Heap tree. The function should take
the root node of the tree as its input and return a boolean value indicating whether or not the
tree is a valid heap
CODE:
#include <iostream>
using namespace std;

bool isHeap(int arr[], int n) {


for (int i = 0; i < (n / 2) - 1; i++) {
if (arr[i] < arr[2 * i + 1] || arr[i] < arr[2 * i + 2]) {
return false;
}
}
if (n % 2 == 0 && arr[n/2-1] < arr[n-1]) {
return false;
}
return true;
}

int main() {
int arr[] = {10, 8, 9, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Select an operation:\n";


cout << "1. Check if binary tree is a heap\n";
cout << "2. Exit\n";

int choice;
cin >> choice;

switch (choice) {
case 1:
if (isHeap(arr, n)) {
cout << "The binary tree is a heap\n";
} else {
cout << "The binary tree is not a heap\n";
}
break;
case 2:
return 0;
default:
cout << "Invalid choice\n";
}

return 0;
}

Program 4
Implement a function to find the height of a heap tree. The function should take the root
node of the tree as its input and return the height of the tree as an integer value.

CODE:
#include <iostream>
#include <cmath>

using namespace std;

void heapify(int arr[], int n, int i) {


int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest]) {


largest = l;
}
if (r < n && arr[r] > arr[largest]) {
largest = r;
}

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void buildHeap(int arr[], int n) {


for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
}

int getHeight(int arr[], int n) {


if (n <= 0) {
return -1;
}

int height = 0;
int i = 0;

while (true) {
int next = (i << 1) + 1;

if (next >= n) {
break;
}

if (next + 1 < n && arr[next + 1] > arr[next]) {


next++;
}

i = next;
height++;
}

return height;
}

int main() {
int n;
cout << "Enter the number of integers: ";
cin >> n;

int arr[n];
cout << "Enter the integers: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

buildHeap(arr, n);

int height = getHeight(arr, n);


cout << "Height of the Heap tree is " << height << endl;

return 0;
}

Program 5
Write a program that reads a list of integers from provided by the user and builds a Heap tree
from them. Then, prompt the user to enter a range of values (e.g., "Enter a lower bound and
an upper bound: "). The program should print all the values in the tree that fall within the
specified range
CODE;
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void heapify(vector<int>& arr, int n, int i) {


int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest]) {


largest = l;
}

if (r < n && arr[r] > arr[largest]) {


largest = r;
}

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void buildHeap(vector<int>& arr, int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
}

void printRange(vector<int>& arr, int n, int lower, int upper) {


for (int i = 0; i < n; i++) {
if (arr[i] >= lower && arr[i] <= upper) {
cout << arr[i] << " ";
}
}
cout << endl;
}

int main() {
int n;
cout << "Enter the number of integers: ";
cin >> n;

vector<int> arr(n);
cout << "Enter the integers: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

buildHeap(arr, n);

int lower, upper;


cout << "Enter a lower bound and an upper bound: ";
cin >> lower >> upper;

cout << "Values within the range [" << lower << ", " << upper << "]: ";
printRange(arr, n, lower, upper);

return 0;
}

Program 6
Implement a function to delete a node from a Heap tree. The function should take the root
node of the tree and the value to be deleted as its input, and return the new root node of the
tree
CODE:
#include <iostream>
#include <algorithm>

using namespace std;

void heapify(int arr[], int n, int i) {


int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest]) {


largest = l;
}

if (r < n && arr[r] > arr[largest]) {


largest = r;
}
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void deleteNode(int arr[], int& n, int val) {


int i;
for (i = 0; i < n; i++) {
if (arr[i] == val) {
break;
}
}
if (i == n) {
return; // value not found in the tree
}

arr[i] = arr[n-1];
n--;

heapify(arr, n, i);
}

int main() {
int arr[] = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1};
int n = sizeof(arr) / sizeof(arr[0]);

int choice, val;


bool quit = false;
while (!quit) {
cout << "Enter 1 to delete a node from the tree\n"
<< "Enter 2 to quit\n"
<< "Choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to delete: ";
cin >> val;
deleteNode(arr, n, val);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
break;
case 2:
quit = true;
break;
default:
cout << "Invalid choice\n";
}
}

return 0;
}

You might also like