diff --git a/src/main/java/ch/hepia/structure/BinaryHeap.java b/src/main/java/ch/hepia/structure/BinaryHeap.java index 041964305527b2694286ecec8d1b8bf750adfc0b..1e94255325ae5ba0d1efa475aebf855e7ed2c817 100644 --- a/src/main/java/ch/hepia/structure/BinaryHeap.java +++ b/src/main/java/ch/hepia/structure/BinaryHeap.java @@ -1,5 +1,6 @@ import java.lang.Math; -import java.util.Random; +import java.util.Random; +import java.util.List; public class BinaryHeap{ private int[] heap; @@ -15,6 +16,18 @@ public class BinaryHeap{ public int parent(int i){ return (i-1)/2; } + public int left(int i){ + return (2*i + 1); + } + + public int right(int i){ + return (2*i + 2); + } + private void swap(int index1, int index2) { + int element = heap[index1]; + heap[index1] = heap[index2]; + heap[index2] = element; + } public void push(int val){ if (nbOfElem == maxCap) { @@ -22,77 +35,101 @@ public class BinaryHeap{ return; } - // First insert the new key at the end - nbOfElem++; - int i = nbOfElem - 1; - heap[i] = val; - - // Fix the min heap property if it is violated - while (i != 0 && heap[parent(i)] > heap[i]) - { - int temp = heap[i]; - heap[i] = heap[parent(i)]; - heap[parent(i)] = temp; - i = parent(i); - } - } + heap[nbOfElem] = val; + nbOfElem++; + + int index = nbOfElem - 1; - public void populate(int nbValues){ - Random random = new Random(); - for (int i = 0; i < nbValues; i++) { - int randomVal = random.nextInt(100); - //System.out.println(randomVal); - this.push(randomVal); + while (index > 0 && heap[parent(index)] < heap[index]) { + swap(parent(index), index); + index = parent(index); } - //return heap; } - /*private static void Tamiser(int[] arbre, int noeud, int n){ - int k = noeud; - int j = 2 * k; - - while (j <= n) - { - if ((j < n) && (arbre[j] < arbre[j + 1])) - j++; - - if (arbre[k] < arbre[j]) - { - int swap = arbre[k]; - arbre[k] = arbre[j]; - arbre[j] = swap; - k = j; - j = 2 * k; + public int pop(){ + int element = heap[0]; + + heap[0] = heap[nbOfElem - 1]; + nbOfElem--; + + int index = 0; + + while (left(index) < nbOfElem) { + int smallestChildIndex = left(index); + + if (right(index) < nbOfElem && heap[right(index)] < heap[left(index)]) { + smallestChildIndex = right(index); } - else + + if (heap[index] < heap[smallestChildIndex]) { + swap(index, smallestChildIndex); + } else { break; + } + index = smallestChildIndex; } + + return element; } - public void heapSort(){ - for (int i = this.heap.length >> 1; i >= 0; i--) - Tamiser(this.heap, i, this.heap.length - 1); + public int peek(){ + return heap[0]; + } - for (int i = this.heap.length - 1; i >= 1; i--) - { - int swap = this.heap[i]; - this.heap[i] = this.heap[0]; - this.heap[0] = swap; - Tamiser(this.heap, 0, i - 1); + public void addAll(List<Integer> lst){ + for(int x : lst){ + this.push(x); } - }*/ + } + + public boolean isEmpty(){ + return (nbOfElem == 0); + } + + public boolean exists(int k){ + for(int x : heap){ + if(x == k){ + return true; + } + } + return false; + } + + public int size(){ + return nbOfElem; + } + + public int depth(){ + int prof = 1; // on compte la racine + int size = nbOfElem; + do { + size = size/2; + prof = prof + 1; + } + while (size > 1); + return prof; + } + + public void populate(int nbValues){ + Random random = new Random(); + for (int i = 0; i < nbValues; i++) { + int randomVal = random.nextInt(100); + this.push(randomVal); + } + } public void printHeap(){ for (int x : this.heap) { - System.out.println(x); + System.out.print(x + " "); } } public static void main(String[] args){ - BinaryHeap test = new BinaryHeap(10); + BinaryHeap test = new BinaryHeap(15); test.populate(19); - test.printHeap(); + int depth = test.depth(); + System.out.println("profondeur = " + depth); } } \ No newline at end of file