A4: blockiert funktioniert noch nicht richtig

This commit is contained in:
Johannes Theiner 2018-12-10 22:47:55 +01:00
parent 8b7fdcf9d1
commit 2fb72180f9
3 changed files with 49 additions and 33 deletions

View File

@ -2,28 +2,39 @@ package xyz.joethei.studium.algodat.praktikum.blatt4;
import java.util.Arrays; import java.util.Arrays;
public class Heap<E extends Comparable<E>> { public class Heap {
private Object[] array; private int[] array;
private int last = 0; private int last = 0;
private int capacity; private int capacity;
private int swapCount = 0; private int swapCount = 0;
public Heap(int capacity) { public Heap(int capacity) {
array = new Object[capacity + 1]; array = new int[capacity + 1];
this.capacity = capacity; this.capacity = capacity;
} }
public Heap(Object[] array) { public Heap(int[] array) {
this.capacity = array.length + 1; this.capacity = array.length + 1;
this.array = array; this.array = array;
this.last = array.length-1; this.last = array.length-1;
for (int i = last; i >= 0; i--) { for (int i = indexOf(findLastWithChild()); i >= 0; i--) {
sink((E) array[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() { public int size() {
return last; return last;
} }
@ -36,32 +47,32 @@ public class Heap<E extends Comparable<E>> {
return swapCount; return swapCount;
} }
public E min() throws RuntimeException { public int min() throws RuntimeException {
if (isEmpty()) if (isEmpty())
throw new RuntimeException("The heap is empty."); throw new RuntimeException("The heap is empty.");
else else
return (E) array[1]; return array[1];
} }
private int compare(Object x, Object y) { private int compare(int x, int y) {
return ((E) x).compareTo((E) y); return Integer.compare(x, y);
} }
public void insert(E e) throws RuntimeException { public void insert(int i) throws RuntimeException {
if (size() == capacity) if (size() == capacity)
throw new RuntimeException("Heap overflow."); throw new RuntimeException("Heap overflow.");
else { else {
last++; last++;
array[last] = e; array[last] = i;
up(); up();
} }
} }
public E removeMin() throws RuntimeException { public int removeMin() throws RuntimeException {
if (isEmpty()) if (isEmpty())
throw new RuntimeException("Heap is empty."); throw new RuntimeException("Heap is empty.");
else { else {
E min = min(); int min = min();
array[1] = array[last]; array[1] = array[last];
last--; last--;
sinkWithIndex(1); sinkWithIndex(1);
@ -69,15 +80,8 @@ public class Heap<E extends Comparable<E>> {
} }
} }
private void sink(E e) { private void sink(int s) {
int index = -1; sinkWithIndex(indexOf(s));
//finde E
for (int i = 0; i < array.length; i++) {
if (array[i].equals(e)) {
index = i;
}
}
sinkWithIndex(index);
} }
private void sinkWithIndex(int index) { private void sinkWithIndex(int index) {
@ -90,7 +94,6 @@ public class Heap<E extends Comparable<E>> {
} }
if (compare(array[index], array[child]) <= 0) if (compare(array[index], array[child]) <= 0)
break; break;
System.out.println(index + " | " + child);
swap(index, child); swap(index, child);
index = child; index = child;
} }
@ -109,9 +112,9 @@ public class Heap<E extends Comparable<E>> {
private void swap(int i, int j) { private void swap(int i, int j) {
swapCount++; swapCount++;
Object temp = array[i]; int tmp = array[i];
array[i] = array[j]; array[i] = array[j];
array[j] = temp; array[j] = tmp;
} }
private int findMin(int leftChild, int rightChild) { private int findMin(int leftChild, int rightChild) {
@ -121,6 +124,17 @@ public class Heap<E extends Comparable<E>> {
return rightChild; 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() { public String toString() {
return Arrays.toString(array); return Arrays.toString(array);
} }

View File

@ -13,9 +13,11 @@ public class HeapMain {
System.out.println("geblockt"); System.out.println("geblockt");
for(int i : n) { for(int i : n) {
Integer[] array = new Integer[i]; int[] array = new int[i];
int y = 0;
for (int j = i; j > 0; j--) { for (int j = i; j > 0; j--) {
array[j-1] = j; array[y] = j;
y++;
} }
Heap heap = new Heap(array); Heap heap = new Heap(array);
System.out.println(i + " : " + heap.getSwapCount()); System.out.println(i + " : " + heap.getSwapCount());
@ -24,7 +26,7 @@ public class HeapMain {
} }
public static Heap getHeap(int n) { public static Heap getHeap(int n) {
Heap<Integer> heap = new Heap<>(n); Heap heap = new Heap(n+1);
for (int i = n; i > 0; i--) { for (int i = n; i > 0; i--) {
heap.insert(i); heap.insert(i);

View File

@ -15,7 +15,7 @@ class HeapTest {
void iterativ() { void iterativ() {
int[] res = new int[]{0, 2, 10, 34, 98, 258, 642, 1538, 3586, 8194, 18434, 40962, 90114, 196610, 425986, 917506}; 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++) { 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() { void geblockt() {
int[] res = new int[]{}; int[] res = new int[]{};
for (int i = 0; i < 15; i++) { for (int i = 0; i < 15; i++) {
Integer[] array = new Integer[i]; int[] array = new int[i];
for (int j = i; j > 0; j--) { for (int j = i; j > 0; j--) {
array[j-1] = j; array[j-1] = j;
} }
Heap<Integer> heap = new Heap<>(array); Heap heap = new Heap(array);
assertEquals(heap.getSwapCount(), res[i]); assertEquals(res[i], heap.getSwapCount());
} }
} }
} }