-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityQ.h
71 lines (68 loc) · 1.42 KB
/
priorityQ.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdexcept>
using namespace std;
#define ll long long int
template <class T>
class defaultCompare {
public:
bool operator()(const T a, const T b) {
return a > b;
}
};
template <class T, class C=defaultCompare<T>>
struct PriorityQ
{
private:
vector<T> A;
C cmp;
ll PARENT(ll i) {
return (i - 1) / 2;
}
ll LEFT(ll i) {
return (2 * i + 1);
}
ll RIGHT(ll i) {
return (2 * i + 2);
}
void heapify_down(ll i) {
ll left = LEFT(i);
ll right = RIGHT(i);
ll smallest = i;
if (left < size() && cmp(A[left],A[i])<0)
smallest = left;
if (right < size() && cmp(A[right],A[smallest])<0)
smallest = right;
if (smallest != i) {
swap(A[i], A[smallest]);
heapify_down(smallest);
}
}
void heapify_up(ll i) {
if(i && cmp(A[PARENT(i)],A[i])>0) {
swap(A[i], A[PARENT(i)]);
heapify_up(PARENT(i));
}
}
public:
unsigned ll size() {
return A.size();
}
bool empty() {
return size() == 0;
}
void push(T key) {
A.push_back(key);
ll index = size() - 1;
heapify_up(index);
}
void pop() {
A[0] = A.back();
A.pop_back();
heapify_down(0);
}
T top() {
return A.at(0);
}
};