2. Rekursive (dynamische) Datenstrukturen
1. Rekursive Algorithmen
II.3.2 Rekursive Datenstrukturen - 1 -
(
new
Typ
Ausdruck
)
Name
Ausdruck
Ausdruck
Infix-Operator
(
Ausdruck
:
[
Ausdruck
)
Methodenaufruf
Methodenaufruf
?
Präfix-Operator
Name
Grundwert
Ausdruck
Ausdruck
]
]
II.3.2 Rekursive Datenstrukturen - 2 -
Ganzzahl
[
(
new
Typ
Ausdruck
)
Name
Ausdruck
Ausdruck
Infix-Operator
(
Ausdruck
:
[
Ausdruck
)
Methodenaufruf
Methodenaufruf
?
Präfix-Operator
Name
Grundwert
Ausdruck
Ausdruck
]
]
II.3.2 Rekursive Datenstrukturen - 3 -
Ganzzahl
[
wert
17
wert
4
int wert;
Element next;
...
}
class Element {
Attribute
wert
25
...
}
II.3.2 Rekursive Datenstrukturen - 4 -
private Element kopf;
null
next
Objekt Element
public class Liste {
next
Attribute
Attribute
next
Objekt Element
kopf
Objekt Element
Attribut
Objekt Liste
Realisierung von Listen
String toString ()
void drucke ()
void druckeRueckwaerts ()
void fuegeVorneEin (int wert)
void fuegeSortiertEin (int wert)
void loesche (int wert)
Element (int wert, Element next)
int getWert ()
void setWert (int wert)
Element getNext()
void setNext (Element next)
String toString ()
II.3.2 Rekursive Datenstrukturen - 5 -
Liste ()
Element (int wert)
void loesche ()
Klasse Liste
Klasse Element
Schnittstellendokumentation
(25);
(4);
(l.suche (17) != null) System.out.println (l.suche(17));
( 4 17 25 30 )
( 30 25 17 4 )
( 2 4 12 17 25 28 30 45 )
17
( 2 4 12 25 30 45 )
( )
II.3.2 Rekursive Datenstrukturen - 6 -
l.loesche (28); l.loesche (10); l.loesche (17); l.drucke ();
l.loesche (); l.drucke ();
if
l.fuegeSortiertEin (28); l.fuegeSortiertEin (12);
l.fuegeSortiertEin (45); l.fuegeSortiertEin (2); l.drucke ();
l.fuegeVorneEin (30); l.fuegeVorneEin
l.fuegeVorneEin (17); l.fuegeVorneEin
l.drucke (); l.druckeRueckwaerts ();
Liste l = new Liste ();
Verwendung von Listen
}
}
public String toString () {
return new Integer(wert).toString();
}
}
II.3.2 Rekursive Datenstrukturen - 7 -
Element getNext () { return next; }
void setNext (Element next) { this.next = next; }
int getWert () {
return wert; }
void setWert (int wert) { this.wert = wert; }
Element (int wert, Element next) {
this.wert = wert; this.next = next;
Element (int wert) { this.wert = wert; next = null;
class Element {
int wert;
Element next;
Element-Klasse
private static Element suche
if
(kopf = = null)
else if (kopf.wert = = wert)
else
}
II.3.2 Rekursive Datenstrukturen - 8 -
(int wert, Element kopf) {
return null;
return kopf;
return suche (wert, kopf.next);
public Element suche (int wert) {
return suche (wert, kopf);
}
public Liste () {
kopf = null;
}
private Element kopf;
public class Liste {
Liste-Klasse: Erzeugung und Suche
}
{
}
}
{
{
}
II.3.2 Rekursive Datenstrukturen - 9 -
public void druckeRueckwaerts()
System.out.println (this.toStringRueckwaerts());
private static String durchlaufeRueckwaerts (Element kopf) {
if
(kopf != null)
return durchlaufeRueckwaerts(kopf.next) + " " + kopf.wert;
else return "";
}
public String toStringRueckwaerts ()
return
"(" + durchlaufeRueckwaerts(kopf) + " )";
public void drucke() { System.out.println (this); }
private static String durchlaufe (Element kopf)
if
(kopf != null)
return kopf.wert + " " + durchlaufe(kopf.next);
else return "";
public String toString () {
return "( " + durchlaufe(kopf) + ")";
Liste-Klasse: Ausgabe
}
if
(kopf = = null)
else
II.3.2 Rekursive Datenstrukturen - 10 -
kopf = new Element (wert);
kopf = new Element (wert, kopf);
public void fuegeVorneEin (int wert) {
Liste-Klasse: Einfügen
{
element.next = fuegeSortiertEin (wert, element.next);
return element;
} }
else
II.3.2 Rekursive Datenstrukturen - 11 -
(wert < element.wert)
return new Element (wert, element);
else if
private Element fuegeSortiertEin (int wert, Element element) {
if
(element = = null)
return new Element (wert);
public void fuegeSortiertEin (int wert) {
kopf = fuegeSortiertEin (wert, kopf); }
Liste-Klasse: Einfügen
else
else if
private
if
II.3.2 Rekursive Datenstrukturen - 12 -
{
fuegeSortiertEin (wert, element.next);
} }
(wert < element.wert)
new Element (wert, element);
void
fuegeSortiertEin (int wert, Element element) {
(element = = null)
new Element (wert);
public void fuegeSortiertEin (int wert) {
fuegeSortiertEin (wert, kopf); }
Liste-Klasse: Einfügen
{
element.next = fuegeSortiertEin (wert, element.next);
return element;
} }
else
II.3.2 Rekursive Datenstrukturen - 13 -
public void fuegeSortiertEin (int wert) {
Element element = kopf;
if
(kopf = = null || wert < kopf.wert) fuegeVorneEin(wert);
else {while (element.next != null && wert > element.next.wert)
element = element.next;
element.next = new Element (wert, element.next);
}}
(wert < element.wert)
return new Element (wert, element);
else if
private Element fuegeSortiertEin (int wert, Element element) {
if
(element = = null)
return new Element (wert);
public void fuegeSortiertEin (int wert) {
kopf = fuegeSortiertEin (wert, kopf); }
Liste-Klasse: Einfügen
}}
II.3.2 Rekursive Datenstrukturen - 14 -
return null;
if
(element = = null)
else if (wert = = element.wert) return element.next;
else
{
element.next = loesche (wert, element.next);
return element;
}
private static Element loesche (int wert, Element element) {
public void loesche (int wert) {
kopf = loesche (wert, kopf);
}
public void loesche () {
kopf = null;
}
Liste-Klasse: Löschen
Attribute
links wert rechts
25
Attribute
links wert rechts
17
II.3.2 Rekursive Datenstrukturen - 15 -
public class Baum {
private Knoten wurzel;
...
}
Objekt Knoten
links wert rechts
4
Objekt Knoten
wurzel
Attribute
Objekt Knoten
class Knoten {
int wert;
Knoten links, rechts;
...
}
Attribut
Objekt Baum
Realisierung von binären Bäumen