-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.cpp
64 lines (63 loc) · 1015 Bytes
/
PriorityQueue.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
#include<iostream>
const int max = 20;
class MaxPQ{
private:
int *arr;
int N = 0;
bool less(int i, int j){
return arr[i] < arr[j];
}
void exch(int i, int j){
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
void swim(int n){
while (n>1 && less(n/2, n)){
exch(n / 2, n);//大于父节点则交换
n /= 2;
}
}
void sink(int n){
while (2 * n <= N){
int j = 2 * n;
if (j < N&&!less(j, j+1))j++;//选取两个子节点中较的的一个,换到上面成为父节点之后才能比下面两个子节点都大
if (!less(n, j))break;
exch(n, j);
n = j;
}
}
public:
MaxPQ(int n){
arr = new int[n];
}
void insert(int n){
arr[++N] = n;
swim(N);
}
int delMax(){
int max = arr[1];
exch(1, N--);
sink(1);
return max;
}
/*void d(){
for (int i = 1; i <= N; i++){
std::cout << arr[i] << " ";
}
std::cout << "\n";
}*/
};
//int main(){
// MaxPQ p;
// p.insert(10);
// p.insert(3);
// p.insert(11);
// p.insert(6);
// p.insert(8);
// p.insert(12);
// p.insert(9);
// p.d();
// p.delMax();
// p.d();
//}