0% found this document useful (0 votes)
7 views5 pages

Dasl 10 P

The document contains a C++ program that implements MinHeap and MaxHeap data structures, allowing users to manage marks through a menu-driven interface. Users can add single or multiple marks, retrieve maximum and minimum marks, and display the heaps as binary trees. The program demonstrates basic heap operations including insertion and maintenance of heap properties.

Uploaded by

akshatagundure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Dasl 10 P

The document contains a C++ program that implements MinHeap and MaxHeap data structures, allowing users to manage marks through a menu-driven interface. Users can add single or multiple marks, retrieve maximum and minimum marks, and display the heaps as binary trees. The program demonstrates basic heap operations including insertion and maintenance of heap properties.

Uploaded by

akshatagundure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Masal Punam Bhausaheb

Roll No:01
Assignment No:10

INPUT:
#include <iostream>
#include <vector>
using namespace std;

// Class for MinHeap


class MinHeap {
private:
vector<int> heap;

// Helper function to maintain heap property (heapify up)


void heapifyUp(int index) {
while (index > 0 && heap[index] < heap[(index - 1) / 2]) {
swap(heap[index], heap[(index - 1) / 2]);
index = (index - 1) / 2;
}
}

// Helper function to maintain heap property (heapify down)


void heapifyDown(int index) {
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;

if (left < heap.size() && heap[left] < heap[smallest])


smallest = left;
if (right < heap.size() && heap[right] < heap[smallest])
smallest = right;
if (smallest != index) {
swap(heap[index], heap[smallest]);
heapifyDown(smallest);
}
}

public:
// Function to insert a value into the heap
void insert(int value) {
heap.push_back(value);
heapifyUp(heap.size() - 1);
}

// Function to get the minimum value


int getMin() {
if (heap.empty()) {
cout << "Heap is empty!\n";
return -1;
}
return heap[0];
}

// Function to display the heap as a complete binary tree


void displayLevelwise() {
cout << "Heap as a Binary Tree:\n";
int index = 0, level = 0;

while (index < heap.size()) {


int nodesInLevel = 1 << level; // Equivalent to 2^level
for (int i = 0; i < nodesInLevel && index < heap.size(); ++i) {
cout << heap[index++] << " ";
}
cout << endl; // Move to the next level
++level;
}
}
};

// Class for MaxHeap


class MaxHeap {
private:
vector<int> heap;

// Helper function to maintain heap property (heapify up)


void heapifyUp(int index) {
while (index > 0 && heap[index] > heap[(index - 1) / 2]) {
swap(heap[index], heap[(index - 1) / 2]);
index = (index - 1) / 2;
}
}

// Helper function to maintain heap property (heapify down)


void heapifyDown(int index) {
int largest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;

if (left < heap.size() && heap[left] > heap[largest])


largest = left;
if (right < heap.size() && heap[right] > heap[largest])
largest = right;
if (largest != index) {
swap(heap[index], heap[largest]);
heapifyDown(largest);
}
}

public:
// Function to insert a value into the heap
void insert(int value) {
heap.push_back(value);
heapifyUp(heap.size() - 1);
}

// Function to get the maximum value


int getMax() {
if (heap.empty()) {
cout << "Heap is empty!\n";
return -1;
}
return heap[0];
}

// Function to display the heap as a complete binary tree


void displayLevelwise() {
cout << "Heap as a Binary Tree:\n";
int index = 0, level = 0;

while (index < heap.size()) {


int nodesInLevel = 1 << level; // Equivalent to 2^level
for (int i = 0; i < nodesInLevel && index < heap.size(); ++i) {
cout << heap[index++] << " ";
}
cout << endl; // Move to the next level
++level;
}
}
};

// Main menu-driven function


int main() {
MinHeap minHeap;
MaxHeap maxHeap;
int choice, mark, numMarks;

do {
cout << "\nMenu:\n";
cout << "1. Add a Single Mark\n";
cout << "2. Add Multiple Marks\n"; // New menu option
cout << "3. Find Maximum Marks\n";
cout << "4. Find Minimum Marks\n";
cout << "5. Display Marks Level-wise\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter mark to add: ";
cin >> mark;
minHeap.insert(mark);
maxHeap.insert(mark);
break;
case 2:
cout << "Enter the number of marks to add: ";
cin >> numMarks;
cout << "Enter the marks: ";
for (int i = 0; i < numMarks; ++i) {
cin >> mark;
minHeap.insert(mark);
maxHeap.insert(mark);
}
break;

case 3:
cout << "Maximum Marks: " << maxHeap.getMax() << endl;
break;

case 4:
cout << "Minimum Marks: " << minHeap.getMin() << endl;
break;

case 5:
cout << "Min-Heap ";
minHeap.displayLevelwise();
cout << "Max-Heap ";
maxHeap.displayLevelwise();
break;

case 6:
cout << "Exiting...\n";
break;

default:
cout << "Invalid choice! Try again.\n";
}
} while (choice != 6);

return 0;
}

Output:

Menu:
1. Add a Single Mark
2. Add Multiple Marks
3. Find Maximum Marks
4. Find Minimum Marks
5. Display Marks Level-wise
6. Exit
Enter your choice: 2
Enter the number of marks to add: 5
Enter the marks: 45
12
67
88
29

Menu:
1. Add a Single Mark
2. Add Multiple Marks
3. Find Maximum Marks
4. Find Minimum Marks
5. Display Marks Level-wise
6. Exit
Enter your choice: 3
Maximum Marks: 88

Menu:
1. Add a Single Mark
2. Add Multiple Marks
3. Find Maximum Marks
4. Find Minimum Marks
5. Display Marks Level-wise
6. Exit
Enter your choice: 4
Minimum Marks: 12

Menu:
1. Add a Single Mark
2. Add Multiple Marks
3. Find Maximum Marks
4. Find Minimum Marks
5. Display Marks Level-wise
6. Exit
Enter your choice: 5
Min-Heap Heap as a Binary Tree:
12
29 67
88 45
Max-Heap Heap as a Binary Tree:
88
67 45
12 29

Menu:
1. Add a Single Mark
2. Add Multiple Marks
3. Find Maximum Marks
4. Find Minimum Marks
5. Display Marks Level-wise
6. Exit
Enter your choice: 6
Exiting...

You might also like