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;
|
package org.insa.graphs.algorithm.utils;
|
||||||
|
|
||||||
import java.util.SortedSet;
|
import java.util.*;
|
||||||
import java.util.TreeSet;
|
|
||||||
|
|
||||||
public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<E> {
|
public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
|
|
||||||
// Underlying implementation
|
// 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.
|
* Create a new empty binary search tree.
|
||||||
*/
|
*/
|
||||||
public BinarySearchTree() {
|
public BinarySearchTree() {
|
||||||
this.sortedSet = new TreeSet<>();
|
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) {
|
public BinarySearchTree(BinarySearchTree<E> bst) {
|
||||||
this.sortedSet = new TreeSet<>(bst.sortedSet);
|
this.sortedSet = new TreeSet<>(bst.sortedSet);
|
||||||
|
this.elems = new HashSet<>(bst.elems);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return sortedSet.isEmpty();
|
return this.sortedSet.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return sortedSet.size();
|
return this.sortedSet.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insert(E x) {
|
public void insert(E x) {
|
||||||
sortedSet.add(x);
|
this.sortedSet.add(x);
|
||||||
|
this.elems.add(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(E x) throws ElementNotFoundException {
|
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);
|
throw new ElementNotFoundException(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,10 +43,16 @@ public abstract class PriorityQueueTest {
|
|||||||
protected static class MutableInteger implements Comparable<MutableInteger> {
|
protected static class MutableInteger implements Comparable<MutableInteger> {
|
||||||
|
|
||||||
// Actual value
|
// Actual value
|
||||||
private int 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) {
|
public MutableInteger(int value) {
|
||||||
this.value = value;
|
this.value = value ;
|
||||||
|
this.id = counter++ ;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,12 +73,13 @@ public abstract class PriorityQueueTest {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(MutableInteger other) {
|
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
|
@Override
|
||||||
public String toString() {
|
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.
|
// Update value before removing it. This is what happens when updating a Dijkstra label before updating it.
|
||||||
mi.set(--min);
|
mi.set(--min);
|
||||||
queue.remove(mi);
|
queue.remove(mi);
|
||||||
|
|
||||||
assertEquals(parameters.data.length - 1, queue.size());
|
assertEquals(parameters.data.length - 1, queue.size());
|
||||||
queue.insert(mi);
|
queue.insert(mi);
|
||||||
assertEquals(parameters.data.length, queue.size());
|
assertEquals(parameters.data.length, queue.size());
|
||||||
|
Loading…
Reference in New Issue
Block a user