binary heap remove
This commit is contained in:
parent
30bf791092
commit
d9e7c9df3b
1 changed files with 25 additions and 0 deletions
|
@ -138,6 +138,31 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
@Override
|
||||
public void remove(E x) throws ElementNotFoundException {
|
||||
// TODO:
|
||||
int k=0;
|
||||
int index =-1; //on cherche l'indice de x si il est dans le cas et sinon on rentre dans l'exception
|
||||
|
||||
for (E element:array){
|
||||
if (k==currentSize){
|
||||
break;
|
||||
}
|
||||
if (element.equals(x)){
|
||||
index=k;
|
||||
}
|
||||
k+=1;
|
||||
}
|
||||
if (index==-1){ //x n'est pas dans le tas
|
||||
throw new ElementNotFoundException(x);
|
||||
}
|
||||
|
||||
E last=this.array.get(currentSize-1); //dernier élement du tas
|
||||
this.array.set(index, last);
|
||||
|
||||
currentSize=currentSize-1; //on enlève le dernier élement (qui était dans le tas) du tas
|
||||
percolateDown(index);
|
||||
percolateUp(index);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue