a
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package UEB08;
|
||||
class BBaum<T extends Comparable<T>>
|
||||
{
|
||||
public BKnoten<T> wurzel;
|
||||
|
||||
public BBaum(BKnoten<T> wurzel)
|
||||
{
|
||||
assert(wurzel != null);
|
||||
|
||||
this.wurzel = wurzel;
|
||||
}
|
||||
|
||||
// Wrapper-Methode
|
||||
public void traversieren()
|
||||
{
|
||||
traversieren(wurzel);
|
||||
}
|
||||
|
||||
// Eigentliche Implementierung
|
||||
private void traversieren(BKnoten<T> knoten)
|
||||
{
|
||||
assert(knoten != null);
|
||||
int i = 0;
|
||||
if(knoten.kinder.length!=0){
|
||||
//System.out.println(knoten.kinder.length +" "+ knoten.elemente.length);
|
||||
for(i = 0; i < knoten.elemente.length;i++){
|
||||
if (knoten.kinder[i]!=null){
|
||||
traversieren(knoten.kinder[i]);
|
||||
}
|
||||
System.out.println(knoten.elemente[i]);
|
||||
}
|
||||
if (knoten.kinder[i]!=null){
|
||||
traversieren(knoten.kinder[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper-Methode
|
||||
public boolean suchen(final T daten)
|
||||
{
|
||||
assert(daten != null);
|
||||
|
||||
return suchen(daten, wurzel);
|
||||
}
|
||||
|
||||
// Eigentliche Implementierung
|
||||
private boolean suchen(final T daten, BKnoten<T> knoten)
|
||||
{
|
||||
for(int i=0;i<knoten.elemente.length;i++){
|
||||
if(knoten.elemente[i].equals(daten)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (int i = 0;i<knoten.kinder.length;i++){
|
||||
if(knoten.kinder[i]!= null){
|
||||
if(suchen(daten,knoten.kinder[i])){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package UEB08;
|
||||
public class BBaumTest
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// B-Baum erzeugen
|
||||
BBaum<Integer> baum = new BBaum<Integer>(
|
||||
new BKnoten<Integer>(new Integer[]{ 19, 34 }, new BKnoten[]{
|
||||
new BKnoten<Integer>(new Integer[]{ 4, 7, 10, 14 }, new BKnoten[]{
|
||||
new BKnoten<Integer>(new Integer[]{ 1, 2, 3 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 5, 6 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 8, 9 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 11, 12, 13 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 15, 16, 17, 18 } )
|
||||
} ),
|
||||
new BKnoten<Integer>(new Integer[]{ 22, 27, 30 }, new BKnoten[]{
|
||||
new BKnoten<Integer>(new Integer[]{ 20, 21 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 23, 24, 25, 26 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 28, 29 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 31, 32, 33 } )
|
||||
} ),
|
||||
new BKnoten<Integer>(new Integer[]{ 39, 42 }, new BKnoten[]{
|
||||
new BKnoten<Integer>(new Integer[]{ 35, 36, 37, 38 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 40, 41 } ),
|
||||
new BKnoten<Integer>(new Integer[]{ 43, 44, 45 } )
|
||||
} )
|
||||
} )
|
||||
);
|
||||
|
||||
// B-Baum traversieren
|
||||
System.out.println("Traversieren:");
|
||||
|
||||
baum.traversieren();
|
||||
|
||||
// Nach Elementen suchen
|
||||
System.out.println("\n\nSuchen:");
|
||||
|
||||
boolean suchen = true;
|
||||
for (int a = -50; a < 100; a++)
|
||||
suchen &= (baum.suchen(a) == ((a >=1 ) && (a <= 45)));
|
||||
|
||||
System.out.println(suchen ? "Ok" : "Fehler!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package UEB08;
|
||||
public class BKnoten<T>
|
||||
{
|
||||
public T[] elemente;
|
||||
public BKnoten<T>[] kinder;
|
||||
|
||||
// F�r Bl�tter
|
||||
public BKnoten(final T[] elemente)
|
||||
{
|
||||
assert(elemente!=null);
|
||||
|
||||
this.elemente = elemente;
|
||||
this.kinder = (BKnoten<T>[])new BKnoten[elemente.length + 1];
|
||||
}
|
||||
|
||||
// F�r innere Knoten
|
||||
public BKnoten(final T[] elemente, final BKnoten<T>[] kinder)
|
||||
{
|
||||
assert(elemente != null);
|
||||
assert(kinder != null);
|
||||
assert(elemente.length+1 == kinder.length);
|
||||
|
||||
this.elemente = elemente;
|
||||
this.kinder = kinder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package UEB10;
|
||||
|
||||
public class BasicSort
|
||||
{
|
||||
// Selectionsort:
|
||||
// Sortiere das Teilfeld von array beginnend mit Index links bis einschlie�lich Index rechts
|
||||
public static void selectionsort(int[] array, final int links, final int rechts)
|
||||
{
|
||||
for (int i = links; i < rechts; i++)
|
||||
{
|
||||
// Kleinstes Element im unsortierten Teil finden
|
||||
int min = i;
|
||||
|
||||
for (int j = i + 1; j <= rechts; j++)
|
||||
if (array[min] > array[j])
|
||||
min = j;
|
||||
|
||||
// Elemente tauschen
|
||||
int temp = array[min];
|
||||
array[min] = array[i];
|
||||
array[i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Insertionsort:
|
||||
// Sortiere das Teilfeld von array beginnend mit Index links bis einschlie�lich Index rechts
|
||||
public static void insertionsort(int[] array, final int links, final int rechts)
|
||||
{
|
||||
// TODO: Praktikum 9
|
||||
}
|
||||
|
||||
// Bubblesort:
|
||||
// Sortiere das Teilfeld von array beginnend mit Index links bis einschlie�lich Index rechts
|
||||
public static void bubblesort(int[] array, final int links, final int rechts)
|
||||
{
|
||||
// TODO: Praktikum 9
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package UEB10;
|
||||
public class HeapSort {
|
||||
// Versickere das Element mit Index pos in dem Teilfeld von Index links bis einschlie�lich Index rechts
|
||||
public static void versickere(int[] array, final int links, int pos, final int rechts) {
|
||||
int groesster = pos;
|
||||
int l = 2 * (pos - links) + 1 + links;
|
||||
int r = 2 * (pos - links) + 2 + links;
|
||||
|
||||
if (l <= rechts && array[l] > array[groesster])
|
||||
groesster = l;
|
||||
|
||||
if (r <= rechts && array[r] > array[groesster])
|
||||
groesster = r;
|
||||
|
||||
if (groesster != pos) {
|
||||
int swap = array[pos];
|
||||
array[pos] = array[groesster];
|
||||
array[groesster] = swap;
|
||||
|
||||
versickere(array, links, groesster, rechts);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void heapsort(int[] array, final int links, final int rechts) {
|
||||
int n = rechts - links + 1;
|
||||
|
||||
// Heap aufbauen
|
||||
for (int i = links + n / 2 - 1; i >= links; i--)
|
||||
versickere(array, links, i, rechts);
|
||||
|
||||
// Eines nach dem anderen ein Element aus dem Heap extrahieren
|
||||
for (int i = rechts; i >= links; i--) {
|
||||
// Aktuelle Wurzel ans Ende verschieben
|
||||
int temp = array[links];
|
||||
array[links] = array[i];
|
||||
array[i] = temp;
|
||||
|
||||
// max heapify auf dem reduzierten Heap aufrufen
|
||||
versickere(array, links, links, i - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package UEB10;
|
||||
public class Main
|
||||
{
|
||||
public static final int SELECTIONSORT = 0;
|
||||
public static final int INSERTIONSORT = 1;
|
||||
public static final int BUBBLESORT = 2;
|
||||
public static final int QUICKSORT = 3;
|
||||
public static final int HEAPSORT = 4;
|
||||
public static final int MERGESORT = 5;
|
||||
|
||||
public static final int ANZAHLALGORITHMEN = 6;
|
||||
public static final int N = 10000;
|
||||
|
||||
public static final String[] sortNames =
|
||||
{
|
||||
"Selection Sort",
|
||||
"Insertion Sort",
|
||||
"Bubblesort",
|
||||
"Quicksort",
|
||||
"Heapsort",
|
||||
"Mergesort"
|
||||
};
|
||||
|
||||
// Gibt ein Array mit n Elementen und zuf�lligen Zahlen zur�ck
|
||||
public static int[] getRandomArray(final int n)
|
||||
{
|
||||
int[] array = new int[n];
|
||||
|
||||
for (int a = 0; a < n ; a++)
|
||||
array[a] = (int)(Math.random()*10*n);
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
// Pr�ft, ob ein Array korrekt sortiert ist
|
||||
public static boolean checkArray(int[] array)
|
||||
{
|
||||
for (int a = 0; a < array.length-1; a++)
|
||||
if (array[a] > array[a+1])
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Pr�ft einen Algorithmus, und gibt die Laufzeiten aus
|
||||
public static void checkSortAlgorithmus(final int algorithmus)
|
||||
{
|
||||
StopUhr stopUhr = new StopUhr();
|
||||
|
||||
boolean isCorrect = true;
|
||||
long minTime = 0;
|
||||
long maxTime = 0;
|
||||
long sumTime = 0;
|
||||
|
||||
// Wir testen mit 100 zuf�llig erzeugten Feldern
|
||||
for (int a = 0; a < 100; a++)
|
||||
{
|
||||
int[] array = getRandomArray(N);
|
||||
|
||||
// Zeitmessung beginnen
|
||||
stopUhr.start();
|
||||
|
||||
// Array sortieren
|
||||
switch (algorithmus)
|
||||
{
|
||||
case INSERTIONSORT:
|
||||
BasicSort.insertionsort(array, 0, array.length - 1);
|
||||
break;
|
||||
|
||||
case SELECTIONSORT:
|
||||
BasicSort.selectionsort(array, 0, array.length - 1);
|
||||
break;
|
||||
|
||||
case BUBBLESORT:
|
||||
BasicSort.bubblesort(array, 0, array.length - 1);
|
||||
break;
|
||||
|
||||
case QUICKSORT:
|
||||
QuickSort.quicksort(array, 0, array.length - 1);
|
||||
break;
|
||||
|
||||
case HEAPSORT:
|
||||
HeapSort.heapsort(array, 0, array.length - 1);
|
||||
break;
|
||||
|
||||
case MERGESORT:
|
||||
MergeSort.mergesort(array, 0, array.length - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
// Zeitmessung stoppen
|
||||
stopUhr.stop();
|
||||
|
||||
// Ergebnis auswerten
|
||||
if (!(isCorrect &= checkArray(array)))
|
||||
break;
|
||||
|
||||
if ((a==0) || (stopUhr.getDuration()<minTime))
|
||||
minTime = stopUhr.getDuration();
|
||||
|
||||
if (stopUhr.getDuration()>maxTime)
|
||||
maxTime = stopUhr.getDuration();
|
||||
|
||||
sumTime += stopUhr.getDuration();
|
||||
}
|
||||
|
||||
// Ausgabe
|
||||
if (isCorrect)
|
||||
{
|
||||
System.out.format("%16s %10.4f %10.4f %10.4f\n",
|
||||
sortNames[algorithmus], minTime / 1000000.0, sumTime / 100000000.0, maxTime / 1000000.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.format("%16s %10s\n", sortNames[algorithmus], "-");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println("Arraygr��e n = " + N + "\n");
|
||||
System.out.println(" Algorithmus Laufzeit (ms)");
|
||||
System.out.println(" minimal durchschnittlich maximal");
|
||||
|
||||
for (int algorithmus = 0; algorithmus < ANZAHLALGORITHMEN; algorithmus++)
|
||||
checkSortAlgorithmus(algorithmus);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
|
||||
package UEB10;
|
||||
public class MergeSort
|
||||
{
|
||||
public static void mergesort(int[] array, final int links, final int rechts)
|
||||
{
|
||||
int[] hilfsarray = new int[array.length];
|
||||
|
||||
mergesortBU(array, hilfsarray, links, rechts);
|
||||
}
|
||||
|
||||
// Top-Down-Mergesort entsprechend der Vorlesung
|
||||
public static void mergesortTD(int[] array, int[] hilfsarray, final int links, final int rechts)
|
||||
{
|
||||
if (links < rechts)
|
||||
{
|
||||
// mindestens 2 Elemente
|
||||
int mitte = (links + rechts)/2;
|
||||
// Feld in der Mitte teilen und rekursive Aufrufe f�r Teilfelder
|
||||
mergesortTD(array, hilfsarray, links, mitte);
|
||||
mergesortTD(array, hilfsarray, mitte+1, rechts);
|
||||
// Sortierte Teilfelder mischen
|
||||
merge(array, hilfsarray, links, mitte, rechts);
|
||||
}
|
||||
}
|
||||
|
||||
// Bottom-Up-Mergesort
|
||||
public static void mergesortBU(int[] array, int[] hilfsarray, final int links, final int rechts)
|
||||
{
|
||||
int n = rechts - links + 1;
|
||||
for (int len = 1; len < n; len *= 2) {
|
||||
for (int lo = links; lo < rechts - len + 1; lo += len+len) {
|
||||
int mid = lo+len-1;
|
||||
int hi = Math.min(lo+len+len-1, rechts);
|
||||
merge(array, hilfsarray, lo, mid, hi);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Mischen der Teilfelder array[links]...array[mitte] und array[mitte+1]...array[rechts]
|
||||
private static void merge(int[] array, int[] hilfsarray, final int links, final int mitte, final int rechts)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
i = links;
|
||||
j = mitte+1;
|
||||
|
||||
// Mischen in Hilfsarray
|
||||
for (int k=links; k <= rechts; k++)
|
||||
{
|
||||
if (i>mitte)
|
||||
hilfsarray[k] = array[j++]; // 1. Teilfeld schon zu Ende
|
||||
else if (j>rechts)
|
||||
hilfsarray[k] = array[i++]; // 2. Zeilfeld schon zu Ende
|
||||
else if(array[i] < array[j])
|
||||
hilfsarray[k] = array[i++]; // Element aus 1. Teilfeld �bernehmen
|
||||
else
|
||||
hilfsarray[k] = array[j++]; // Element aus 2. teilfeld �bernehmen
|
||||
}
|
||||
|
||||
|
||||
for (int k = links; k <= rechts; k++)
|
||||
{
|
||||
array[k] = hilfsarray[k];
|
||||
}
|
||||
}
|
||||
|
||||
// Alternative Metdode zum Mischen
|
||||
private static void merge2(int[] array, int[] hilfsarray, final int links, final int mitte, final int rechts)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
// 1. Teilfeld kopieren
|
||||
for (i = mitte+1; i > links; i--)
|
||||
hilfsarray[i-1] = array[i-1];
|
||||
// 2. Teilfeld in umgekehrter Reihenfolge kopieren
|
||||
for (j = mitte; j < rechts; j++)
|
||||
hilfsarray[rechts+mitte-j] = array[j+1];
|
||||
|
||||
// Zeiger i und j gegeneinander laufen lassen
|
||||
for (int k = links; k <= rechts; k++ )
|
||||
if (hilfsarray[j] < hilfsarray[i])
|
||||
array[k] = hilfsarray[j--];
|
||||
else
|
||||
array[k] = hilfsarray[i++];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package UEB10;
|
||||
public class QuickSort
|
||||
{
|
||||
public static void quicksort(int[] array, final int links, final int rechts)
|
||||
{
|
||||
// TODO: Praktikum 9
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package UEB10;
|
||||
public class StopUhr
|
||||
{
|
||||
private long startTime, stopTime;
|
||||
|
||||
public void start()
|
||||
{
|
||||
startTime = System.nanoTime();
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
stopTime = System.nanoTime();
|
||||
}
|
||||
|
||||
public long getDuration()
|
||||
{
|
||||
return stopTime - startTime;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package UEB11;
|
||||
public class HashTable
|
||||
{
|
||||
private static final int START_TABELLENGROESSE = 10; // Standardgr��e
|
||||
|
||||
private IHashable[] table; // Array f�r Hash-Objekte
|
||||
private int currentSize; // Die Anzahl der belegten Slots
|
||||
|
||||
public HashTable()
|
||||
{
|
||||
this(START_TABELLENGROESSE);
|
||||
}
|
||||
|
||||
// Konstruktor mit expliziter Gr��enangabe
|
||||
public HashTable(final int groesse)
|
||||
{
|
||||
assert(groesse > 0);
|
||||
|
||||
table = new IHashable[groesse];
|
||||
clear();
|
||||
}
|
||||
|
||||
// F�gt ein Objekt in die Hashtabelle ein.
|
||||
// Falls der Eintrag bereits existiert passiert NICHTS!
|
||||
public void insert(final IHashable o)
|
||||
{
|
||||
assert(o != null);
|
||||
|
||||
final int currentPos = findePosition(o);
|
||||
|
||||
if (table[currentPos] == null)
|
||||
{
|
||||
table[currentPos] = o;
|
||||
|
||||
if (++currentSize > table.length / 2)
|
||||
rehash();
|
||||
}
|
||||
}
|
||||
|
||||
// Erweiterung der Hashtabelle
|
||||
private void rehash()
|
||||
{
|
||||
// Debug-Ausgabe
|
||||
System.out.println("rehash(" + table.length+ ")");
|
||||
|
||||
// Kopie der alten Hashtabelle anlegen
|
||||
IHashable[] oldTable = table;
|
||||
|
||||
// Erzeuge table mit mindestens der doppelten Gr��e
|
||||
// (f�hrt zu O(log n) Vergr��erungs-Operationen f�r n Objekte)
|
||||
table = new IHashable[nextPrime(2 * oldTable.length)];
|
||||
|
||||
currentSize = 0;
|
||||
|
||||
// Einsortieren der Elemente ins neue Array
|
||||
for (int a = 0; a < oldTable.length; a++)
|
||||
if (oldTable[a] != null)
|
||||
insert(oldTable[a]);
|
||||
}
|
||||
|
||||
// Quadratisches Sondieren
|
||||
public int findePosition(IHashable o)
|
||||
{
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Findet ein Objekt in der Hashtabelle
|
||||
public IHashable find(IHashable o)
|
||||
{
|
||||
return table[findePosition(o)];
|
||||
}
|
||||
|
||||
// L�schen der gesamten Hashtabelle
|
||||
public void clear()
|
||||
{
|
||||
for (int a = 0; a < table.length; a++)
|
||||
table[a] = null;
|
||||
|
||||
currentSize = 0;
|
||||
}
|
||||
|
||||
// Test-Methode
|
||||
public IHashable getEntry(final int pos)
|
||||
{
|
||||
return table[pos];
|
||||
}
|
||||
|
||||
// Test-Methode
|
||||
public int getSize()
|
||||
{
|
||||
return table.length;
|
||||
}
|
||||
|
||||
// Private Funktion, die zu einer vorgegebenen Zahl n die n�chsth�here Primzahl findet
|
||||
private static int nextPrime(int n)
|
||||
{
|
||||
assert(n >= 0);
|
||||
|
||||
// N�chste ungerade Zahl
|
||||
n |= 1;
|
||||
|
||||
while (!isPrime(n))
|
||||
n += 2;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
// Testet, ob eine Zahl eine Primzahl ist (einfache, aber ineffiziente Implementierung)
|
||||
private static boolean isPrime(final int n)
|
||||
{
|
||||
if ((n == 2) || (n == 3))
|
||||
return true;
|
||||
|
||||
if ((n == 1) || ((n & 1) == 0))
|
||||
return false;
|
||||
|
||||
for (int a = 3; a * a <= n; a += 2)
|
||||
if ((n % a) == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package UEB11;
|
||||
import java.util.Random;
|
||||
|
||||
public class HashTableTest
|
||||
{
|
||||
private static String[][] Profs =
|
||||
{
|
||||
{"Bab", "8305"},
|
||||
{"B�ckmann", "6728"},
|
||||
{"Cleven", "6732"},
|
||||
{"Ecke-Sch�th", "6784"},
|
||||
{"Engels", "6777"},
|
||||
{"Friedrich", "6796"},
|
||||
{"Haake", "6766"},
|
||||
{"Haas", "6719"},
|
||||
{"Hagen", "6782"},
|
||||
{"Hamburg", "6708"},
|
||||
{"Harrer", "6748"},
|
||||
{"Hesseler", "6732"},
|
||||
{"Hessel-von Molo", "6779"},
|
||||
{"Hirsch", "6835"},
|
||||
{"Hoffmann", "0"},
|
||||
{"J�rges", "6741"},
|
||||
{"Kamsties", "6816"},
|
||||
{"Kienle", "9777"},
|
||||
{"K�nisgmann", "6776"},
|
||||
{"Kuhnt", "8935"},
|
||||
{"Kukuk", "6715"},
|
||||
{"Kunau", "8906"},
|
||||
{"K�nemund", "6764"},
|
||||
{"Lauenroth", "0"},
|
||||
{"Lenze", "6729"},
|
||||
{"Lu", "6758"},
|
||||
{"Preis", "6756"},
|
||||
{"Recker", "6783"},
|
||||
{"Reimann", "6786"},
|
||||
{"Rettinger", "6797"},
|
||||
{"R�hrig", "8100"},
|
||||
{"Saatz", "6765"},
|
||||
{"Sachweh", "6760"},
|
||||
{"Sch�nberg", "8919"},
|
||||
{"Schuster", "8903"},
|
||||
{"Stark", "6747"},
|
||||
{"Teschler-Nunkesser", "6785"},
|
||||
{"Vollmer", "6737"},
|
||||
{"Wiesmann", "8918"},
|
||||
{"Wolff", "0"},
|
||||
{"Zeppenfeld", "0"}
|
||||
};
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
HashTable h = new HashTable(10);
|
||||
|
||||
System.out.println("Hashtablle mit " + Profs.length + " Objekten anlegen:");
|
||||
|
||||
// Professorendaten in Hashtabelle eintragen
|
||||
for (int a = 0; a < Profs.length; a++)
|
||||
h.insert(new Professor(Profs[a][0], Integer.parseInt(Profs[a][1])));
|
||||
|
||||
// Gespeicherte Professorendaten nacheinander aus der Tabelle
|
||||
// auslesen und den jeweils zugeh�rigen Namen ausgeben
|
||||
System.out.println("\nHash-Tabelle:");
|
||||
for (int a = 0; a < h.getSize(); a++)
|
||||
{
|
||||
final Professor p = (Professor)h.getEntry(a);
|
||||
if (p != null)
|
||||
System.out.println(a + " " + p.getName());
|
||||
}
|
||||
|
||||
System.out.println("\nZuordnung per Roundtrip �ber den Hashwert testen:");
|
||||
for (int a = 0; a < h.getSize(); a++)
|
||||
if ((Professor)h.getEntry(a) != null)
|
||||
System.out.println("" + a + " <---> " + h.findePosition(h.getEntry(a)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package UEB11;
|
||||
public interface IHashable
|
||||
{
|
||||
// Es wird gefordert, dass von den Objekten, die gespeichert werden sollen,
|
||||
// eine Methode zur Berechnung des Hash-Wertes angeboten wird.
|
||||
int hash(final int tableSize);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package UEB11;
|
||||
public class Professor implements IHashable
|
||||
{
|
||||
private String name;
|
||||
private long hausruf;
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public long getHausruf()
|
||||
{
|
||||
return hausruf;
|
||||
}
|
||||
|
||||
public Professor(String name, int Hausruf)
|
||||
{
|
||||
this.name = name;
|
||||
this.hausruf = Hausruf;
|
||||
}
|
||||
|
||||
// Hash-Wert
|
||||
public int hash(final int tableSize)
|
||||
{
|
||||
char c1 = name.charAt(0);
|
||||
char c2 = name.charAt(1);
|
||||
|
||||
int h1 = c1 - '0';
|
||||
int h2 = c2 - '0';
|
||||
int h = (h1 + h2) % tableSize;
|
||||
|
||||
// Debug-Ausgabe zu Testzwecken
|
||||
// System.out.println("Name:" + Name + " Hashwert:" + h);
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
// In diesem Beispiel nur Professor-Objekte zulassen
|
||||
if (!(o instanceof Professor))
|
||||
return false;
|
||||
|
||||
final Professor p = (Professor)o;
|
||||
|
||||
return ((p.name).equals(this.name) && (p.hausruf == this.hausruf));
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package UEB11;
|
||||
|
||||
import UEB10.Main;
|
||||
|
||||
public class StringSearch {
|
||||
public static void main(String[] args) {
|
||||
String[] words = {
|
||||
"aardvark", "abandon", "ability", "academy", "acorn",
|
||||
"action", "adventure", "aerial", "aerodynamic", "affection",
|
||||
"algorithm", "alpine", "amber", "analogy", "anchor",
|
||||
"antique", "aquarium", "arboreal", "architect", "arctic",
|
||||
"argument", "armor", "artistic", "asteroid", "athletic",
|
||||
"atmosphere", "auction", "audio", "authority", "automobile",
|
||||
"avalanche", "ballad", "banquet", "barrier", "bathtub",
|
||||
"beacon", "beholder", "biography", "blossom", "blueprint",
|
||||
"boulder", "broccoli", "brother", "buffalo", "butterfly",
|
||||
"cabbage", "calculator", "camouflage", "campfire", "candle",
|
||||
"carnation", "carousel", "cathedral", "cavalry", "celestial",
|
||||
"chameleon", "character", "cherry", "chestnut", "chimpanzee",
|
||||
"chronicle", "circus", "cobweb", "compass", "compendium",
|
||||
"conductor", "constellation", "contraption", "conundrum", "coral",
|
||||
"corkscrew", "crabapple", "crescendo", "cricket", "crimson",
|
||||
"cruise", "crystal", "cucumber", "curriculum", "cylinder",
|
||||
"daffodil", "daisy", "dandelion", "darkness", "daylight",
|
||||
"demonstration", "desert", "dictionary", "dinosaur", "direction",
|
||||
"dolphin", "dragonfly", "dream", "dune", "dwarf",
|
||||
"dynamite", "eagle", "earthquake", "echo", "eclipse",
|
||||
"education", "electricity", "elephant", "embassy", "engine",
|
||||
"escalator", "eucalyptus", "exploration", "falcon", "ferry",
|
||||
"firework", "flamingo", "flower", "fossil", "galaxy"
|
||||
};
|
||||
|
||||
|
||||
|
||||
String search = "aardvark" ;
|
||||
System.out.println(binaereSuche(words,search));
|
||||
}
|
||||
|
||||
static boolean binaereSuche(final String[] worte, final String begriff){
|
||||
int left = 0;
|
||||
int right = worte.length;
|
||||
int midIndex = worte.length/2;
|
||||
|
||||
int i=0;
|
||||
int goal = (int)Math.log(worte.length)+2;
|
||||
while(i <= goal){
|
||||
i++;
|
||||
System.out.println(worte[midIndex]);
|
||||
int comp = worte[midIndex].compareTo(begriff);
|
||||
System.out.println(comp);
|
||||
if (comp > 0) {
|
||||
right = midIndex-1;
|
||||
midIndex = (left+right)/2;
|
||||
} else if (comp < 0) {
|
||||
left = midIndex+1;
|
||||
midIndex = (left+right)/2;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package UEB12;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Graph
|
||||
{
|
||||
private static final int KNOTENZAHL = 7;
|
||||
private static boolean[] besucht;
|
||||
private Knoten[] knoten;
|
||||
private boolean[][] matrix = {
|
||||
{false, true, false, false, false, false, false},
|
||||
{false, false, true, false, false, false, false},
|
||||
{false, true, false, false, false, false, false},
|
||||
{false, false, false, false, false, false, false},
|
||||
{false, false, false, true, false, false, false},
|
||||
{false, true, true, false, false, false, false},
|
||||
{false, false, false, false, false, false, false},
|
||||
};
|
||||
|
||||
public Graph()
|
||||
{
|
||||
knoten = new Knoten[KNOTENZAHL];
|
||||
knoten[0] = new Knoten("A");
|
||||
knoten[1] = new Knoten("B");
|
||||
knoten[2] = new Knoten("C");
|
||||
knoten[3] = new Knoten("D");
|
||||
knoten[4] = new Knoten("E");
|
||||
knoten[5] = new Knoten("F");
|
||||
knoten[6] = new Knoten("G");
|
||||
}
|
||||
|
||||
private boolean isKante(int k1, int k2)
|
||||
{
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
// Tiefensuche
|
||||
private void rekDfs(final int k)
|
||||
{
|
||||
// TODO
|
||||
// Rekursive Tiefensuche mit Hilfe der Methode isKante implementieren
|
||||
|
||||
}
|
||||
|
||||
public void zusammenhangskomponenten()
|
||||
{
|
||||
besucht = new boolean [KNOTENZAHL];
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
||||
// TODO
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
new Graph().zusammenhangskomponenten();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package UEB12;
|
||||
|
||||
public class Knoten
|
||||
{
|
||||
private String name;
|
||||
|
||||
public Knoten(String s)
|
||||
{
|
||||
name = s;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user