diff --git a/.idea/misc.xml b/.idea/misc.xml index 80cd069..e5e2ec5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,4 +7,5 @@ + \ No newline at end of file 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 7a17c6b..832f3e9 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 @@ -1,4 +1,5 @@ package org.insa.graphs.algorithm.utils; +import java.util.HashMap; import java.util.ArrayList; @@ -16,6 +17,9 @@ public class BinaryHeap> implements PriorityQueue { // Number of elements in heap. private int currentSize; + // a hash map for the element - index link + private HashMap map; + // The heap array. protected final ArrayList array; @@ -25,6 +29,7 @@ public class BinaryHeap> implements PriorityQueue { public BinaryHeap() { this.currentSize = 0; this.array = new ArrayList(); + this.map = new HashMap(); } /** @@ -35,6 +40,7 @@ public class BinaryHeap> implements PriorityQueue { public BinaryHeap(BinaryHeap heap) { this.currentSize = heap.currentSize; this.array = new ArrayList(heap.array); + this.map = new HashMap(heap.map); } /** @@ -50,6 +56,9 @@ public class BinaryHeap> implements PriorityQueue { else { this.array.set(index, value); } + + // we need to add the correspondence between index - value + map.put(value,index); } /** @@ -137,15 +146,36 @@ public class BinaryHeap> implements PriorityQueue { @Override public void remove(E x) throws ElementNotFoundException { - // index of is not optimized but it's better than nothing. We will come back to improve it if we have time at the end - int i = this.array.indexOf(x); - if (i == -1 || i > this.currentSize - 1) { + /* LEGACY CODE : + Index of is not optimized but it's better than nothing. We will come back to improve it if we have time at the end + int i = this.array.indexOf(x) */ + + + int i = -1; + + // check if it is in the hashMap + if (this.map.get(x) != null){ + i = this.map.get(x); + } else { + throw new ElementNotFoundException(x); + } + + // check if the value makes sense + if (i > this.currentSize - 1 || i == -1) { throw new ElementNotFoundException(x); } E lastItem = this.array.get(--this.currentSize); this.arraySet(i, lastItem); this.percolateDown(i); this.percolateUp(i); + + + + // since we removed the element we need to make sure it's not referenced in the hashMap (hence putting it to -1) + // since usually (not sure for the case of java, depends on how collisions are handled) the deletion of a pair + // costs more than changing its value so could just set it to -1 but keeping it increases the chances of collisions + // We therefore chose to remove it + this.map.remove(x); } @Override @@ -161,6 +191,7 @@ public class BinaryHeap> implements PriorityQueue { E lastItem = this.array.get(--this.currentSize); this.arraySet(0, lastItem); this.percolateDown(0); + this.map.remove(minItem); return minItem; }