The recently added test on priorityqueue was not compatible with BinarySearchTree implementation. This is fixed.
This commit is contained in:
parent
375e783c42
commit
5ee48b4598
@ -1,18 +1,23 @@
|
||||
package org.insa.graphs.algorithm.utils;
|
||||
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.*;
|
||||
|
||||
public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||
|
||||
// Underlying implementation
|
||||
private final SortedSet<E> sortedSet;
|
||||
|
||||
// Elements, sorted by their value
|
||||
private SortedSet<E> sortedSet;
|
||||
|
||||
// Elements, by their hashcode.
|
||||
private HashSet<E> elems;
|
||||
|
||||
/**
|
||||
* Create a new empty binary search tree.
|
||||
*/
|
||||
public BinarySearchTree() {
|
||||
this.sortedSet = new TreeSet<>();
|
||||
this.elems = new HashSet<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -22,26 +27,50 @@ public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<
|
||||
*/
|
||||
public BinarySearchTree(BinarySearchTree<E> bst) {
|
||||
this.sortedSet = new TreeSet<>(bst.sortedSet);
|
||||
this.elems = new HashSet<>(bst.elems);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return sortedSet.isEmpty();
|
||||
return this.sortedSet.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return sortedSet.size();
|
||||
return this.sortedSet.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insert(E x) {
|
||||
sortedSet.add(x);
|
||||
this.sortedSet.add(x);
|
||||
this.elems.add(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(E x) throws ElementNotFoundException {
|
||||
if (!sortedSet.remove(x)) {
|
||||
|
||||
if (this.elems.contains(x)) {
|
||||
// x is known to be here.
|
||||
|
||||
boolean removed = this.sortedSet.remove(x) ;
|
||||
this.elems.remove(x) ;
|
||||
|
||||
if (!removed) {
|
||||
// The element x was not found in the sorted tree, because its value has changed.
|
||||
// However, we know it is here.
|
||||
|
||||
// This forces the sorted set to be reorganized
|
||||
SortedSet<E> ts = this.sortedSet ;
|
||||
this.sortedSet = new TreeSet<>() ;
|
||||
for (E y : ts) {
|
||||
this.sortedSet.add(y) ;
|
||||
}
|
||||
|
||||
removed = this.sortedSet.remove(x) ;
|
||||
assert(removed) ;
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new ElementNotFoundException(x);
|
||||
}
|
||||
}
|
||||
|
@ -45,8 +45,14 @@ public abstract class PriorityQueueTest {
|
||||
// Actual value
|
||||
private int value ;
|
||||
|
||||
// Unique identifier, used to get a hashcode that does not depend on the value (which may change)
|
||||
private int id ;
|
||||
|
||||
private static int counter = 1000 ;
|
||||
|
||||
public MutableInteger(int value) {
|
||||
this.value = value ;
|
||||
this.id = counter++ ;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,12 +73,13 @@ public abstract class PriorityQueueTest {
|
||||
|
||||
@Override
|
||||
public int compareTo(MutableInteger other) {
|
||||
return Integer.compare(this.value, other.value);
|
||||
if (this.id == other.id) return 0 ;
|
||||
else return Integer.compare(this.value, other.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Integer.toString(get());
|
||||
return "" + this.get() + " [id:" + this.id + "]";
|
||||
}
|
||||
|
||||
};
|
||||
@ -314,6 +321,7 @@ public abstract class PriorityQueueTest {
|
||||
// Update value before removing it. This is what happens when updating a Dijkstra label before updating it.
|
||||
mi.set(--min);
|
||||
queue.remove(mi);
|
||||
|
||||
assertEquals(parameters.data.length - 1, queue.size());
|
||||
queue.insert(mi);
|
||||
assertEquals(parameters.data.length, queue.size());
|
||||
|
Loading…
Reference in New Issue
Block a user