This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_Heap.cpp
77 lines (77 loc) · 2.04 KB
/
Binary_Heap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>
class BinaryHeap {
private:
std::vector<int> heap;
public:
BinaryHeap() {}
void insert(int value) {
heap.push_back(value);
heapifyUp(heap.size() - 1);
}
int extractMin() {
if (isEmpty()) {
std::cerr << "Heap is empty. Cannot extract minimum element.\n";
return -1;
}
int minValue = heap[0];
int lastIndex = heap.size() - 1;
std::swap(heap[0], heap[lastIndex]);
heap.pop_back();
heapifyDown(0);
return minValue;
}
bool isEmpty() const {
return heap.empty();
}
void printHeap() const {
for (int value : heap) {
std::cout << value << " ";
}
std::cout << std::endl;
}
private:
void heapifyUp(int index) {
while (index > 0) {
int parentIndex = (index - 1) / 2;
if (heap[index] < heap[parentIndex]) {
std::swap(heap[index], heap[parentIndex]);
index = parentIndex;
} else {
break;
}
}
}
void heapifyDown(int index) {
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
int smallest = index;
if (leftChild < heap.size() && heap[leftChild] < heap[smallest]) {
smallest = leftChild;
}
if (rightChild < heap.size() && heap[rightChild] < heap[smallest]) {
smallest = rightChild;
}
if (smallest != index) {
std::swap(heap[index], heap[smallest]);
heapifyDown(smallest);
}
}
};
int main() {
BinaryHeap minHeap;
minHeap.insert(3);
minHeap.insert(2);
minHeap.insert(1);
minHeap.insert(7);
minHeap.insert(8);
minHeap.insert(4);
minHeap.insert(6);
minHeap.insert(5);
std::cout << "Original Heap: ";
minHeap.printHeap();
std::cout << "Extracted Min: " << minHeap.extractMin() << std::endl;
std::cout << "Heap after extraction: ";
minHeap.printHeap();
return 0;
}