Skip to content
Snippets Groups Projects
Commit 4f543c18 authored by tanguy.dietrich's avatar tanguy.dietrich
Browse files

Ecriture de .push() pour le BinaryHeap

parent 433883ea
No related branches found
No related tags found
2 merge requests!2Tanguy,!1David
This commit is part of merge request !2. Comments created here will be created in the context of that merge request.
...@@ -8,14 +8,80 @@ import java.util.function.Function; ...@@ -8,14 +8,80 @@ import java.util.function.Function;
final public class BinaryHeap final public class BinaryHeap
{ {
final private List<Integer> lstBinaryHeap; final private List<Integer> lstBinaryHeap;
private BinaryHeap() private int taille;
public BinaryHeap()
{ {
lstBinaryHeap=new ArrayList<Integer>(); this.lstBinaryHeap=new ArrayList<Integer>();
this.taille=0;
} }
public void push(int value) public void push(int value)
{ {
this.taille++;
this.lstBinaryHeap.add(value);
this.sort();
}
private void sort()
{
int index=this.size()-1;
while(index!=0)
{
if(this.getParentNodeValue(index)<this.get(index))
{
//les place doivent etre changer
this.switchPlace(this.getParentNodeIndex(index),index);
//test la valeur d'a cote
}
else
{
//test si la branche existe
if(this.getSameLevelIndex(index)<this.size())
{
//si elle existe
index=this.getSameLevelIndex(index);//passe a l'index d'a coter
if(this.getParentNodeValue(index)<this.get(index))
{
//les place doivent etre changer
this.switchPlace(this.getParentNodeIndex(index),index);
}
}
}
index=this.getParentNodeIndex(index);
}
}
private void switchPlace(int index1,int index2)
{
int tmp;
tmp=this.lstBinaryHeap.get(index1);
this.set(index1,this.get(index2));
this.set(index2,tmp);
}
public void print()
{
for(int val:lstBinaryHeap)
{
System.out.println(val);
}
}
public int size()
{
return this.taille;
}
private int getSameLevelIndex(int index)
{
if(index%2==0)
{
return index-1;
}
else
{
return index+1;
}
} }
private int getRightNodeIndex(int index) private int getRightNodeIndex(int index)
......
...@@ -11,7 +11,20 @@ import static org.junit.jupiter.api.Assertions.assertFalse; ...@@ -11,7 +11,20 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
class BinaryHeapTest class BinaryHeapTest
{ {
@Test @Test
void BinaryHeapMainOperations() { void BinaryHeapPushTest()
assertEquals(1,1); {
BinaryHeap bHeap= new BinaryHeap();
bHeap.push(100);
bHeap.push(19);
bHeap.push(36);
bHeap.push(17);
bHeap.push(3);
bHeap.push(25);
bHeap.push(1);
bHeap.push(2);
bHeap.push(7);
bHeap.push(40);
bHeap.print();
//assertEquals();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment