diff --git a/src/main/java/ch/hepia/structure/BinaryHeap.java b/src/main/java/ch/hepia/structure/BinaryHeap.java index 131ae468e325cfe5e32da7517816085f7154a43d..041964305527b2694286ecec8d1b8bf750adfc0b 100644 --- a/src/main/java/ch/hepia/structure/BinaryHeap.java +++ b/src/main/java/ch/hepia/structure/BinaryHeap.java @@ -1,43 +1,53 @@ -java.lang.Math -java.util.Random +import java.lang.Math; +import java.util.Random; public class BinaryHeap{ - //private int[] heap; + private int[] heap; + private int maxCap; + private int nbOfElem; public BinaryHeap(int size){ - //this.heap = new int[10]; - heap = int[size]; + maxCap = size; + heap = new int[size]; + nbOfElem = 0; } - /*public BinaryHeap(int[] values){ - int[] newHeap = new int[values.length]; - for (int value : values){ - for (int i = 0; i < newHeap.length; i++){ - - } - System.out.println(newHeap[1]); - } - this.heap = newHeap; - }*/ + public int parent(int i){ + return (i-1)/2; + } - public static populate(int nbValues){ - System.out.println("gfgdg"); + public void push(int val){ + if (nbOfElem == maxCap) { + System.out.println("Plus de place !"); + 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); + } + } + + public void populate(int nbValues){ + Random random = new Random(); for (int i = 0; i < nbValues; i++) { - randomVal = Math.random(); + int randomVal = random.nextInt(100); //System.out.println(randomVal); - this.heap[i] = randomVal; + this.push(randomVal); } //return heap; } - private static void Echange(ref int a, ref int b){ - int swap = a; - a = b; - b = swap; - } - - - private static void Tamiser(int[] arbre, int noeud, int n){ + /*private static void Tamiser(int[] arbre, int noeud, int n){ int k = noeud; int j = 2 * k; @@ -48,7 +58,9 @@ public class BinaryHeap{ if (arbre[k] < arbre[j]) { - Tri.Echange(ref arbre[k], ref arbre[j]); + int swap = arbre[k]; + arbre[k] = arbre[j]; + arbre[j] = swap; k = j; j = 2 * k; } @@ -57,22 +69,30 @@ public class BinaryHeap{ } } - public static void TriParTas(BinaryHeap arbre){ - for (int i = arbre.Length >> 1; i >= 0; i--) - Tri.Tamiser(arbre, i, arbre.Length - 1); + public void heapSort(){ + for (int i = this.heap.length >> 1; i >= 0; i--) + Tamiser(this.heap, i, this.heap.length - 1); - for (int i = arbre.Length - 1; i >= 1; i--) + for (int i = this.heap.length - 1; i >= 1; i--) { - Tri.Echange(ref arbre[i], ref arbre[0]); - Tri.Tamiser(arbre, 0, i - 1); + int swap = this.heap[i]; + this.heap[i] = this.heap[0]; + this.heap[0] = swap; + Tamiser(this.heap, 0, i - 1); + } + }*/ + + public void printHeap(){ + for (int x : this.heap) { + System.out.println(x); } } public static void main(String[] args){ - BinaryHeap test = new BinaryHeap(); - test.populate(5); - //System.out.println(Arrays.toString(test)); - System.out.println("aaa"); + BinaryHeap test = new BinaryHeap(10); + test.populate(19); + + test.printHeap(); } } \ No newline at end of file