From 50c23d62093ff350a373836d4fe2405026f4cb3f Mon Sep 17 00:00:00 2001 From: knzrd Date: Tue, 13 May 2025 11:58:22 +0200 Subject: [PATCH] BinaryHeap done --- .../graphs/algorithm/utils/BinaryHeap.java | 19 ++- .../graphs/algorithm/utils/BinaryHeap.class | 122 ++++++++++++++++++ .../org/insa/graphs/gui/simple/Launch.java | 8 +- 3 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BinaryHeap.class 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..d55a946 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 @@ -134,8 +134,23 @@ public class BinaryHeap> implements PriorityQueue { @Override 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 public E findMin() throws EmptyPriorityQueueException { diff --git a/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BinaryHeap.class b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BinaryHeap.class new file mode 100644 index 0000000..19cea73 --- /dev/null +++ b/be-graphes-algos/src/test/java/org/insa/graphs/algorithm/utils/BinaryHeap.class @@ -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> implements PriorityQueue { + private int currentSize; + protected final ArrayList array; + + public BinaryHeap() { + this.currentSize = 0; + this.array = new ArrayList(); + } + + public BinaryHeap(BinaryHeap 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); + } +} diff --git a/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java b/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java index aa7ea04..85caf28 100644 --- a/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java +++ b/be-graphes-gui/src/main/java/org/insa/graphs/gui/simple/Launch.java @@ -58,22 +58,24 @@ public class Launch { new BufferedInputStream(new FileInputStream(mapName))))) { // TODO: read the graph - graph = null; + graph = reader.read(); + reader.close(); } // create the drawing final Drawing drawing = createDrawing(); // TODO: draw the graph on the drawing - + drawing.drawGraph(graph); // TODO: create a path reader try (final PathReader pathReader = null) { // TODO: read the path - path = null; + path = pathReader.readPath(graph); } // TODO: draw the path on the drawing + drawing.drawPath(path); } }