Skip to content

Commit 4ec8e7e

Browse files
author
agribeau
committed
feat(min heap): Add method of removing the element at the specified position
1 parent 2678da5 commit 4ec8e7e

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/data_structure/heap/MinHeap.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,24 @@ private void siftDownUsingComparator(int k, E x) {
201201
data.set(k, x);
202202
}
203203

204+
/**
205+
* 移除指定索引的元素
206+
*
207+
* @param i 待移除元素的索引
208+
*/
209+
private void removeAt(int i) {
210+
int s = data.size() - 1;
211+
212+
if (i == s) {
213+
data.remove(i);
214+
} else {
215+
E moved = data.get(s);
216+
data.set(s, null);
217+
218+
siftDown(i, moved);
219+
if (data.get(i) == moved) {
220+
siftUp(i, moved);
221+
}
222+
}
223+
}
204224
}

0 commit comments

Comments
 (0)