-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_array.cpp
144 lines (127 loc) · 2.73 KB
/
heap_array.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
template <typename T, typename C = std::less<T> >
class heap
{
C _lt;
T *_base;
int _size;
public:
explicit heap() : _base(0x0), _size(0){
}
template <std::size_t N>
explicit heap(T(&p)[N]) : _size(N){
_base = new T[N];
memcpy(_base, p, sizeof(T)* N);
__make_heap(_lt);
}
const T &top() const{
return _base[0];
}
bool empty() const {
return _size <= 0;
}
T pop() {
// assert not empty;
T x = top(); // copy for return
std::swap(_base[0], _base[--_size]);
realloc(_base, sizeof(T)* _size);
if (_size > 0){
__heapify(_base, 0, _size, _lt);
}
return x;
}
// insert to heap.
void push(T key) {
realloc(_base, sizeof(T)* (++_size));
_base[_size - 1] = key;
heap_fix(_base, _size - 1);
}
// sort result stored in p
void heap_sort(T *p) {
while (_size){
*p++ = pop();
}
}
void inplace_heap_sort(){
struct GT {
inline bool operator ()(T const &a, T const &b) const {
return !(C()(a, b));
}
}_gt; // define NOT.(less<T>)
int n = _size;
__make_heap(_gt);
while (n > 1) {
std::swap(_base[0], _base[--n]);
__heapify(_base, 0, n, _gt);
}
}
__forceinline int parent(int k){
return ((k + 1) >> 1) - 1;
}
__forceinline int left(int k){
return (k << 1) + 1;
}
__forceinline int right(int k){
return (k + 1) << 1;
}
protected:
template <typename Cmp>
void __heapify(T *_ary, int i, int n, Cmp lt){
while (true) { // loop all children of i, pop the smallest to top.
int _left = left(i);
int _right = right(i);
int _min = i; // smallest
if (_left < n && lt(_ary[_left], _ary[i])){
_min = _left;
}
if (_right < n && lt(_ary[_right], _ary[_min])){
_min = _right;
}
if (_min != i){
std::swap(_ary[i], _ary[_min]); // i <->_min
i = _min;
}
else {
return;
}
}
}
// Cmp may be less<T> or greater<T>
template <typename Cmp>
void __make_heap(Cmp lt){
for (int i = (_size - 1) >> 1; i >= 0; --i){
__heapify(_base, i, _size, lt);
}
}
void heap_fix(T *a, int i) {
while (i > 0 && _lt(a[i], a[parent(i)])) {
std::swap(a[i], a[parent(i)]);
i = parent(i);
}
}
//-
void decrease_key(T *a, int i, T k) {
if (lt(k, a[i])) {
a[i] = k;
heap_fix(a, i, _lt);
}
}
// DATA SEGMENT ---------------------------------------------
};
int main()
{
int ar[] = {16, 4, 10, 14, 7, 9, 3, 2, 8, 1};
heap<int, std::less<int> > a(ar);
int k = a.pop();
a.push(k);
a.inplace_heap_sort();
// a.heap_sort(ar);
while (!a.empty()) {
std::cout << a.top() << std::endl;
a.pop();
}
return 0;
}