From b654219e64c74d0b0ab274d7b62a2193152a6113 Mon Sep 17 00:00:00 2001 From: "tanguy.dietrich" <tanguy.dietrich@etu.hesge.ch> Date: Sun, 3 Nov 2019 12:25:16 +0100 Subject: [PATCH] Ecriture de .peek(), pop(), et addAll() pour le BinaryHeap --- .../java/ch/hepia/structure/BinaryHeap.java | 29 +++++++++++++++++ src/test/java/ch/hepia/structure/AppTest.java | 32 +++++++++++++------ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/hepia/structure/BinaryHeap.java b/src/main/java/ch/hepia/structure/BinaryHeap.java index f0300b9..3e91507 100644 --- a/src/main/java/ch/hepia/structure/BinaryHeap.java +++ b/src/main/java/ch/hepia/structure/BinaryHeap.java @@ -23,6 +23,35 @@ final public class BinaryHeap 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() { if(this.size()==0) diff --git a/src/test/java/ch/hepia/structure/AppTest.java b/src/test/java/ch/hepia/structure/AppTest.java index 3da01ab..1ea41ba 100644 --- a/src/test/java/ch/hepia/structure/AppTest.java +++ b/src/test/java/ch/hepia/structure/AppTest.java @@ -2,6 +2,7 @@ package ch.hepia.structure; import org.junit.jupiter.api.Test; import java.util.List; +import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -13,21 +14,25 @@ class BinaryHeapTest @Test void BinaryHeapMainOpreationTest() { + List<Integer> lst = new ArrayList<Integer>(); BinaryHeap bHeap= new BinaryHeap(); + BinaryHeap bHeap2= new BinaryHeap(); BinaryHeap bHeapRand= new BinaryHeap(); BinaryHeap bHeapEmpty= new BinaryHeap(); BinaryHeap.populate(bHeapRand,20); - 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.push(100);lst.add(100); + bHeap.push(19);lst.add(19); + bHeap.push(36);lst.add(36); + bHeap.push(17);lst.add(17); + bHeap.push(3);lst.add(3); + bHeap.push(25);lst.add(25); + bHeap.push(1);lst.add(1); + bHeap.push(2);lst.add(2); + bHeap.push(7);lst.add(7); + bHeap.push(40);lst.add(40); + bHeap2.addAll(lst); //bHeap.print(); + //bHeap2.print(); //bHeapRand.print(); assertEquals(bHeap.size(),10); assertEquals(bHeapRand.size(),20); @@ -36,5 +41,12 @@ class BinaryHeapTest assertEquals(bHeap.exist(7),true); assertEquals(bHeap.exist(42),false); 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 } } -- GitLab