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;
public class Heap<E extends Comparable<E>> {
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<E extends Comparable<E>> {
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<E extends Comparable<E>> {
}
}
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<E extends Comparable<E>> {
}
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<E extends Comparable<E>> {
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<E extends Comparable<E>> {
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);
}

View File

@ -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<Integer> heap = new Heap<>(n);
Heap heap = new Heap(n+1);
for (int i = n; i > 0; i--) {
heap.insert(i);

View File

@ -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<Integer> heap = new Heap<>(array);
assertEquals(heap.getSwapCount(), res[i]);
Heap heap = new Heap(array);
assertEquals(res[i], heap.getSwapCount());
}
}
}