A4: Anfang geblockter Heap
This commit is contained in:
parent
fdc84e3876
commit
01b54e90e4
|
@ -19,7 +19,9 @@ public class Heap<E extends Comparable<E>> {
|
|||
this.capacity = array.length + 1;
|
||||
this.array = array;
|
||||
this.last = array.length - 1;
|
||||
down();
|
||||
for (int i = last; i >= 0; i--) {
|
||||
down((E) array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
int index = 1;
|
||||
while (true) {
|
||||
|
@ -83,7 +98,6 @@ public class Heap<E extends Comparable<E>> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private void up() {
|
||||
int index = size();
|
||||
while (index > 1) {
|
||||
|
|
|
@ -1,18 +1,33 @@
|
|||
package xyz.joethei.studium.algodat.praktikum;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import xyz.joethei.studium.algodat.praktikum.blatt4.Heap;
|
||||
import xyz.joethei.studium.algodat.praktikum.blatt4.HeapMain;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class HeapTest {
|
||||
|
||||
private int[] n = new int[]{1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535};
|
||||
|
||||
@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]);
|
||||
}
|
||||
}
|
||||
|
||||
@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]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue