Skip to content
Snippets Groups Projects
Commit 7f9bbbe2 authored by loick.pipolo's avatar loick.pipolo
Browse files

interation 1 termine juste pop a coriger

parent c0d9db78
Branches
Tags
No related merge requests found
import java.lang.Math; import java.lang.Math;
import java.util.Random; import java.util.Random;
import java.util.List;
public class BinaryHeap{ public class BinaryHeap{
private int[] heap; private int[] heap;
...@@ -15,6 +16,18 @@ public class BinaryHeap{ ...@@ -15,6 +16,18 @@ public class BinaryHeap{
public int parent(int i){ public int parent(int i){
return (i-1)/2; 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){ public void push(int val){
if (nbOfElem == maxCap) { if (nbOfElem == maxCap) {
...@@ -22,77 +35,101 @@ public class BinaryHeap{ ...@@ -22,77 +35,101 @@ public class BinaryHeap{
return; return;
} }
// First insert the new key at the end heap[nbOfElem] = val;
nbOfElem++; nbOfElem++;
int i = nbOfElem - 1;
heap[i] = val; int index = nbOfElem - 1;
// 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){ while (index > 0 && heap[parent(index)] < heap[index]) {
Random random = new Random(); swap(parent(index), index);
for (int i = 0; i < nbValues; i++) { index = parent(index);
int randomVal = random.nextInt(100);
//System.out.println(randomVal);
this.push(randomVal);
} }
//return heap;
} }
/*private static void Tamiser(int[] arbre, int noeud, int n){ public int pop(){
int k = noeud; int element = heap[0];
int j = 2 * k;
heap[0] = heap[nbOfElem - 1];
while (j <= n) nbOfElem--;
{
if ((j < n) && (arbre[j] < arbre[j + 1])) int index = 0;
j++;
while (left(index) < nbOfElem) {
if (arbre[k] < arbre[j]) int smallestChildIndex = left(index);
{
int swap = arbre[k]; if (right(index) < nbOfElem && heap[right(index)] < heap[left(index)]) {
arbre[k] = arbre[j]; smallestChildIndex = right(index);
arbre[j] = swap;
k = j;
j = 2 * k;
} }
else
if (heap[index] < heap[smallestChildIndex]) {
swap(index, smallestChildIndex);
} else {
break; break;
}
index = smallestChildIndex;
} }
return element;
} }
public void heapSort(){ public int peek(){
for (int i = this.heap.length >> 1; i >= 0; i--) return heap[0];
Tamiser(this.heap, i, this.heap.length - 1); }
for (int i = this.heap.length - 1; i >= 1; i--) public void addAll(List<Integer> lst){
{ for(int x : lst){
int swap = this.heap[i]; this.push(x);
this.heap[i] = this.heap[0];
this.heap[0] = swap;
Tamiser(this.heap, 0, i - 1);
} }
}*/ }
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(){ public void printHeap(){
for (int x : this.heap) { for (int x : this.heap) {
System.out.println(x); System.out.print(x + " ");
} }
} }
public static void main(String[] args){ public static void main(String[] args){
BinaryHeap test = new BinaryHeap(10); BinaryHeap test = new BinaryHeap(15);
test.populate(19); test.populate(19);
test.printHeap(); test.printHeap();
int depth = test.depth();
System.out.println("profondeur = " + depth);
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment