parent
3d0bd7a924
commit
fdc84e3876
21
algodat.iml
21
algodat.iml
|
@ -1,21 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.3.1" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.0.0" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.1.1" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.3.1" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.3.1" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.3.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
|
||||
</component>
|
||||
</module>
|
|
@ -0,0 +1,115 @@
|
|||
package xyz.joethei.studium.algodat.praktikum.blatt4;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Heap<E extends Comparable<E>> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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<Integer> heap = new Heap<>(n);
|
||||
|
||||
for (int i = n; i > 0; i--) {
|
||||
heap.insert(i);
|
||||
}
|
||||
return heap;
|
||||
}
|
||||
}
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue