ajout de remove dans BinaryHeap

This commit is contained in:
Matteo Sabben 2025-04-29 16:52:03 +02:00
parent f95ded7b24
commit 8160f40454

View file

@ -134,7 +134,29 @@ 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: int ind = -1;
for (int i = 0; i < currentSize; i++) {
if (this.array.get(i).equals(x)) {
ind = i;
break;
}
}
if (ind == -1) {
throw new ElementNotFoundException(x);
}
int lastIndex = currentSize - 1;
if (ind != lastIndex) {
E lastElement = this.array.get(lastIndex);
this.array.set(ind, lastElement);
currentSize--;
percolateDown(ind);
percolateUp(ind);
} else {
currentSize--;
}
} }
@Override @Override