ajout de remove dans BinaryHeap
This commit is contained in:
parent
f95ded7b24
commit
8160f40454
1 changed files with 23 additions and 1 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue