From 558a665ec0184a65a693a09b01796123729ac2df Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Wed, 25 Mar 2020 16:23:31 +0100 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20remove=20avec=20indexOf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../graphs/algorithm/utils/BinaryHeap.java | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) 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 2c1a239..f19c325 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 @@ -4,10 +4,10 @@ import java.util.ArrayList; /** * Implements a binary heap containing elements of type E. - * + *

* Note that all comparisons are based on the compareTo method, hence E must * implement Comparable - * + * * @author Mark Allen Weiss * @author DLB */ @@ -29,7 +29,7 @@ public class BinaryHeap> implements PriorityQueue { /** * Construct a copy of the given heap. - * + * * @param heap Binary heap to copy. */ public BinaryHeap(BinaryHeap heap) { @@ -39,15 +39,14 @@ public class BinaryHeap> implements PriorityQueue { /** * Set an element at the given index. - * + * * @param index Index at which the element should be set. * @param value Element to set. */ private void arraySet(int index, E value) { if (index == this.array.size()) { this.array.add(value); - } - else { + } else { this.array.set(index, value); } } @@ -68,7 +67,7 @@ public class BinaryHeap> implements PriorityQueue { /** * Internal method to percolate up in the heap. - * + * * @param index Index at which the percolate begins. */ private void percolateUp(int index) { @@ -76,7 +75,7 @@ public class BinaryHeap> implements PriorityQueue { for (; index > 0 && x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent( - index)) { + index)) { E moving_val = this.array.get(indexParent(index)); this.arraySet(index, moving_val); } @@ -86,7 +85,7 @@ public class BinaryHeap> implements PriorityQueue { /** * Internal method to percolate down in the heap. - * + * * @param index Index at which the percolate begins. */ private void percolateDown(int index) { @@ -106,8 +105,7 @@ public class BinaryHeap> implements PriorityQueue { this.arraySet(ileft, current); this.percolateDown(ileft); } - } - else { + } else { // Right is smaller if (right.compareTo(current) < 0) { this.arraySet(index, right); @@ -137,7 +135,24 @@ public class BinaryHeap> implements PriorityQueue { @Override public void remove(E x) throws ElementNotFoundException { - // TODO: + int index = this.array.indexOf(x); + System.out.println(x); + System.out.println(index); + if (isEmpty() || index == -1 || index >= this.currentSize) + throw new ElementNotFoundException(x); + else if (index == 0) + deleteMin(); + else if (index == this.currentSize-1) + this.currentSize--; + else { + System.out.println(this.toStringTree()); + E lastItem = this.array.get(--this.currentSize); + System.out.println(lastItem); + this.arraySet(index, lastItem); + this.percolateDown(index); + this.percolateUp(index); + System.out.println(this.toStringTree()); + } } @Override @@ -158,7 +173,7 @@ public class BinaryHeap> implements PriorityQueue { /** * Creates a multi-lines string representing a sorted view of this binary heap. - * + * * @return a string containing a sorted view this binary heap. */ public String toStringSorted() { @@ -167,10 +182,9 @@ public class BinaryHeap> implements PriorityQueue { /** * Creates a multi-lines string representing a sorted view of this binary heap. - * + * * @param maxElement Maximum number of elements to display. or {@code -1} to * display all the elements. - * * @return a string containing a sorted view this binary heap. */ public String toStringSorted(int maxElement) { @@ -179,7 +193,7 @@ public class BinaryHeap> implements PriorityQueue { /** * Creates a multi-lines string representing a tree view of this binary heap. - * + * * @return a string containing a tree view of this binary heap. */ public String toStringTree() { @@ -188,9 +202,8 @@ public class BinaryHeap> implements PriorityQueue { /** * Creates a multi-lines string representing a tree view of this binary heap. - * + * * @param maxDepth Maximum depth of the tree to display. - * * @return a string containing a tree view of this binary heap. */ public String toStringTree(int maxDepth) {