From 01b54e90e497251f8123e7dae6422fe4d1ba48de Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 10 Dec 2018 11:20:18 +0100 Subject: [PATCH] A4: Anfang geblockter Heap --- .../algodat/praktikum/blatt4/Heap.java | 20 ++++++++++++++++--- .../studium/algodat/praktikum/HeapTest.java | 17 +++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/Heap.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/Heap.java index e5e1976..c54c205 100644 --- a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/Heap.java +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/Heap.java @@ -18,8 +18,10 @@ public class Heap> { public Heap(Object[] array) { this.capacity = array.length + 1; this.array = array; - this.last = array.length-1; - down(); + this.last = array.length - 1; + for (int i = last; i >= 0; i--) { + down((E) array[i]); + } } public int size() { @@ -67,6 +69,19 @@ public class Heap> { } } + private void down(E e) { + Object obj = null; + int index = -1; + //finde E + for (int i = 0; i < array.length; i++) { + if (array[i] == e) { + obj = array[i]; + index = i; + } + } + + } + private void down() { int index = 1; while (true) { @@ -83,7 +98,6 @@ public class Heap> { } } - private void up() { int index = size(); while (index > 1) { diff --git a/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java b/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java index 710da5b..cfdb6c0 100644 --- a/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java +++ b/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java @@ -1,18 +1,33 @@ package xyz.joethei.studium.algodat.praktikum; import org.junit.jupiter.api.Test; +import xyz.joethei.studium.algodat.praktikum.blatt4.Heap; import xyz.joethei.studium.algodat.praktikum.blatt4.HeapMain; import static org.junit.jupiter.api.Assertions.assertEquals; class HeapTest { + private int[] n = new int[]{1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535}; + @Test void iterativ() { - int[] n = new int[]{1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535}; int[] res = new int[]{0, 2, 10, 34, 98, 258, 642, 1538, 3586, 8194, 18434, 40962, 90114, 196610, 425986, 917506}; for (int i = 0; i < 15; i++) { assertEquals(HeapMain.getHeap(n[i]).getSwapCount(), res[i]); } } + + @Test + void geblockt() { + int[] res = new int[]{}; + for (int i = 0; i < 15; i++) { + Integer[] array = new Integer[i]; + for (int j = i; j > 0; j--) { + array[j-1] = j; + } + Heap heap = new Heap<>(array); + assertEquals(heap.getSwapCount(), res[i]); + } + } } \ No newline at end of file