-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.h
287 lines (260 loc) · 8.31 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* File Heap.h
* -----------
* You should finish your homework in this file
*/
#ifndef _heap_h
#define _heap_h
#include <iostream>
#include <vector>
#include <map>
#include<algorithm>
#include<cmath>
using namespace std;
//assumption:
//all paras are legal!!!!!!!!!!!
/*
* Class Heap
* ----------
* This is the class you should implement.
* It is a template class, Compare can be greater<int>, less<int> and so on.
* When you define a Heap like Heap<greater<int>>, it should be a maxHeap (who's top this the max element)
* When you define a Heap like Heap<less<int>>, it should be a minHeap (who's top this the min element)
* So, when you want to compare two element while implement the member function, you shoud use the member variable cmp, rather than <,<=,>,>=
*/
template<class Compare>
class Heap
{
private:
// use elements to store the element
map<int,vector<int>> elements;
int hp=0;
// cmp is a comparer that can help you to compare two element's priority
// Usage:
// cmp(element_1, element_2)
// if element_1's priority is higher than element_2, it will return true, otherwise, return false
// Example:
// when you define a Heap like Heap<greater<int>>, it means Compare is greater<int>, and cmp is a instance of greater<int>
// So, cmp(1,2) and cmp(1,1) will return false and cmp(2,1) will return true
// when you define a Heap like Heap<less<int>>, it means Compare is less<int>, and cmp is a instance of less<int>
// So, cmp(2,1) and cmp(1,1) will return false and cmp(1,2) will return true
// By this way, you can implement minHeap and maxHeap using one class
Compare cmp;
// the Top element's index, can make the code more readable
// It's up to you whether to use it
pair<int,int> topIndex = make_pair(0,0);
pair<int,int> nilIndex = make_pair(0,0);
// Some help function, can improve the code structure
// It's up to you whether to use them
int& get(pair<int,int>index)
{
if (!elements.count(index.first))elements[index.first] = vector<int>(pow(2, index.first));//complete
return elements[index.first][index.second];
}
pair<int,int> getParentIndex(pair<int,int> index)
{
return make_pair(index.first-1,index.second/2);
}
pair<int,int> getLeftIndex(pair<int,int> index)
{
return make_pair(index.first+1,index.second*2);
}
pair<int,int> getRightIndex(pair<int,int> index)
{
return make_pair(index.first+1,index.second*2+1);
}
bool isInRange(pair<int,int> index)
{
return index.first == nilIndex.first && index.second <= nilIndex.second || nilIndex.first>index.first;
}
bool isTop(pair<int,int> index)
{
return index==topIndex;
}
void swap(pair<int,int> index_1, pair<int,int> index_2)
{
std::swap(get(index_1),get(index_2));
}
pair<int,int> findIndex(int element)
{
for (auto& x : elements)//暴力搜索
{
auto i = find(x.second.begin(), x.second.end(), element);
if(i!=x.second.end())
return make_pair(x.first, i- x.second.begin());
}
return make_pair(-1, -1);
}
void shift_up(pair<int,int> index)
{
if(isTop(index))return;
auto prt=getParentIndex(index);
if(cmp(get(index),get(prt))) //下克上 (●ˇ∀ˇ●)
{
swap(index, prt);
shift_up(prt);
}
}
void shift_down(pair<int,int> index)
{
auto l=getLeftIndex(index);
auto r=getRightIndex(index);
if(isInRange(l) && isInRange(r))//二胎
{
auto root= cmp(get(l),get(r))? l:r;//二子夺嫡 ヾ(•ω•`)o
if(cmp(get(root),get(index)))//传位嫡长子
{
swap(index, root);
shift_down(root);
}
}
else if(isInRange(l))//独生子女
{
if(cmp(get(l),get(index)))
{
swap(index, l);
shift_down(l);
}
}
else
return;
}
public:
void show()
{
for (auto &x : elements)
{
for (auto & y : x.second)
cout << y << '\t';
cout << endl;
}
}
/*
* Constructor: Heap()
* Usage: Heap<greater<int>> maxHeap
*/
Heap()=default;
/*
* Constructor: Heap(vector<int> init_elements)
* Usage: Heap<greater<int>> maxHeap(init_elements)
* ------------------------------------------------
* You should implement this Method with O(mlog(n)) time complexity (m = init_elements.size(), n = elements.size())
*/
Heap(vector<int> init_elements);
/*
* Destructor: ~Heap()
* Usage: usually implicit
*/
~Heap()=default;
/*
* Method: insert(int element)
* Usage: maxHeap.insert(1)
* ------------------------
* Add the new element in to the end of elements and shift_up this element
* You should implement this Method with O(log(n)) time complexity (n = elements.size())
*/
void insert(int element);
/*
* Method: insert(vector<int> new_elements)
* Usage: maxHeap.insert(new_elements)
* ------------------------
* Add the new element in to the end of elements and shift_up this element one by one
* You should implement this Method with O(mlog(n)) time complexity (m = new_elements.size(), n = elements.size())
*/
void insert(const vector<int>& new_elements);
/*
* Method: erase(int element)
* Usage: maxHeap.erase(1)
* ------------------------
* Swap this element with the elements's last element, delete the last element, and shift_down the swapped element
* You should implement this Method with O(log(n)) time complexity (n = elements.size())
*/
bool erase(int element);
/*
* Method: pop()
* Usage: maxHeap.pop()
* ------------------------
* Swap the top element with the elements's last element, delete and return the last element, and shift_down the swapped element
* You should implement this Method with O(log(n)) time complexity (n = elements.size())
*/
int pop();
/*
* Method: top()
* Usage: maxHeap.top()
* ------------------------
* return the top element
* You should implement this Method with O(log(1)) time complexity
*/
int top();
/*
* Method: size()
* Usage: maxHeap.size()
* ------------------------
* return the number of element in the Heap
* You should implement this Method with O(log(1)) time complexity
*/
int size();
};
template<class Compare>
Heap<Compare>::Heap(vector<int> init_elements)//mlgn
{
insert(init_elements);
}
template<class Compare>
void Heap<Compare>::insert(int element)//only one operation lgn -> lgn
{
hp++;
if (hp == 1)nilIndex=make_pair(0,0);
else if(nilIndex.second==pow(2,nilIndex.first)-1)
{
nilIndex.first++;
nilIndex.second=0;
}
else nilIndex.second++;
get(nilIndex) = element;
shift_up(nilIndex);//lgn
}
template<class Compare>
void Heap<Compare>::insert(const vector<int>& new_elements)//mlgn
{
for(auto x:new_elements)
insert(x);
}
template<class Compare>
bool Heap<Compare>::erase(int element)//n+ lgn
{
hp--;
auto index=findIndex(element);//On
if (index.first == -1)return false;
swap(index,nilIndex);
if(nilIndex.second==0)
{
elements.erase(nilIndex.first);//O1
nilIndex.first--;
nilIndex.second=pow(2,nilIndex.first)-1;
}
else
{
nilIndex.second--;
}
shift_down(index);
return true;
}
template<class Compare>
int Heap<Compare>::pop()//olgn
{
int t= top();
erase(top());//erase: findIndex always find top first, so O(lgn)
return t;
}
template<class Compare>
int Heap<Compare>::top()//o1
{
return get(topIndex);
}
template<class Compare>
int Heap<Compare>::size()//o1
{
return hp;
}
#endif