q3 fini
This commit is contained in:
@@ -1,17 +1,82 @@
|
||||
package fr.istic.vv;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class BinaryHeap<T> {
|
||||
private Comparator<T> comparator;
|
||||
private ArrayList<T> elements = new ArrayList<>();
|
||||
|
||||
public BinaryHeap(Comparator<T> comparator) { }
|
||||
public BinaryHeap(Comparator<T> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Integer> valeur) {
|
||||
Comparator<Integer> comp = Comparator.naturalOrder();
|
||||
BinaryHeap<Integer> bh = new BinaryHeap(comp);
|
||||
List<Integer> 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<triee.size()-1;i++){
|
||||
assert(triee.get(i)<= triee.get(i+1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user