-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPQ.h
72 lines (53 loc) · 1.29 KB
/
PQ.h
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
#ifndef ALGS4_PQ_H
#define ALGS4_PQ_H
#include <optional>
#include <utility>
#include <vector>
template<typename Key>
class PQ {
protected:
std::vector<std::optional<Key> > pq; // heap-ordered complete binary tree
int N = 0; // in pq[1..N] with pq[0] unused
virtual bool lower(int i, int j) const = 0;
void exch(int i, int j) { std::swap(pq[i], pq[j]); }
void swim(int k);
void sink(int k);
std::optional<Key> delTop();
public:
explicit PQ(int maxN) : pq(maxN + 1) {}
virtual ~PQ() = default;
bool isEmpty() const { return N == 0; }
int size() const { return N; }
void insert(const Key &v);
};
template<typename Key>
void PQ<Key>::swim(int k) {
while (k > 1 && lower(k / 2, k)) {
exch(k / 2, k);
k = k / 2;
}
}
template<typename Key>
void PQ<Key>::sink(int k) {
while (2 * k <= N) {
int j = 2 * k;
if (j < N && lower(j, j + 1)) ++j;
if (!lower(k, j)) break;
exch(k, j);
k = j;
}
}
template<typename Key>
std::optional<Key> PQ<Key>::delTop() {
auto top = pq[1];
exch(1, N--);
pq[N + 1] = std::nullopt;
sink(1);
return top;
}
template<typename Key>
void PQ<Key>::insert(const Key &v) {
pq[++N] = v;
swim(N);
}
#endif //ALGS4_PQ_H