-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.cpp
53 lines (47 loc) · 1.26 KB
/
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
#include "heap.h"
void Heap::SiftDown() {
size_t index = 0;
while (2 * index + 1 < value_.size()) {
size_t left = 2 * index + 1;
size_t right = 2 * index + 2;
size_t next_index = left;
if (right < value_.size() &&
value_[right]->priority <= value_[next_index]->priority) {
next_index = right;
}
if (value_[index]->priority <= value_[next_index]->priority) {
break;
}
std::swap(value_[index], value_[next_index]);
index = next_index;
}
}
void Heap::SiftUp(size_t index) {
if (index > value_.size()) {
return;
}
while (index > 0 &&
value_[index]->priority < value_[(index - 1) / 2]->priority) {
std::swap(value_[index], value_[(index - 1) / 2]);
index = (index - 1) / 2;
}
}
std::shared_ptr<Trie> Heap::GetMin() {
if (value_.empty()) {
return nullptr;
}
return value_[0];
}
void Heap::RemoveElement() {
if (value_.empty()) {
return;
}
value_[0] = value_.back();
value_.pop_back();
SiftDown();
}
void Heap::AddElement(const std::shared_ptr<Trie> &elem) {
value_.push_back(elem);
SiftUp(value_.size() - 1);
}
size_t Heap::Size() const { return value_.size(); }