0% found this document useful (0 votes)
3 views3 pages

Lab 7 Problem 2

The document contains a C++ implementation of a MaxHeap class that supports operations such as inserting elements, deleting elements, displaying the heap, and sorting in descending order. It includes methods for maintaining the heap property through heapify operations. The main function demonstrates the usage of the MaxHeap class with a menu-driven interface for user interaction.

Uploaded by

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

Lab 7 Problem 2

The document contains a C++ implementation of a MaxHeap class that supports operations such as inserting elements, deleting elements, displaying the heap, and sorting in descending order. It includes methods for maintaining the heap property through heapify operations. The main function demonstrates the usage of the MaxHeap class with a menu-driven interface for user interaction.

Uploaded by

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

#include <iostream>

using namespace std;

#define MAX_SIZE 100

class MaxHeap {
private:
int heap[MAX_SIZE];
int size;

void heapifyUp(int index) {


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

void heapifyDown(int index) {


int leftChild, rightChild, largest;
while (2 * index + 1 < size) {
leftChild = 2 * index + 1;
rightChild = 2 * index + 2;
largest = index;

if (leftChild < size && heap[leftChild] > heap[largest])


largest = leftChild;
if (rightChild < size && heap[rightChild] > heap[largest])
largest = rightChild;

if (largest == index)
break;

swap(heap[index], heap[largest]);
index = largest;
}
}

public:
MaxHeap() {
size = 0;
}

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


for (int i = 0; i < n; ++i)
insert(arr[i]);
}

void insert(int value) {


if (size == MAX_SIZE) {
cout << "Heap is full!\n";
return;
}
heap[size] = value;
heapifyUp(size);
size++;
}
void deleteElement(int value) {
int index = -1;
for (int i = 0; i < size; ++i) {
if (heap[i] == value) {
index = i;
break;
}
}
if (index == -1) {
cout << "Element not found in heap.\n";
return;
}
heap[index] = heap[size - 1];
size--;
heapifyDown(index);
cout << "Element deleted successfully.\n";
}

void heapSortDescending() {
int originalSize = size;
while (size > 1) {
swap(heap[0], heap[size - 1]);
size--;
heapifyDown(0);
}
cout << "Sorted in descending order: ";
for (int i = 0; i < originalSize; ++i)
cout << heap[i] << " ";
cout << endl;
size = originalSize;
}

void displayHeap() {
cout << "Current heap: ";
for (int i = 0; i < size; ++i)
cout << heap[i] << " ";
cout << endl;
}
};

int main() {
MaxHeap h;
int initial[] = {45, 36, 54, 27, 63, 72, 61, 18};
int n = sizeof(initial) / sizeof(initial[0]);

h.buildHeap(initial, n);

int choice, value;


do {
cout << "1. Insert element\n";
cout << "2. Delete element\n";
cout << "3. Display heap\n";
cout << "4. Sort in descending order\n";
cout << "5. Exit\n";
cout << "Enter: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
h.insert(value);
break;
case 2:
cout << "Enter value to delete: ";
cin >> value;
h.deleteElement(value);
break;
case 3:
h.displayHeap();
break;
case 4:
h.heapSortDescending();
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}

} while (choice != 5);

return 0;
}

You might also like