BinaryHeap done

This commit is contained in:
knzrd 2025-05-13 11:58:22 +02:00
parent e52af9e118
commit 50c23d6209
3 changed files with 144 additions and 5 deletions

View file

@ -134,7 +134,22 @@ 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: if(this.array.indexOf(x)==-1 || this.array.indexOf(x)>=currentSize) {
throw new ElementNotFoundException(x);
}
else {
int monindex=this.array.indexOf(x);
//chercher la valeur dans le dernier node
E dernierelement = this.array.get(currentSize-1);
// remplacer le node qu'on veut supprimer avec le tout dernier
this.arraySet(monindex, dernierelement);
// on remet le heap dans l'ordre
this.percolateDown(monindex);
this.percolateUp(monindex);
//diminuer la taille du heap: on n'a plus acces à la derniere valeur
//equivalent à l'avoir supprimé
this.currentSize--;
}
} }
@Override @Override

View file

@ -0,0 +1,122 @@
// Source code is decompiled from a .class file using FernFlower decompiler.
package org.insa.graphs.algorithm.utils;
import java.util.ArrayList;
public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
private int currentSize;
protected final ArrayList<E> array;
public BinaryHeap() {
this.currentSize = 0;
this.array = new ArrayList();
}
public BinaryHeap(BinaryHeap<E> heap) {
this.currentSize = heap.currentSize;
this.array = new ArrayList(heap.array);
}
private void arraySet(int index, E value) {
if (index == this.array.size()) {
this.array.add(value);
} else {
this.array.set(index, value);
}
}
protected int indexParent(int index) {
return (index - 1) / 2;
}
protected int indexLeft(int index) {
return index * 2 + 1;
}
private void percolateUp(int index) {
Comparable x;
for(x = (Comparable)this.array.get(index); index > 0 && x.compareTo((Comparable)this.array.get(this.indexParent(index))) < 0; index = this.indexParent(index)) {
E moving_val = (Comparable)this.array.get(this.indexParent(index));
this.arraySet(index, moving_val);
}
this.arraySet(index, x);
}
private void percolateDown(int index) {
int ileft = this.indexLeft(index);
int iright = ileft + 1;
if (ileft < this.currentSize) {
E current = (Comparable)this.array.get(index);
E left = (Comparable)this.array.get(ileft);
boolean hasRight = iright < this.currentSize;
E right = hasRight ? (Comparable)this.array.get(iright) : null;
if (hasRight && left.compareTo(right) >= 0) {
if (right.compareTo(current) < 0) {
this.arraySet(index, right);
this.arraySet(iright, current);
this.percolateDown(iright);
}
} else if (left.compareTo(current) < 0) {
this.arraySet(index, left);
this.arraySet(ileft, current);
this.percolateDown(ileft);
}
}
}
public boolean isEmpty() {
return this.currentSize == 0;
}
public int size() {
return this.currentSize;
}
public void insert(E x) {
int index = this.currentSize++;
this.arraySet(index, x);
this.percolateUp(index);
}
public void remove(E x) throws ElementNotFoundException {
}
public E findMin() throws EmptyPriorityQueueException {
if (this.isEmpty()) {
throw new EmptyPriorityQueueException();
} else {
return (Comparable)this.array.get(0);
}
}
public E deleteMin() throws EmptyPriorityQueueException {
E minItem = this.findMin();
E lastItem = (Comparable)this.array.get(--this.currentSize);
this.arraySet(0, lastItem);
this.percolateDown(0);
return minItem;
}
public String toStringSorted() {
return BinaryHeapFormatter.toStringSorted(this, -1);
}
public String toStringSorted(int maxElement) {
return BinaryHeapFormatter.toStringSorted(this, maxElement);
}
public String toStringTree() {
return BinaryHeapFormatter.toStringTree(this, Integer.MAX_VALUE);
}
public String toStringTree(int maxDepth) {
return BinaryHeapFormatter.toStringTree(this, maxDepth);
}
public String toString() {
return BinaryHeapFormatter.toStringTree(this, 8);
}
}

View file

@ -58,22 +58,24 @@ public class Launch {
new BufferedInputStream(new FileInputStream(mapName))))) { new BufferedInputStream(new FileInputStream(mapName))))) {
// TODO: read the graph // TODO: read the graph
graph = null; graph = reader.read();
reader.close();
} }
// create the drawing // create the drawing
final Drawing drawing = createDrawing(); final Drawing drawing = createDrawing();
// TODO: draw the graph on the drawing // TODO: draw the graph on the drawing
drawing.drawGraph(graph);
// TODO: create a path reader // TODO: create a path reader
try (final PathReader pathReader = null) { try (final PathReader pathReader = null) {
// TODO: read the path // TODO: read the path
path = null; path = pathReader.readPath(graph);
} }
// TODO: draw the path on the drawing // TODO: draw the path on the drawing
drawing.drawPath(path);
} }
} }