-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapStruct.cpp
68 lines (54 loc) · 1.05 KB
/
HeapStruct.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
#include <iostream>
using namespace std;
const int MAXN = 1000;
struct heap{
int value[MAXN], size;
heap(){
size = 0;
}
void down_to_up(int node){
if(node == 1) return;
if(value[node] < value[node / 2]){
swap(value[node], value[node / 2]);
down_to_up(node / 2);
}
}
void up_to_down(int node){
if(node * 2 > size) return;
else if(node * 2 + 1 > size and value[node] > value[node * 2]){
swap(value[node], value[node * 2]);
up_to_down(node * 2);
}
else if (node * 2 + 1 <= size){
int ind = (value[node * 2] < value[node * 2 + 1]) ? (node * 2) : (node * 2 + 1);
swap(value[node], value[ind]);
up_to_down(ind);
}
}
void add(int x){
value[++size] = x;
down_to_up(size);
}
int min(){
return value[1];
}
void pop(){
if(size == 0) return;
swap(value[1], value[size]);
size--;
up_to_down(1);
}
};
int main(){
// test
heap jahan;
jahan.add(15);
jahan.add(12);
jahan.add(-2);
cout << jahan.min() << endl;
jahan.pop();
jahan.add(13);
cout << jahan.min() << endl;
// end test
return 0;
}