-
Notifications
You must be signed in to change notification settings - Fork 0
/
p71_heap.cpp
69 lines (50 loc) · 913 Bytes
/
p71_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
#include <iostream>
#define MAX_N 10000
int heap[MAX_N], sz = 0;
void push(int x){
int i = sz++;
while (i > 0){
// the number of a parent's node
int p = (i-1) / 2;
if(heap[p] <= x) break;
heap[i] = heap[p];
i = p;
}
heap[i] = x;
}
int pop(){
// min
int ret = heap[0];
// root variable
int x = heap[--sz];
// drug x from root
int i = 0;
while (i*2+1<sz){
// compare children
int a = i * 2 + 1, b = i * 2 + 2;
if (b < sz && heap[b] < heap[a]) a = b;
if (heap[a] >= x) break;
heap[i] = heap[a];
i = a;
}
heap[i] = x;
return ret;
}
int main(){
int size;
std::cout << "Input size: ";
std::cin >> size;
std::cout << "Input numbers: ";
// input numbers
for(int i=0; i<size; i++){
int temp;
std::cin >> temp;
push(temp);
}
// print all in accending order
for(int i=0; i<size; i++){
std::cout << pop() << ',';
}
std::cout << std::endl;
return 0;
}