diff --git a/code/heap/src/main/java/fr/istic/vv/BinaryHeap.java b/code/heap/src/main/java/fr/istic/vv/BinaryHeap.java index ce38e05..95cb47e 100644 --- a/code/heap/src/main/java/fr/istic/vv/BinaryHeap.java +++ b/code/heap/src/main/java/fr/istic/vv/BinaryHeap.java @@ -1,17 +1,82 @@ package fr.istic.vv; +import java.util.ArrayList; import java.util.Comparator; +import java.util.NoSuchElementException; public class BinaryHeap { + private Comparator comparator; + private ArrayList elements = new ArrayList<>(); - public BinaryHeap(Comparator comparator) { } + public BinaryHeap(Comparator comparator) { + this.comparator = comparator; + this.elements = new ArrayList<>(); + } - public T pop() { return null; } + public T pop() { + if (elements.isEmpty()) { + throw new NoSuchElementException(); + } else { + T first = elements.get(0); + T last = elements.remove(elements.size() - 1); + if ((!elements.isEmpty())) { + //swap to first to reshift down + elements.set(0,last); + shiftDown(0); + } + return first; + } + } - public T peek() { return null; } + protected void shiftDown(int index){ + int size=this.count(); + int temp=index; + while(true) { + int left = 2*index + 1; + int right = left + 1; + if ((left < size) && comparator.compare(elements.get(left), elements.get(temp)) < 0) { + temp = left; + } + if ((right < size) && comparator.compare(elements.get(right), elements.get(temp)) < 0) { + temp = right; + } + if (temp == index) { + break; + } + //Il faut swap + swapItems(temp, index); + index= temp; + } + } - public void push(T element) { } + public T peek() { + if (elements.isEmpty()) { + throw new NoSuchElementException(); + } else { + return elements.get(0); + } + } - public int count() { return 0; } + public void push(T element) { + elements.add(element); + int size = elements.size() - 1; + while (size > 0) { + int parentIndex = (size - 1) / 2; + if (comparator.compare(elements.get(size), elements.get(parentIndex)) < 0) { + swapItems(size, parentIndex); + } + size = parentIndex; + } + } + + public int count() { + return elements.size(); + } + + public void swapItems(int index1, int index2) { + T temp = elements.get(index1); + elements.set(index1, elements.get(index2)); + elements.set(index2, temp); + } } \ No newline at end of file diff --git a/code/heap/src/test/java/fr/istic/vv/BinaryHeapTest.java b/code/heap/src/test/java/fr/istic/vv/BinaryHeapTest.java index 67ed489..6a86943 100644 --- a/code/heap/src/test/java/fr/istic/vv/BinaryHeapTest.java +++ b/code/heap/src/test/java/fr/istic/vv/BinaryHeapTest.java @@ -1,10 +1,37 @@ package fr.istic.vv; + import net.jqwik.api.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + public class BinaryHeapTest { + /* @Property boolean absoluteValueOfAllNumbersIsPositive(@ForAll int anInteger) { return Math.abs(anInteger) >= 0; } + */ + + @Property + void popToujoursMinimum(@ForAll List valeur) { + Comparator comp = Comparator.naturalOrder(); + BinaryHeap bh = new BinaryHeap(comp); + List triee = new ArrayList<>(); + + //PUSH + for (Integer val : valeur) { + bh.push(val); + } + while(bh.count()!=0){ + int valeur_pop=bh.pop(); + triee.add(valeur_pop); + } + + for(int i=1; i