Added validity check for binary heap

This commit is contained in:
Arnaud Vergnet 2020-04-03 08:40:48 +02:00
parent 304b71e1d9
commit 72a3a840ff
4 changed files with 38 additions and 1 deletions

View file

@ -17,7 +17,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
super(data);
}
final private boolean DEBUG = false;
final private boolean DEBUG = true;
@Override
protected ShortestPathSolution doRun() {
@ -51,6 +51,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
this.log("Cout marqué : " + parentLabel.getCost());
this.log("Nb successeurs : " + parentNode.getNumberOfSuccessors());
this.log("tas valide : " + heap.isValid(0));
for (Arc arc : parentNode.getSuccessors()) {
Label currentLabel = labels[arc.getDestination().getId()];

View file

@ -188,6 +188,29 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
return minItem;
}
@Override
public boolean isValid(int index) {
if (this.currentSize <= index)
return true;
boolean valid = true;
E current = this.array.get(0);
int ileft = indexLeft(index);
int iright = ileft + 1;
if (ileft < this.currentSize) {
E left = this.array.get(ileft);
if (current.compareTo(left) < 0)
valid = this.isValid(ileft);
}
if (iright < this.currentSize) {
E right = this.array.get(iright);
if (current.compareTo(right) < 0)
valid = this.isValid(iright);
}
return valid;
}
/**
* Creates a multi-lines string representing a sorted view of this binary heap.
*

View file

@ -61,4 +61,10 @@ public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<
return min;
}
@Override
public boolean isValid(int index) {
System.out.println("TODO implement isValid");
return false;
}
}

View file

@ -78,4 +78,11 @@ public interface PriorityQueue<E extends Comparable<E>> {
*/
public E deleteMin() throws EmptyPriorityQueueException;
/**
* Checks if the current queue is valid
*
* @return If the queue is valid
*/
public boolean isValid(int index);
}