Implémentation remove avec indexOf

This commit is contained in:
Arnaud Vergnet 2020-03-25 16:23:31 +01:00
parent 51c027f3b7
commit 558a665ec0

View file

@ -4,10 +4,10 @@ import java.util.ArrayList;
/**
* Implements a binary heap containing elements of type E.
*
* <p>
* 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<E extends Comparable<E>> implements PriorityQueue<E> {
/**
* Construct a copy of the given heap.
*
*
* @param heap Binary heap to copy.
*/
public BinaryHeap(BinaryHeap<E> heap) {
@ -39,15 +39,14 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
/**
* 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<E extends Comparable<E>> implements PriorityQueue<E> {
/**
* 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<E extends Comparable<E>> implements PriorityQueue<E> {
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<E extends Comparable<E>> implements PriorityQueue<E> {
/**
* 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<E extends Comparable<E>> implements PriorityQueue<E> {
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<E extends Comparable<E>> implements PriorityQueue<E> {
@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<E extends Comparable<E>> implements PriorityQueue<E> {
/**
* 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<E extends Comparable<E>> implements PriorityQueue<E> {
/**
* 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<E extends Comparable<E>> implements PriorityQueue<E> {
/**
* 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<E extends Comparable<E>> implements PriorityQueue<E> {
/**
* 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) {