Hallo,
ich muss hier einen Rot-Schwarzbaum machen im Fach Algorithmen und Datenstruktur. Kann ich die Methode Print auch Rekursiv machen?
Ich bedanke mich im vorraus und frohes Fest.
Mit freundlichen Grüßen Xalo
public class Test {
private Knoten wurzel;
public Integer findeIndex(Character key) {
Knoten x = wurzel;
while (x != null) {
int vergleich = key.compareTo(x.key);
if (vergleich < 0) x = x.linkesKind;
else if (vergleich > 0) x = x.rechtesKind;
else return x.wert;
}
return null;
}
public static final boolean ROT = true;
public static final boolean SCHWARZ = false;
private class Knoten {
private Character key;
private Integer wert;
private Knoten linkesKind, rechtesKind, eltern;
boolean farbe;
Knoten(Character key, Integer wert, boolean farbe) {
this.key = key;
this.wert = wert;
this.farbe = farbe;
}
}
private Knoten linksRotation(Knoten h) {
Knoten x = h.rechtesKind;
h.rechtesKind = x.linkesKind;
x.linkesKind = h;
x.farbe = h.farbe;
h.farbe = ROT;
return x;
}
private Knoten rechtsRotation(Knoten h) {
Knoten x = h.linkesKind;
h.linkesKind = x.rechtesKind;
x.rechtesKind = h;
x.farbe = h.farbe;
h.farbe = ROT;
return x;
}
public void tauscheFarben(Knoten h) {
h.farbe = ROT;
h.linkesKind.farbe = SCHWARZ;
h.rechtesKind.farbe = SCHWARZ;
}
private boolean istRot(Knoten x) {
if (x == null) return false;
return x.farbe == ROT;
}
public void put(Character key, Integer wert) {
wurzel = put(wurzel, key, wert);
}
public Knoten put(Knoten h, Character key, Integer wert) {
if (h == null) {
return new Knoten(key, wert, ROT);
}
int vergleich = key.compareTo(h.key);
if (vergleich < 0) {
h.linkesKind = put(h.linkesKind, key, wert);
} else if (vergleich > 0) {
h.rechtesKind = put(h.rechtesKind, key, wert);
} else {
h.wert = wert;
}
if (istRot(h.rechtesKind) && !istRot(h.linkesKind)) {
h = linksRotation(h);
}
if (istRot(h.linkesKind) && istRot(h.linkesKind.linkesKind)) {
h = rechtsRotation(h);
}
if (istRot(h.linkesKind) && istRot(h.rechtesKind)) {
tauscheFarben(h);
}
return h;
}
public void print(){
System.out.println(wurzel.key);
System.out.println(wurzel.linkesKind.key);
System.out.println(wurzel.rechtesKind.key);
System.out.println(wurzel.linkesKind.linkesKind.key);
System.out.println(wurzel.linkesKind.rechtesKind.key);
System.out.println(wurzel.rechtesKind.rechtesKind.key);
System.out.println(wurzel.rechtesKind.linkesKind.key);
System.out.println(wurzel.linkesKind.linkesKind.linkesKind.key);
//System.out.println(wurzel.rechtesKind.rechtesKind.rechtesKind.key);
}
public static void main(String[] args) {
char[] a = {'W', 'E', 'I', 'H', 'N', 'A', 'C', 'H', 'T', 'E', 'N'}; //char array erstellt//
Test Baum = new Test();
for (char i = 0; i < a.length; i++) {
Baum.put(a, i + 1);
}
Baum.print();
}
}
ich muss hier einen Rot-Schwarzbaum machen im Fach Algorithmen und Datenstruktur. Kann ich die Methode Print auch Rekursiv machen?
Ich bedanke mich im vorraus und frohes Fest.
Mit freundlichen Grüßen Xalo
public class Test {
private Knoten wurzel;
public Integer findeIndex(Character key) {
Knoten x = wurzel;
while (x != null) {
int vergleich = key.compareTo(x.key);
if (vergleich < 0) x = x.linkesKind;
else if (vergleich > 0) x = x.rechtesKind;
else return x.wert;
}
return null;
}
public static final boolean ROT = true;
public static final boolean SCHWARZ = false;
private class Knoten {
private Character key;
private Integer wert;
private Knoten linkesKind, rechtesKind, eltern;
boolean farbe;
Knoten(Character key, Integer wert, boolean farbe) {
this.key = key;
this.wert = wert;
this.farbe = farbe;
}
}
private Knoten linksRotation(Knoten h) {
Knoten x = h.rechtesKind;
h.rechtesKind = x.linkesKind;
x.linkesKind = h;
x.farbe = h.farbe;
h.farbe = ROT;
return x;
}
private Knoten rechtsRotation(Knoten h) {
Knoten x = h.linkesKind;
h.linkesKind = x.rechtesKind;
x.rechtesKind = h;
x.farbe = h.farbe;
h.farbe = ROT;
return x;
}
public void tauscheFarben(Knoten h) {
h.farbe = ROT;
h.linkesKind.farbe = SCHWARZ;
h.rechtesKind.farbe = SCHWARZ;
}
private boolean istRot(Knoten x) {
if (x == null) return false;
return x.farbe == ROT;
}
public void put(Character key, Integer wert) {
wurzel = put(wurzel, key, wert);
}
public Knoten put(Knoten h, Character key, Integer wert) {
if (h == null) {
return new Knoten(key, wert, ROT);
}
int vergleich = key.compareTo(h.key);
if (vergleich < 0) {
h.linkesKind = put(h.linkesKind, key, wert);
} else if (vergleich > 0) {
h.rechtesKind = put(h.rechtesKind, key, wert);
} else {
h.wert = wert;
}
if (istRot(h.rechtesKind) && !istRot(h.linkesKind)) {
h = linksRotation(h);
}
if (istRot(h.linkesKind) && istRot(h.linkesKind.linkesKind)) {
h = rechtsRotation(h);
}
if (istRot(h.linkesKind) && istRot(h.rechtesKind)) {
tauscheFarben(h);
}
return h;
}
public void print(){
System.out.println(wurzel.key);
System.out.println(wurzel.linkesKind.key);
System.out.println(wurzel.rechtesKind.key);
System.out.println(wurzel.linkesKind.linkesKind.key);
System.out.println(wurzel.linkesKind.rechtesKind.key);
System.out.println(wurzel.rechtesKind.rechtesKind.key);
System.out.println(wurzel.rechtesKind.linkesKind.key);
System.out.println(wurzel.linkesKind.linkesKind.linkesKind.key);
//System.out.println(wurzel.rechtesKind.rechtesKind.rechtesKind.key);
}
public static void main(String[] args) {
char[] a = {'W', 'E', 'I', 'H', 'N', 'A', 'C', 'H', 'T', 'E', 'N'}; //char array erstellt//
Test Baum = new Test();
for (char i = 0; i < a.length; i++) {
Baum.put(a, i + 1);
}
Baum.print();
}
}