wip(binheap): remove

This commit is contained in:
Paul Alnet 2024-04-05 09:46:28 +02:00
parent e45e83dc69
commit 04c4316e36

View file

@ -137,17 +137,16 @@ 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: // TODO optimize indexOf
if (isEmpty() || !array.contains(x)) { int index = array.indexOf(x);
if (isEmpty() || index == -1 || index >= this.currentSize) {
throw new ElementNotFoundException(x); throw new ElementNotFoundException(x);
} }
int indice_suppression = array.indexOf(x);
arraySet(indice_suppression, array.get(this.currentSize - 1)); // replace element with last element
//array.remove(this.currentSize); arraySet(index, array.get(--this.currentSize));
this.currentSize--;
if (indice_suppression < this.currentSize) { percolateDown(index);
percolateDown(indice_suppression);
}
} }
@Override @Override