Skip to content

Commit 02de4ed

Browse files
committed
fix: 유일 원소일 때 삭제가 제대로 되지 않는 현상 수정
1 parent 57535ef commit 02de4ed

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

DataStructure/Java/_09_Heap/Heap.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ public E remove() {
166166
throw new NoSuchElementException();
167167
}
168168
E result = (E) array[1];
169-
E target = (E) array[size];
169+
E target;
170+
if(size == 1) {
171+
target = null;
172+
}
173+
else {
174+
target = (E) array[size];
175+
}
170176
array[size] = null;
171177
size--;
172178
siftDown(1, target);

DataStructure/Java/_10_PriorityQueue/PriorityQueue.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@ public E remove() {
194194
}
195195

196196
E result = (E) array[1];
197-
E target = (E) array[size];
197+
E target;
198+
if(size == 1) {
199+
target = null;
200+
}
201+
else {
202+
target = (E) array[size];
203+
}
198204

199205
array[size] = null;
200206
size--;

0 commit comments

Comments
 (0)