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 922eab9..d237c6c 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,13 +1,15 @@ package org.insa.graphs.algorithm.utils; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; /** * Implements a binary heap containing elements of type E. - * + *

* Note that all comparisons are based on the compareTo method, hence E must * implement Comparable - * + * * @author Mark Allen Weiss * @author DLB */ @@ -15,6 +17,7 @@ public class BinaryHeap> implements PriorityQueue { // Number of elements in heap. private int currentSize; + private Map elementIndexes = new HashMap<>(); // The heap array. protected final ArrayList array; @@ -29,16 +32,18 @@ public class BinaryHeap> implements PriorityQueue { /** * Construct a copy of the given heap. - * + * * @param heap Binary heap to copy. */ public BinaryHeap(BinaryHeap heap) { this.currentSize = heap.currentSize; this.array = new ArrayList(heap.array); + this.elementIndexes = new HashMap<>(heap.elementIndexes); } /** * Check if each element is lower than its children + * * @return true if valid, false otherwise */ public boolean isValid() { @@ -62,15 +67,15 @@ public class BinaryHeap> implements PriorityQueue { /** * Set an element at the given index. - * + * * @param index Index at which the element should be set. * @param value Element to set. */ private void arraySet(int index, E value) { + elementIndexes.put(value, index); if (index == this.array.size()) { this.array.add(value); - } - else { + } else { this.array.set(index, value); } } @@ -98,15 +103,13 @@ public class BinaryHeap> implements PriorityQueue { /** * Internal method to percolate up in the heap. - * + * * @param index Index at which the percolate begins. */ private void percolateUp(int index) { E x = this.array.get(index); - for (; index > 0 - && x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent( - index)) { + for (; index > 0 && x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(index)) { E moving_val = this.array.get(indexParent(index)); this.arraySet(index, moving_val); } @@ -116,7 +119,7 @@ public class BinaryHeap> implements PriorityQueue { /** * Internal method to percolate down in the heap. - * + * * @param index Index at which the percolate begins. */ private void percolateDown(int index) { @@ -136,8 +139,7 @@ public class BinaryHeap> implements PriorityQueue { this.arraySet(ileft, current); this.percolateDown(ileft); } - } - else { + } else { // Right is smaller if (right.compareTo(current) < 0) { this.arraySet(index, right); @@ -167,21 +169,14 @@ public class BinaryHeap> implements PriorityQueue { @Override public void remove(E x) throws ElementNotFoundException { - int index = 0; - // We avoid array.indexOf(x) because we don't need to search - // through the whole list, we can stop at indexSize. - boolean found = false; - while(!found && index < currentSize) { - if (array.get(index) == x) - found =true; - else - index++; + Integer index = elementIndexes.remove(x); + if (index == null) throw new ElementNotFoundException(x); + currentSize--; + if (currentSize != 0 && index != currentSize) { + arraySet(index, array.get(currentSize)); + percolateUp(index); + percolateDown(index); } - - if (!found) throw new ElementNotFoundException(x); - arraySet(index, array.get(--currentSize)); - percolateUp(index); - percolateDown(index); } @Override @@ -194,15 +189,19 @@ public class BinaryHeap> implements PriorityQueue { @Override public E deleteMin() throws EmptyPriorityQueueException { E minItem = findMin(); - E lastItem = this.array.get(--this.currentSize); - this.arraySet(0, lastItem); - this.percolateDown(0); + elementIndexes.remove(minItem); + currentSize--; + if (currentSize != 0) { + E lastItem = this.array.get(this.currentSize); + this.arraySet(0, lastItem); + this.percolateDown(0); + } return minItem; } /** * Creates a multi-lines string representing a sorted view of this binary heap. - * + * * @return a string containing a sorted view this binary heap. */ public String toStringSorted() { @@ -211,10 +210,9 @@ public class BinaryHeap> implements PriorityQueue { /** * Creates a multi-lines string representing a sorted view of this binary heap. - * + * * @param maxElement Maximum number of elements to display. or {@code -1} to * display all the elements. - * * @return a string containing a sorted view this binary heap. */ public String toStringSorted(int maxElement) { @@ -223,7 +221,7 @@ public class BinaryHeap> implements PriorityQueue { /** * Creates a multi-lines string representing a tree view of this binary heap. - * + * * @return a string containing a tree view of this binary heap. */ public String toStringTree() { @@ -232,9 +230,8 @@ public class BinaryHeap> implements PriorityQueue { /** * Creates a multi-lines string representing a tree view of this binary heap. - * + * * @param maxDepth Maximum depth of the tree to display. - * * @return a string containing a tree view of this binary heap. */ public String toStringTree(int maxDepth) {