Added validity check for binary heap
This commit is contained in:
parent
304b71e1d9
commit
72a3a840ff
4 changed files with 38 additions and 1 deletions
|
@ -17,7 +17,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
super(data);
|
super(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
final private boolean DEBUG = false;
|
final private boolean DEBUG = true;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ShortestPathSolution doRun() {
|
protected ShortestPathSolution doRun() {
|
||||||
|
@ -51,6 +51,7 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
|
||||||
|
|
||||||
this.log("Cout marqué : " + parentLabel.getCost());
|
this.log("Cout marqué : " + parentLabel.getCost());
|
||||||
this.log("Nb successeurs : " + parentNode.getNumberOfSuccessors());
|
this.log("Nb successeurs : " + parentNode.getNumberOfSuccessors());
|
||||||
|
this.log("tas valide : " + heap.isValid(0));
|
||||||
|
|
||||||
for (Arc arc : parentNode.getSuccessors()) {
|
for (Arc arc : parentNode.getSuccessors()) {
|
||||||
Label currentLabel = labels[arc.getDestination().getId()];
|
Label currentLabel = labels[arc.getDestination().getId()];
|
||||||
|
|
|
@ -188,6 +188,29 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
return minItem;
|
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.
|
* Creates a multi-lines string representing a sorted view of this binary heap.
|
||||||
*
|
*
|
||||||
|
|
|
@ -61,4 +61,10 @@ public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(int index) {
|
||||||
|
System.out.println("TODO implement isValid");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,4 +78,11 @@ public interface PriorityQueue<E extends Comparable<E>> {
|
||||||
*/
|
*/
|
||||||
public E deleteMin() throws EmptyPriorityQueueException;
|
public E deleteMin() throws EmptyPriorityQueueException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the current queue is valid
|
||||||
|
*
|
||||||
|
* @return If the queue is valid
|
||||||
|
*/
|
||||||
|
public boolean isValid(int index);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue