feat(binheap): remove passes tests

This commit is contained in:
Paul Alnet 2024-04-05 10:51:13 +02:00
parent 04c4316e36
commit cce3fcdca8

View file

@ -137,16 +137,17 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
@Override
public void remove(E x) throws ElementNotFoundException {
// TODO optimize indexOf
int index = array.indexOf(x);
if (isEmpty() || index == -1 || index >= this.currentSize) {
throw new ElementNotFoundException(x);
}
// replace element with last element
arraySet(index, array.get(--this.currentSize));
// replace element with the min
arraySet(index, this.findMin());
percolateDown(index);
percolateUp(index);
this.deleteMin();
}
@Override