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

Ecriture de .peek(), pop(), et addAll() pour le BinaryHeap

parent 186c9f84
No related branches found
No related tags found
2 merge requests!2Tanguy,!1David
...@@ -23,6 +23,35 @@ final public class BinaryHeap ...@@ -23,6 +23,35 @@ final public class BinaryHeap
this.sort(); this.sort();
} }
public int peek()
{
return this.get(0);
}
public int pop()
{
int val=this.peek();
this.set(0,0);
for(int i=0;i<this.depth();i++)
{
this.sort();
}
if(this.size()>0)
{
this.lstBinaryHeap.remove(this.size()-1);
this.taille--;
}
return val;
}
public void addAll(List<Integer> lst)
{
for(int val:lst)
{
this.push(val);
}
}
public boolean isEmpty() public boolean isEmpty()
{ {
if(this.size()==0) if(this.size()==0)
......
...@@ -2,6 +2,7 @@ package ch.hepia.structure; ...@@ -2,6 +2,7 @@ package ch.hepia.structure;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
...@@ -13,21 +14,25 @@ class BinaryHeapTest ...@@ -13,21 +14,25 @@ class BinaryHeapTest
@Test @Test
void BinaryHeapMainOpreationTest() void BinaryHeapMainOpreationTest()
{ {
List<Integer> lst = new ArrayList<Integer>();
BinaryHeap bHeap= new BinaryHeap(); BinaryHeap bHeap= new BinaryHeap();
BinaryHeap bHeap2= new BinaryHeap();
BinaryHeap bHeapRand= new BinaryHeap(); BinaryHeap bHeapRand= new BinaryHeap();
BinaryHeap bHeapEmpty= new BinaryHeap(); BinaryHeap bHeapEmpty= new BinaryHeap();
BinaryHeap.populate(bHeapRand,20); BinaryHeap.populate(bHeapRand,20);
bHeap.push(100); bHeap.push(100);lst.add(100);
bHeap.push(19); bHeap.push(19);lst.add(19);
bHeap.push(36); bHeap.push(36);lst.add(36);
bHeap.push(17); bHeap.push(17);lst.add(17);
bHeap.push(3); bHeap.push(3);lst.add(3);
bHeap.push(25); bHeap.push(25);lst.add(25);
bHeap.push(1); bHeap.push(1);lst.add(1);
bHeap.push(2); bHeap.push(2);lst.add(2);
bHeap.push(7); bHeap.push(7);lst.add(7);
bHeap.push(40); bHeap.push(40);lst.add(40);
bHeap2.addAll(lst);
//bHeap.print(); //bHeap.print();
//bHeap2.print();
//bHeapRand.print(); //bHeapRand.print();
assertEquals(bHeap.size(),10); assertEquals(bHeap.size(),10);
assertEquals(bHeapRand.size(),20); assertEquals(bHeapRand.size(),20);
...@@ -36,5 +41,12 @@ class BinaryHeapTest ...@@ -36,5 +41,12 @@ class BinaryHeapTest
assertEquals(bHeap.exist(7),true); assertEquals(bHeap.exist(7),true);
assertEquals(bHeap.exist(42),false); assertEquals(bHeap.exist(42),false);
assertEquals(bHeap.depth(),4); assertEquals(bHeap.depth(),4);
assertEquals(bHeap.peek(),100);
assertEquals(bHeap.pop(),100);
assertEquals(bHeap.pop(),40);
assertEquals(bHeap.pop(),36);
assertEquals(bHeap.size(),7);
//bHeap.print();
//assertEquals(bHeap,bHeap2);//ecrire un override pour equals
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment