A4: Anfang geblockter Heap

This commit is contained in:
Johannes Theiner 2018-12-10 11:20:18 +01:00
parent fdc84e3876
commit 01b54e90e4
2 changed files with 33 additions and 4 deletions

View File

@ -18,8 +18,10 @@ public class Heap<E extends Comparable<E>> {
public Heap(Object[] array) { public Heap(Object[] 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;
down(); for (int i = last; i >= 0; i--) {
down((E) array[i]);
}
} }
public int size() { public int size() {
@ -67,6 +69,19 @@ public class Heap<E extends Comparable<E>> {
} }
} }
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() { private void down() {
int index = 1; int index = 1;
while (true) { while (true) {
@ -83,7 +98,6 @@ public class Heap<E extends Comparable<E>> {
} }
} }
private void up() { private void up() {
int index = size(); int index = size();
while (index > 1) { while (index > 1) {

View File

@ -1,18 +1,33 @@
package xyz.joethei.studium.algodat.praktikum; package xyz.joethei.studium.algodat.praktikum;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import xyz.joethei.studium.algodat.praktikum.blatt4.Heap;
import xyz.joethei.studium.algodat.praktikum.blatt4.HeapMain; import xyz.joethei.studium.algodat.praktikum.blatt4.HeapMain;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
class HeapTest { class HeapTest {
private int[] n = new int[]{1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535};
@Test @Test
void iterativ() { 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}; 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(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<Integer> heap = new Heap<>(array);
assertEquals(heap.getSwapCount(), res[i]);
}
}
} }