From a35c883c44dd53b01aedc1f7d179574278427469 Mon Sep 17 00:00:00 2001 From: Brendan Saint-Germes Date: Tue, 14 Apr 2026 18:17:05 +0200 Subject: [PATCH] =?UTF-8?q?ajout:=20Impl=C3=A9mentation=20de=20BinaryHeap#?= =?UTF-8?q?remove(E=20x)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../graphs/algorithm/utils/BinaryHeap.java | 32 ++++++++++++++++++- 1 file changed, 31 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..16a7a87 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 @@ -1,6 +1,9 @@ package org.insa.graphs.algorithm.utils; +import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Queue; +import java.util.Stack; /** * Implements a binary heap containing elements of type E. Note that all comparisons are @@ -134,7 +137,34 @@ public class BinaryHeap> implements PriorityQueue { @Override public void remove(E x) throws ElementNotFoundException { - // TODO: + if (this.isEmpty()) throw new ElementNotFoundException(x); + + Stack stack = new Stack<>(); + stack.add(0); + while (!stack.isEmpty()) { + int i = stack.pop(); + System.out.println("i=" + i); + E current = this.array.get(i); + int comp = current.compareTo(x); + if (comp == 0) { + // on a trouvé notre élément + this.currentSize--; + if (this.currentSize > 0) { + E lastItem = this.array.get(this.currentSize); + this.arraySet(i, lastItem); + this.percolateDown(i); + this.percolateUp(i); + } + return; + } else { + int indLeft = indexLeft(i); + if (indLeft < this.currentSize) stack.push(indLeft); + int indRight = indLeft+1; + if (indRight < this.currentSize) stack.push(indRight); + } + } + + throw new ElementNotFoundException(x); } @Override