-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask3.cpp
More file actions
147 lines (130 loc) · 3.95 KB
/
Task3.cpp
File metadata and controls
147 lines (130 loc) · 3.95 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
template <typename T>
class Heap {
vector<T> heap;
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 left = 2 * index + 1;
int right = 2 * index + 2;
int largest = index;
if (left < (int)heap.size() && heap[left] > heap[largest]) largest = left;
if (right < (int)heap.size() && heap[right] > heap[largest]) largest = right;
if (largest != index) {
swap(heap[index], heap[largest]);
heapifyDown(largest);
}
}
public:
void insert(const T& value) {
heap.push_back(value);
heapifyUp((int)heap.size() - 1);
}
T extractMax() {
if (heap.empty()) return T();
T max = heap[0];
heap[0] = heap.back();
heap.pop_back();
if (!heap.empty()) heapifyDown(0);
return max;
}
T extractMin() {
if (heap.empty()) return T();
int n = (int)heap.size();
int minIdx = 0;
for (int i = 1; i < n; ++i) {
if (heap[i] < heap[minIdx]) minIdx = i;
}
T minValue = heap[minIdx];
heap[minIdx] = heap.back();
heap.pop_back();
if (minIdx < (int)heap.size()) {
heapifyDown(minIdx);
heapifyUp(minIdx);
}
return minValue;
}
T getMax() {
if (heap.empty()) return T();
T maxValue = heap[0];
for (int i = 1; i < (int)heap.size(); ++i) {
if (heap[i] > maxValue) {
maxValue = heap[i];
}
}
return maxValue;
}
void heapSort(vector<T>& arr) {
Heap<T> maxHeap;
for (const T& value : arr) maxHeap.insert(value);
for (int i = (int)arr.size() - 1; i >= 0; --i) {
arr[i] = maxHeap.extractMax();
}
}
bool isEmpty() const {
return heap.empty();
}
int size() const {
return (int)heap.size();
}
T top() const {
if (heap.empty()) return T();
return heap[0];
}
};
template <class T>
class PriorityQueue {
Heap<pair<int, T>> pq;
public:
void Insert(int idx, T value) {
pair<int, T> p = { idx,value };
pq.insert(p);
}
T maxPriority() {
if (pq.isEmpty()) return T();
return pq.top().second;
}
T extractHighestPriority() {
if (pq.isEmpty()) return T();
return pq.extractMax().second;
}
bool isEmpty() const {
return pq.isEmpty();
}
int size() const {
return pq.size();
}
};
int main() {
vector<int> examples = { 8,32,6,12,1,4,60,10 };
Heap<int> h;
for (int v : examples) h.insert(v);
cout << "heap -> top (max) = " << h.top() << "\n";
cout << "heap -> extractMax() = " << h.extractMax() << "\n";
cout << "heap -> new top = " << h.top() << "\n";
cout << "heap -> extractMin() = " << h.extractMin() << "\n";
cout << "heap -> top after extractMin = " << h.top() << "\n";
PriorityQueue<string> pq;
pq.Insert(3, "low");
pq.Insert(10, "high");
pq.Insert(5, "medium");
cout << "\nPriorityQueue maxPriority() (peek) = " << pq.maxPriority() << "\n";
cout << "PriorityQueue extractHighestPriority() = " << pq.extractHighestPriority() << "\n";
cout << "PriorityQueue next maxPriority() = " << pq.maxPriority() << "\n";
vector<int> arr = { 8,32,6,12,1,4,60,10 };
cout << "\nbefore heapSort: ";
for (int v : arr) cout << v << " ";
cout << "\n";
h.heapSort(arr);
cout << "after heapSort (ascending): ";
for (int v : arr) cout << v << " ";
cout << "\n";
return 0;
}