From 8160f404541ff247375aba4cac876411dd9249bc Mon Sep 17 00:00:00 2001 From: Matteo Date: Tue, 29 Apr 2025 16:52:03 +0200 Subject: [PATCH] ajout de remove dans BinaryHeap --- .../graphs/algorithm/utils/BinaryHeap.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java index a5073cd..df29f3a 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java @@ -134,7 +134,29 @@ public class BinaryHeap> implements PriorityQueue { @Override public void remove(E x) throws ElementNotFoundException { - // TODO: + int ind = -1; + for (int i = 0; i < currentSize; i++) { + if (this.array.get(i).equals(x)) { + ind = i; + break; + } + } + + if (ind == -1) { + throw new ElementNotFoundException(x); + } + + int lastIndex = currentSize - 1; + if (ind != lastIndex) { + E lastElement = this.array.get(lastIndex); + this.array.set(ind, lastElement); + currentSize--; + percolateDown(ind); + percolateUp(ind); + } else { + currentSize--; + } + } @Override