diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java index f19c325..a21e211 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java @@ -135,10 +135,14 @@ public class BinaryHeap> implements PriorityQueue { @Override public void remove(E x) throws ElementNotFoundException { - int index = this.array.indexOf(x); - System.out.println(x); - System.out.println(index); - if (isEmpty() || index == -1 || index >= this.currentSize) + int index; + try { + index = this.indexOf(x); + } catch(EmptyPriorityQueueException e) { + index = -1; + } + + if (index == -1) throw new ElementNotFoundException(x); else if (index == 0) deleteMin(); @@ -155,6 +159,19 @@ public class BinaryHeap> implements PriorityQueue { } } + private int indexOf(E x) { + if(this.isEmpty()) + throw new EmptyPriorityQueueException(); + int index = -1; + for(int i = 0; i < this.currentSize; ++i) { + if(x.compareTo(this.array.get(i)) == 0) { + index = i; + break; + } + } + return index; + } + @Override public E findMin() throws EmptyPriorityQueueException { if (isEmpty())