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
No related branches found
No related tags found
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;
// Fix the min heap property if it is violated int index = nbOfElem - 1;
while (i != 0 && heap[parent(i)] > heap[i])
{ while (index > 0 && heap[parent(index)] < heap[index]) {
int temp = heap[i]; swap(parent(index), index);
heap[i] = heap[parent(i)]; index = parent(index);
heap[parent(i)] = temp;
i = parent(i);
} }
} }
public void populate(int nbValues){ public int pop(){
Random random = new Random(); int element = heap[0];
for (int i = 0; i < nbValues; i++) {
int randomVal = random.nextInt(100); heap[0] = heap[nbOfElem - 1];
//System.out.println(randomVal); nbOfElem--;
this.push(randomVal);
int index = 0;
while (left(index) < nbOfElem) {
int smallestChildIndex = left(index);
if (right(index) < nbOfElem && heap[right(index)] < heap[left(index)]) {
smallestChildIndex = right(index);
}
if (heap[index] < heap[smallestChildIndex]) {
swap(index, smallestChildIndex);
} else {
break;
}
index = smallestChildIndex;
} }
//return heap;
return element;
} }
/*private static void Tamiser(int[] arbre, int noeud, int n){ public int peek(){
int k = noeud; return heap[0];
int j = 2 * k; }
while (j <= n) public void addAll(List<Integer> lst){
{ for(int x : lst){
if ((j < n) && (arbre[j] < arbre[j + 1])) this.push(x);
j++; }
}
if (arbre[k] < arbre[j]) public boolean isEmpty(){
{ return (nbOfElem == 0);
int swap = arbre[k]; }
arbre[k] = arbre[j];
arbre[j] = swap; public boolean exists(int k){
k = j; for(int x : heap){
j = 2 * k; if(x == k){
return true;
} }
else
break;
} }
return false;
}
public int size(){
return nbOfElem;
} }
public void heapSort(){ public int depth(){
for (int i = this.heap.length >> 1; i >= 0; i--) int prof = 1; // on compte la racine
Tamiser(this.heap, i, this.heap.length - 1); int size = nbOfElem;
do {
size = size/2;
prof = prof + 1;
}
while (size > 1);
return prof;
}
for (int i = this.heap.length - 1; i >= 1; i--) public void populate(int nbValues){
{ Random random = new Random();
int swap = this.heap[i]; for (int i = 0; i < nbValues; i++) {
this.heap[i] = this.heap[0]; int randomVal = random.nextInt(100);
this.heap[0] = swap; this.push(randomVal);
Tamiser(this.heap, 0, i - 1); }
} }
}*/
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