added hashMap to the binaryHeap to bring complexity from n to log(n)
This commit is contained in:
джерело
d46ea7fcb4
коміт
f023a2ae70
2 змінених файлів з 35 додано та 3 видалено
|
@ -7,4 +7,5 @@
|
|||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="11" project-jdk-type="JavaSDK" />
|
||||
</project>
|
|
@ -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<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
// Number of elements in heap.
|
||||
private int currentSize;
|
||||
|
||||
// a hash map for the element - index link
|
||||
private HashMap<E, Integer> map;
|
||||
|
||||
// The heap array.
|
||||
protected final ArrayList<E> array;
|
||||
|
||||
|
@ -25,6 +29,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
public BinaryHeap() {
|
||||
this.currentSize = 0;
|
||||
this.array = new ArrayList<E>();
|
||||
this.map = new HashMap<E, Integer>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,6 +40,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
public BinaryHeap(BinaryHeap<E> heap) {
|
||||
this.currentSize = heap.currentSize;
|
||||
this.array = new ArrayList<E>(heap.array);
|
||||
this.map = new HashMap<E, Integer>(heap.map);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,6 +56,9 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
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<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
|
||||
@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<E extends Comparable<E>> implements PriorityQueue<E> {
|
|||
E lastItem = this.array.get(--this.currentSize);
|
||||
this.arraySet(0, lastItem);
|
||||
this.percolateDown(0);
|
||||
this.map.remove(minItem);
|
||||
return minItem;
|
||||
}
|
||||
|
||||
|
|
Завантаження…
Посилання в новій задачі