From fdc84e38766d9ed6f6ce0c93d424d105d58590ff Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Sun, 9 Dec 2018 19:40:58 +0100 Subject: [PATCH] A4: Implementierung Heap ~ iterativ fertig --- algodat.iml | 21 ---- .../algodat/praktikum/blatt4/Heap.java | 115 ++++++++++++++++++ .../algodat/praktikum/blatt4/HeapMain.java | 34 ++++++ .../studium/algodat/praktikum/HeapTest.java | 18 +++ 4 files changed, 167 insertions(+), 21 deletions(-) delete mode 100644 algodat.iml create mode 100644 src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/Heap.java create mode 100644 src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/HeapMain.java create mode 100644 src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java diff --git a/algodat.iml b/algodat.iml deleted file mode 100644 index 39e64ba..0000000 --- a/algodat.iml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file 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 new file mode 100644 index 0000000..e5e1976 --- /dev/null +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/Heap.java @@ -0,0 +1,115 @@ +package xyz.joethei.studium.algodat.praktikum.blatt4; + +import java.util.Arrays; + +public class Heap> { + + private Object[] array; + private int last = 0; + private int capacity; + private int swapCount = 0; + + + public Heap(int capacity) { + array = new Object[capacity + 1]; + this.capacity = capacity; + } + + public Heap(Object[] array) { + this.capacity = array.length + 1; + this.array = array; + this.last = array.length-1; + down(); + } + + public int size() { + return last; + } + + public boolean isEmpty() { + return size() == 0; + } + + public int getSwapCount() { + return swapCount; + } + + public E min() throws RuntimeException { + if (isEmpty()) + throw new RuntimeException("The heap is empty."); + else + return (E) array[1]; + } + + private int compare(Object x, Object y) { + return ((E) x).compareTo((E) y); + } + + public void insert(E e) throws RuntimeException { + if (size() == capacity) + throw new RuntimeException("Heap overflow."); + else { + last++; + array[last] = e; + up(); + } + } + + public E removeMin() throws RuntimeException { + if (isEmpty()) + throw new RuntimeException("Heap is empty."); + else { + E min = min(); + array[1] = array[last]; + last--; + down(); + return min; + } + } + + private void down() { + int index = 1; + while (true) { + int child = index * 2; + if (child > size()) + break; + if (child + 1 <= size()) { + child = findMin(child, child + 1); + } + if (compare(array[index], array[child]) <= 0) + break; + swap(index, child); + index = child; + } + } + + + private void up() { + int index = size(); + while (index > 1) { + int parent = index / 2; + if (compare(array[index], array[parent]) >= 0) + break; + swap(index, parent); + index = parent; + } + } + + private void swap(int i, int j) { + swapCount++; + Object temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + + private int findMin(int leftChild, int rightChild) { + if (compare(array[leftChild], array[rightChild]) <= 0) + return leftChild; + else + return rightChild; + } + + public String toString() { + return Arrays.toString(array); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..806b785 --- /dev/null +++ b/src/main/java/xyz/joethei/studium/algodat/praktikum/blatt4/HeapMain.java @@ -0,0 +1,34 @@ +package xyz.joethei.studium.algodat.praktikum.blatt4; + +public class HeapMain { + + public static void main(String[] args) { + int[] n = new int[]{1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535}; + + System.out.println("iterativ"); + for(int i : n) { + Heap heap = getHeap(i); + System.out.println(i + " : " + heap.getSwapCount()); + } + + System.out.println("geblockt"); + for(int i : n) { + Integer[] array = new Integer[i]; + for (int j = i; j > 0; j--) { + array[j-1] = j; + } + Heap heap = new Heap(array); + System.out.println(i + " : " + heap.getSwapCount()); + } + + } + + public static Heap getHeap(int n) { + Heap heap = new Heap<>(n); + + for (int i = n; i > 0; i--) { + heap.insert(i); + } + return heap; + } +} \ No newline at end of file diff --git a/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java b/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java new file mode 100644 index 0000000..710da5b --- /dev/null +++ b/src/test/java/xyz/joethei/studium/algodat/praktikum/HeapTest.java @@ -0,0 +1,18 @@ +package xyz.joethei.studium.algodat.praktikum; + +import org.junit.jupiter.api.Test; +import xyz.joethei.studium.algodat.praktikum.blatt4.HeapMain; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class HeapTest { + + @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]); + } + } +} \ No newline at end of file