Implementierung des Binärbaums - Teil 2: Einfügen
This commit is contained in:
@@ -16,8 +16,9 @@ public class Abschluss extends Baumelement
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// weitere Methoden
|
public Baumelement einfügen(Datenelement datenNeu) {
|
||||||
|
return new Knoten(datenNeu);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,6 @@ public abstract class Baumelement
|
|||||||
// Attribute
|
// Attribute
|
||||||
|
|
||||||
// Methoden
|
// Methoden
|
||||||
|
public abstract Baumelement einfügen(Datenelement datenNeu);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ public class BinBaum
|
|||||||
this.wurzel = new Abschluss();
|
this.wurzel = new Abschluss();
|
||||||
}
|
}
|
||||||
|
|
||||||
// weitere Methoden
|
public void einfügen(Datenelement datenNeu) {
|
||||||
|
this.wurzel = this.wurzel.einfügen(datenNeu);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,4 +9,7 @@
|
|||||||
public interface Datenelement
|
public interface Datenelement
|
||||||
{
|
{
|
||||||
//hier Methodenköpfe eintragen in der Form "int beispielMethode(int y);"
|
//hier Methodenköpfe eintragen in der Form "int beispielMethode(int y);"
|
||||||
|
public boolean istGleich(Datenelement d);
|
||||||
|
public boolean istGrößerAls(Datenelement d);
|
||||||
|
public String gibSchlüssel();
|
||||||
}
|
}
|
||||||
|
|||||||
13
Knoten.java
13
Knoten.java
@@ -23,7 +23,18 @@ public class Knoten extends Baumelement
|
|||||||
this.rechterNachfolger = new Abschluss();
|
this.rechterNachfolger = new Abschluss();
|
||||||
}
|
}
|
||||||
|
|
||||||
// weitere Methoden
|
public Baumelement einfügen(Datenelement datenNeu) {
|
||||||
|
if (this.daten.istGleich(datenNeu)) {
|
||||||
|
System.out.println("Daten schon enthalten!");
|
||||||
|
} else {
|
||||||
|
if (this.daten.istGrößerAls(datenNeu)) {
|
||||||
|
this.linkerNachfolger = this.linkerNachfolger.einfügen(datenNeu);
|
||||||
|
} else {
|
||||||
|
this.rechterNachfolger = this.rechterNachfolger.einfügen(datenNeu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,16 +7,28 @@
|
|||||||
*/
|
*/
|
||||||
public class Wortpaar implements Datenelement
|
public class Wortpaar implements Datenelement
|
||||||
{
|
{
|
||||||
// Attribute
|
String wortDeutsch;
|
||||||
|
String wortEnglisch;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Konstruktor für Objekte der Klasse Wortpaar
|
* Konstruktor für Objekte der Klasse Wortpaar
|
||||||
*/
|
*/
|
||||||
public Wortpaar()
|
public Wortpaar(String wortDeutsch, String wortEnglisch) {
|
||||||
{
|
this.wortDeutsch = wortDeutsch;
|
||||||
|
this.wortEnglisch = wortEnglisch;
|
||||||
}
|
}
|
||||||
|
|
||||||
// weitere Methoden
|
public String gibSchlüssel() {
|
||||||
|
return this.wortDeutsch;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean istGleich(Datenelement anderesWortpaar) {
|
||||||
|
return this.gibSchlüssel().equals(anderesWortpaar.gibSchlüssel());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean istGrößerAls(Datenelement anderesWortpaar) {
|
||||||
|
return this.gibSchlüssel().compareTo(anderesWortpaar.gibSchlüssel()) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user