From 5ee48b459861014f0eae9e5b0ebed4096ae5d390 Mon Sep 17 00:00:00 2001 From: Didier Le Botlan Date: Tue, 29 Apr 2025 15:29:41 +0200 Subject: [PATCH] The recently added test on priorityqueue was not compatible with BinarySearchTree implementation. This is fixed. --- .../algorithm/utils/BinarySearchTree.java | 43 ++++++++++++++++--- .../algorithm/utils/PriorityQueueTest.java | 16 +++++-- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java index f711b30..1aa3284 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java @@ -1,18 +1,23 @@ package org.insa.graphs.algorithm.utils; -import java.util.SortedSet; -import java.util.TreeSet; +import java.util.*; public class BinarySearchTree> implements PriorityQueue { // Underlying implementation - private final SortedSet sortedSet; + + // Elements, sorted by their value + private SortedSet sortedSet; + + // Elements, by their hashcode. + private HashSet 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> implements PriorityQueue< */ public BinarySearchTree(BinarySearchTree 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 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); } } diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/PriorityQueueTest.java b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/PriorityQueueTest.java index 27bb14e..aca7b79 100644 --- a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/PriorityQueueTest.java +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/PriorityQueueTest.java @@ -43,10 +43,16 @@ public abstract class PriorityQueueTest { protected static class MutableInteger implements Comparable { // 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) { - this.value = 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());