Skip to content

Commit 9cb6aea

Browse files
committed
Fix BinaryHeap
1 parent d0bb192 commit 9cb6aea

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

algorithms/binary_heap/binary_heap.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,22 @@ struct BinaryHeap {
9191
}
9292

9393
void remove(size_t key) {
94-
std::swap(
95-
this->container->begin()[key],
96-
this->container->begin()[this->container->size() - 1]
97-
);
98-
this->container->pop_back();
99-
this->sift_down(key);
94+
if (this->container->size() > key) {
95+
std::swap(
96+
this->container->begin()[key],
97+
this->container->begin()[this->container->size() - 1]
98+
);
99+
this->container->pop_back();
100+
this->sift_down(key);
101+
}
100102
}
101103

102104
void pop() {
103105
this->del(0);
104106
}
105107

106108
void change(size_t key, T new_value) {
107-
if (this->container->begin()[key] != new_value) {
109+
if (this->container->size() > key && this->container->begin()[key] != new_value) {
108110
this->container->begin()[key] = new_value;
109111
if (
110112
key > 0 &&

0 commit comments

Comments
 (0)