Skip to content

Commit 66f9e4a

Browse files
authored
Merge pull request #131 from Sugyanee/add-max-heap-algorithm-cpp
Add Max Heap Algorithm in C++
2 parents 7a123f6 + ba4c679 commit 66f9e4a

1 file changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/****************************************
2+
Problem: Max Heap Implementation
3+
4+
Description:
5+
Implements a Max Heap data structure for integers. Supports the following operations:
6+
- Build a heap from a given array of integers.
7+
- Insert a new element into the heap while maintaining the max-heap property.
8+
- Retrieve the maximum element without removing it.
9+
- Extract (remove) the maximum element from the heap.
10+
- Delete a specific key from the heap.
11+
- Print the current elements of the heap.
12+
13+
The max-heap property ensures that for every node i, the value of i is greater than or
14+
equal to its children. The heap is stored internally as a dynamic array.
15+
16+
Time Complexity:
17+
- buildHeap: O(n) - heapify applied to all non-leaf nodes
18+
- insertValue: O(log n) - may traverse up the height of the heap
19+
- extractMax: O(log n) - heapify down from root
20+
- removeKey: O(log n) - heapify up/down as needed
21+
22+
Space Complexity: O(n) - stores the heap elements in a vector
23+
****************************************/
24+
25+
#include<bits/stdc++.h>
26+
using namespace std;
27+
28+
class MaxHeap {
29+
private:
30+
vector<int> heapArray; // Stores heap elements
31+
int heapSize; // Current size of the heap
32+
int heapCapacity; // Maximum capacity of the heap
33+
34+
// Function to move an element down the tree to maintain max-heap property
35+
void heapifyDown(int index) {
36+
int largest = index;
37+
int leftChild = 2 * index + 1;
38+
int rightChild = 2 * index + 2;
39+
40+
if (leftChild < heapSize && heapArray[leftChild] > heapArray[largest])
41+
largest = leftChild;
42+
43+
if (rightChild < heapSize && heapArray[rightChild] > heapArray[largest])
44+
largest = rightChild;
45+
46+
if (largest != index) {
47+
swap(heapArray[index], heapArray[largest]);
48+
heapifyDown(largest);
49+
}
50+
}
51+
52+
// Function to move an element up the tree to maintain max-heap property
53+
void heapifyUp(int index) {
54+
while (index > 0 && heapArray[(index - 1) / 2] < heapArray[index]) {
55+
swap(heapArray[index], heapArray[(index - 1) / 2]);
56+
index = (index - 1) / 2;
57+
}
58+
}
59+
60+
public:
61+
// Constructor
62+
MaxHeap(int capacity) {
63+
heapSize = 0;
64+
heapCapacity = capacity;
65+
heapArray.resize(capacity);
66+
}
67+
68+
// Build a max heap from an existing array
69+
void buildHeap(const vector<int>& input) {
70+
heapCapacity = input.size();
71+
heapSize = heapCapacity;
72+
heapArray = input;
73+
74+
for (int i = (heapSize - 1) / 2; i >= 0; i--) {
75+
heapifyDown(i);
76+
}
77+
}
78+
79+
// Insert a new value into the heap
80+
void insertValue(int value) {
81+
if (heapSize == heapCapacity) {
82+
heapCapacity *= 2;
83+
heapArray.resize(heapCapacity);
84+
}
85+
86+
heapArray[heapSize] = value;
87+
heapSize++;
88+
heapifyUp(heapSize - 1);
89+
}
90+
91+
// Return the maximum value from the heap
92+
int getMax() const {
93+
if (heapSize == 0) {
94+
throw runtime_error("Heap is empty!");
95+
}
96+
return heapArray[0];
97+
}
98+
99+
// Remove and return the maximum value from the heap
100+
int extractMax() {
101+
if (heapSize == 0) {
102+
throw runtime_error("Heap is empty!");
103+
}
104+
if (heapSize == 1) {
105+
heapSize--;
106+
return heapArray[0];
107+
}
108+
109+
int rootValue = heapArray[0];
110+
heapArray[0] = heapArray[heapSize - 1];
111+
heapSize--;
112+
heapifyDown(0);
113+
114+
return rootValue;
115+
}
116+
117+
// Delete a specific value from the heap
118+
void removeKey(int key) {
119+
int index = -1;
120+
for (int i = 0; i < heapSize; i++) {
121+
if (heapArray[i] == key) {
122+
index = i;
123+
break;
124+
}
125+
}
126+
127+
if (index == -1) {
128+
cout << "Key not found in heap." << endl;
129+
return;
130+
}
131+
132+
heapArray[index] = heapArray[heapSize - 1];
133+
heapSize--;
134+
135+
heapifyDown(index);
136+
heapifyUp(index);
137+
}
138+
139+
// Print all elements in the heap
140+
void displayHeap() const {
141+
cout << "Max Heap: ";
142+
for (int i = 0; i < heapSize; i++) {
143+
cout << heapArray[i] << " ";
144+
}
145+
cout << endl;
146+
}
147+
};
148+
149+
// Driver Code
150+
int main() {
151+
MaxHeap maxHeap(6);
152+
vector<int> values = {2, 3, 4, 5, 10, 15};
153+
154+
// Build heap from array
155+
maxHeap.buildHeap(values);
156+
maxHeap.displayHeap();
157+
158+
// Insert a new element
159+
maxHeap.insertValue(9);
160+
cout << "After inserting 9:" << endl;
161+
maxHeap.displayHeap();
162+
163+
// Get max element
164+
cout << "Maximum value: " << maxHeap.getMax() << endl;
165+
166+
// Extract max element
167+
cout << "Extracted max: " << maxHeap.extractMax() << endl;
168+
cout << "After extraction: ";
169+
maxHeap.displayHeap();
170+
171+
// Delete a specific key
172+
maxHeap.removeKey(5);
173+
cout << "After removing 5: ";
174+
maxHeap.displayHeap();
175+
176+
return 0;
177+
}
178+
179+
/****************************************
180+
Sample Output:
181+
182+
Max Heap: 15 10 4 5 3 2
183+
184+
After inserting 9:
185+
Max Heap: 15 10 9 5 3 2 4
186+
187+
Maximum value: 15
188+
189+
Extracted max: 15
190+
After extraction: Max Heap: 10 5 9 4 3 2
191+
192+
After removing 5:
193+
Max Heap: 10 4 9 2 3
194+
195+
Note:
196+
- The output shows the heap after each operation, maintaining the max-heap property.
197+
- Multiple valid heap configurations may exist after insertions or deletions,
198+
as long as the max-heap property is preserved.
199+
****************************************/

0 commit comments

Comments
 (0)