Implémentation remove avec indexOf
This commit is contained in:
parent
51c027f3b7
commit
558a665ec0
1 changed files with 31 additions and 18 deletions
|
@ -4,10 +4,10 @@ import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements a binary heap containing elements of type E.
|
* Implements a binary heap containing elements of type E.
|
||||||
*
|
* <p>
|
||||||
* Note that all comparisons are based on the compareTo method, hence E must
|
* Note that all comparisons are based on the compareTo method, hence E must
|
||||||
* implement Comparable
|
* implement Comparable
|
||||||
*
|
*
|
||||||
* @author Mark Allen Weiss
|
* @author Mark Allen Weiss
|
||||||
* @author DLB
|
* @author DLB
|
||||||
*/
|
*/
|
||||||
|
@ -29,7 +29,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a copy of the given heap.
|
* Construct a copy of the given heap.
|
||||||
*
|
*
|
||||||
* @param heap Binary heap to copy.
|
* @param heap Binary heap to copy.
|
||||||
*/
|
*/
|
||||||
public BinaryHeap(BinaryHeap<E> heap) {
|
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.
|
* Set an element at the given index.
|
||||||
*
|
*
|
||||||
* @param index Index at which the element should be set.
|
* @param index Index at which the element should be set.
|
||||||
* @param value Element to set.
|
* @param value Element to set.
|
||||||
*/
|
*/
|
||||||
private void arraySet(int index, E value) {
|
private void arraySet(int index, E value) {
|
||||||
if (index == this.array.size()) {
|
if (index == this.array.size()) {
|
||||||
this.array.add(value);
|
this.array.add(value);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
this.array.set(index, value);
|
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.
|
* Internal method to percolate up in the heap.
|
||||||
*
|
*
|
||||||
* @param index Index at which the percolate begins.
|
* @param index Index at which the percolate begins.
|
||||||
*/
|
*/
|
||||||
private void percolateUp(int index) {
|
private void percolateUp(int index) {
|
||||||
|
@ -76,7 +75,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
|
|
||||||
for (; index > 0
|
for (; index > 0
|
||||||
&& x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(
|
&& x.compareTo(this.array.get(indexParent(index))) < 0; index = indexParent(
|
||||||
index)) {
|
index)) {
|
||||||
E moving_val = this.array.get(indexParent(index));
|
E moving_val = this.array.get(indexParent(index));
|
||||||
this.arraySet(index, moving_val);
|
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.
|
* Internal method to percolate down in the heap.
|
||||||
*
|
*
|
||||||
* @param index Index at which the percolate begins.
|
* @param index Index at which the percolate begins.
|
||||||
*/
|
*/
|
||||||
private void percolateDown(int index) {
|
private void percolateDown(int index) {
|
||||||
|
@ -106,8 +105,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
this.arraySet(ileft, current);
|
this.arraySet(ileft, current);
|
||||||
this.percolateDown(ileft);
|
this.percolateDown(ileft);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Right is smaller
|
// Right is smaller
|
||||||
if (right.compareTo(current) < 0) {
|
if (right.compareTo(current) < 0) {
|
||||||
this.arraySet(index, right);
|
this.arraySet(index, right);
|
||||||
|
@ -137,7 +135,24 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(E x) throws ElementNotFoundException {
|
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
|
@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.
|
* Creates a multi-lines string representing a sorted view of this binary heap.
|
||||||
*
|
*
|
||||||
* @return a string containing a sorted view this binary heap.
|
* @return a string containing a sorted view this binary heap.
|
||||||
*/
|
*/
|
||||||
public String toStringSorted() {
|
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.
|
* 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
|
* @param maxElement Maximum number of elements to display. or {@code -1} to
|
||||||
* display all the elements.
|
* display all the elements.
|
||||||
*
|
|
||||||
* @return a string containing a sorted view this binary heap.
|
* @return a string containing a sorted view this binary heap.
|
||||||
*/
|
*/
|
||||||
public String toStringSorted(int maxElement) {
|
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.
|
* Creates a multi-lines string representing a tree view of this binary heap.
|
||||||
*
|
*
|
||||||
* @return a string containing a tree view of this binary heap.
|
* @return a string containing a tree view of this binary heap.
|
||||||
*/
|
*/
|
||||||
public String toStringTree() {
|
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.
|
* Creates a multi-lines string representing a tree view of this binary heap.
|
||||||
*
|
*
|
||||||
* @param maxDepth Maximum depth of the tree to display.
|
* @param maxDepth Maximum depth of the tree to display.
|
||||||
*
|
|
||||||
* @return a string containing a tree view of this binary heap.
|
* @return a string containing a tree view of this binary heap.
|
||||||
*/
|
*/
|
||||||
public String toStringTree(int maxDepth) {
|
public String toStringTree(int maxDepth) {
|
||||||
|
|
Loading…
Reference in a new issue