-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.h
188 lines (157 loc) · 4.02 KB
/
heap.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Created by tyz on 2015/11/24.
//
#ifndef PPR_HEAP_H
#define PPR_HEAP_H
/*
* C++ Program to Implement Binary Heap
*/
#include <iostream>
#include <cstdlib>
#include <vector>
#include <iterator>
using namespace std;
/*
* Class Declaration
*/
template<typename T, typename Compare = less<T>>
class BinaryHeap {
private:
vector<pair<T, int>> heap;
vector<int> heap_idx;
int left(int parent) {
int l = 2 * parent + 1;
if (l < int(heap.size()))
return l;
else
return -1;
}
int right(int parent) {
int r = 2 * parent + 2;
if (r < int(heap.size()))
return r;
else
return -1;
}
int parent(int child) {
int p = (child - 1) / 2;
if (child == 0)
return -1;
else
return p;
}
void heapswap(int x, int y) {
swap(heap_idx[heap[x].second], heap_idx[heap[y].second]);
swap(heap[x], heap[y]);
}
void heapifyup(int in) {
while (true) {
int pin = parent(in);
// if smaller than parent then swap
if (in >= 0 && pin >= 0 && comp(heap[in].first, heap[pin].first)) {
heapswap(in, pin);
in = pin;
}
else
break;
}
}
void heapifydown(int in) {
while (true) {
int child = left(in);
int child1 = right(in);
// child point to smaller one
if (child >= 0 && child1 >= 0 && comp(heap[child1].first, heap[child].first)) {
child = child1;
}
// if child is smaller, then swap
if (child > 0 && comp(heap[child].first, heap[in].first)) {
heapswap(in, child);
in = child;
}
else {
break;
}
}
}
Compare comp;
// comp return true means more close to root
public:
BinaryHeap(int maxn, Compare _comp = std::less<T>()) {
heap_idx = vector<int>((unsigned long) maxn + 10, -1);
comp = _comp;
// cout << comp(1, 2) << endl;
// cout << comp(2, 1) << endl;
}
map<int, T> as_map() {
map<int, T> rtn;
for (auto item: heap) {
rtn[item.second] = item.first;
}
return rtn;
};
vector<pair<T, int>>& get_elements(){
return heap;
}
T get_value(int idx) {
return heap[heap_idx[idx]].first;
};
void insert(int index, T element) {
heap_idx[index] = (int) heap.size();
heap.push_back(MP(element, index));
heapifyup(int(heap.size()) - 1);
}
void delete_top() {
if (heap.size() == 0) {
cout << "Heap is Empty" << endl;
return;
}
heapswap(0, (int) (heap.size() - 1));
heap_idx[heap.back().second] = -1;
heap.pop_back();
heapifydown(0);
//cout << "Element Deleted" << endl;
}
void modify(int index, T newvalue) {
int idx = heap_idx[index];
if (comp(newvalue, heap[idx].first)) {
heap[idx].first = newvalue;
heapifyup(idx);
}
else {
heap[idx].first = newvalue;
heapifydown(idx);
}
}
pair<T, int> extract_top() {
return heap.front();
}
void display() {
cout << "Heap --> ";
for (auto p:heap) {
cout << p.first << " ";
}
cout << endl;
}
int size() {
return int(heap.size());
}
void clear() {
// heap.clear();
while (size())
delete_top();
}
// return true is has index
bool has_idx(int idx) {
return heap_idx[idx] != -1;
}
void verify() {
for (int child = 0; child < heap.size(); ++child) {
int p = parent(child);
if (p >= 0)
// child cannot be smaller than parent
assert (!comp(heap[child].first, heap[p].first));
}
}
};
#endif //PPR_HEAP_H