feat(binheap): remove passes tests
This commit is contained in:
parent
04c4316e36
commit
cce3fcdca8
1 changed files with 5 additions and 4 deletions
|
@ -137,16 +137,17 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(E x) throws ElementNotFoundException {
|
public void remove(E x) throws ElementNotFoundException {
|
||||||
// TODO optimize indexOf
|
|
||||||
int index = array.indexOf(x);
|
int index = array.indexOf(x);
|
||||||
if (isEmpty() || index == -1 || index >= this.currentSize) {
|
if (isEmpty() || index == -1 || index >= this.currentSize) {
|
||||||
throw new ElementNotFoundException(x);
|
throw new ElementNotFoundException(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace element with last element
|
// replace element with the min
|
||||||
arraySet(index, array.get(--this.currentSize));
|
arraySet(index, this.findMin());
|
||||||
|
|
||||||
percolateDown(index);
|
percolateUp(index);
|
||||||
|
|
||||||
|
this.deleteMin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue