From 2fb72180f97f6731253e6ddeecd37bfb44b37167 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 10 Dec 2018 22:47:55 +0100 Subject: [PATCH] A4: blockiert funktioniert noch nicht richtig --- .../algodat/praktikum/blatt4/Heap.java | 66 +++++++++++-------- .../algodat/praktikum/blatt4/HeapMain.java | 8 ++- .../studium/algodat/praktikum/HeapTest.java | 8 +-- 3 files changed, 49 insertions(+), 33 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 929cd5f..b8649ba 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 @@ -2,28 +2,39 @@ package xyz.joethei.studium.algodat.praktikum.blatt4; import java.util.Arrays; -public class Heap> { +public class Heap { - private Object[] array; + private int[] array; private int last = 0; private int capacity; private int swapCount = 0; public Heap(int capacity) { - array = new Object[capacity + 1]; + array = new int[capacity + 1]; this.capacity = capacity; } - public Heap(Object[] array) { + public Heap(int[] array) { this.capacity = array.length + 1; this.array = array; this.last = array.length-1; - for (int i = last; i >= 0; i--) { - sink((E) array[i]); + for (int i = indexOf(findLastWithChild()); i >= 0; i--) { + sinkWithIndex(array[i]); } } + private int findLastWithChild() { + int index = -1; + for(int i = 0; i < last; i++) { + if(i * 2 > size()) { + index = i / 2; + break; + } + } + return index + 1; + } + public int size() { return last; } @@ -36,32 +47,32 @@ public class Heap> { return swapCount; } - public E min() throws RuntimeException { + public int min() throws RuntimeException { if (isEmpty()) throw new RuntimeException("The heap is empty."); else - return (E) array[1]; + return array[1]; } - private int compare(Object x, Object y) { - return ((E) x).compareTo((E) y); + private int compare(int x, int y) { + return Integer.compare(x, y); } - public void insert(E e) throws RuntimeException { + public void insert(int i) throws RuntimeException { if (size() == capacity) throw new RuntimeException("Heap overflow."); else { last++; - array[last] = e; + array[last] = i; up(); } } - public E removeMin() throws RuntimeException { + public int removeMin() throws RuntimeException { if (isEmpty()) throw new RuntimeException("Heap is empty."); else { - E min = min(); + int min = min(); array[1] = array[last]; last--; sinkWithIndex(1); @@ -69,15 +80,8 @@ public class Heap> { } } - private void sink(E e) { - int index = -1; - //finde E - for (int i = 0; i < array.length; i++) { - if (array[i].equals(e)) { - index = i; - } - } - sinkWithIndex(index); + private void sink(int s) { + sinkWithIndex(indexOf(s)); } private void sinkWithIndex(int index) { @@ -90,7 +94,6 @@ public class Heap> { } if (compare(array[index], array[child]) <= 0) break; - System.out.println(index + " | " + child); swap(index, child); index = child; } @@ -109,9 +112,9 @@ public class Heap> { private void swap(int i, int j) { swapCount++; - Object temp = array[i]; + int tmp = array[i]; array[i] = array[j]; - array[j] = temp; + array[j] = tmp; } private int findMin(int leftChild, int rightChild) { @@ -121,6 +124,17 @@ public class Heap> { return rightChild; } + private int indexOf(int j) { + int index = -1; + for (int i = 0; i < array.length; i++) { + if (array[i] == j) { + index = i; + } + } + return index; + } + + @Override public String toString() { return Arrays.toString(array); } diff --git a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/HeapMain.java b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/HeapMain.java index 806b785..c5dea80 100644 --- a/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/HeapMain.java +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/HeapMain.java @@ -13,9 +13,11 @@ public class HeapMain { System.out.println("geblockt"); for(int i : n) { - Integer[] array = new Integer[i]; + int[] array = new int[i]; + int y = 0; for (int j = i; j > 0; j--) { - array[j-1] = j; + array[y] = j; + y++; } Heap heap = new Heap(array); System.out.println(i + " : " + heap.getSwapCount()); @@ -24,7 +26,7 @@ public class HeapMain { } public static Heap getHeap(int n) { - Heap heap = new Heap<>(n); + Heap heap = new Heap(n+1); for (int i = n; i > 0; i--) { heap.insert(i); 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 a617ad9..17d9cf2 100644 --- a/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java +++ b/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java @@ -15,7 +15,7 @@ class HeapTest { void iterativ() { 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]); + assertEquals(res[i], HeapMain.getHeap(n[i]).getSwapCount()); } } @@ -24,12 +24,12 @@ class HeapTest { void geblockt() { int[] res = new int[]{}; for (int i = 0; i < 15; i++) { - Integer[] array = new Integer[i]; + int[] array = new int[i]; for (int j = i; j > 0; j--) { array[j-1] = j; } - Heap heap = new Heap<>(array); - assertEquals(heap.getSwapCount(), res[i]); + Heap heap = new Heap(array); + assertEquals(res[i], heap.getSwapCount()); } } } \ No newline at end of file