Noch fehlerhaftes Löschen im Baum.
parent
3d9fbcd8bb
commit
f8c0e96fdb
|
@ -3,10 +3,10 @@ import java.util.Queue;
|
||||||
|
|
||||||
public class Baum {
|
public class Baum {
|
||||||
private Knoten wurzel;
|
private Knoten wurzel;
|
||||||
|
|
||||||
private Queue<Knoten> ll = new LinkedList<Knoten>();
|
private Queue<Knoten> ll = new LinkedList<Knoten>();
|
||||||
private StringBuilder sb;
|
private StringBuilder sb;
|
||||||
|
|
||||||
public void add(int wert) {
|
public void add(int wert) {
|
||||||
if (wurzel == null) {
|
if (wurzel == null) {
|
||||||
wurzel = new Knoten(wert);
|
wurzel = new Knoten(wert);
|
||||||
|
@ -15,26 +15,31 @@ public class Baum {
|
||||||
|
|
||||||
wurzel.add(wert);
|
wurzel.add(wert);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void inorder() {
|
public void inorder() {
|
||||||
wurzel.inorder();
|
wurzel.inorder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void remove(int wert) {
|
||||||
|
if (wurzel != null)
|
||||||
|
wurzel.remove(wert);
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
// Breitensuche
|
// Breitensuche
|
||||||
if (wurzel == null)
|
if (wurzel == null)
|
||||||
return "Baum ist noch leer";
|
return "Baum ist noch leer";
|
||||||
|
|
||||||
sb = new StringBuilder();
|
sb = new StringBuilder();
|
||||||
ll.add(wurzel);
|
ll.add(wurzel);
|
||||||
|
|
||||||
wurzel.breitensuche();
|
wurzel.breitensuche();
|
||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------
|
// ----------
|
||||||
|
|
||||||
class Knoten {
|
class Knoten {
|
||||||
private int wert;
|
private int wert;
|
||||||
private Knoten links, rechts;
|
private Knoten links, rechts;
|
||||||
|
@ -46,7 +51,7 @@ public class Baum {
|
||||||
public void add(int neuerWert) {
|
public void add(int neuerWert) {
|
||||||
if (this.wert == neuerWert)
|
if (this.wert == neuerWert)
|
||||||
throw new RuntimeException("Doppelte Werte nicht erlaubt.");
|
throw new RuntimeException("Doppelte Werte nicht erlaubt.");
|
||||||
|
|
||||||
if (neuerWert < this.wert)
|
if (neuerWert < this.wert)
|
||||||
if (this.links == null)
|
if (this.links == null)
|
||||||
links = new Knoten(neuerWert);
|
links = new Knoten(neuerWert);
|
||||||
|
@ -58,34 +63,70 @@ public class Baum {
|
||||||
else
|
else
|
||||||
rechts.add(neuerWert);
|
rechts.add(neuerWert);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void inorder() {
|
public void inorder() {
|
||||||
// links
|
// links
|
||||||
if (this.links != null)
|
if (this.links != null)
|
||||||
this.links.inorder();
|
this.links.inorder();
|
||||||
|
|
||||||
// ausgabe
|
// ausgabe
|
||||||
System.out.println(this.wert);
|
System.out.println(this.wert);
|
||||||
|
|
||||||
// rechts
|
// rechts
|
||||||
if (this.rechts != null)
|
if (this.rechts != null)
|
||||||
this.rechts.inorder();
|
this.rechts.inorder();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void breitensuche() {
|
public void breitensuche() {
|
||||||
if (links != null)
|
if (links != null)
|
||||||
ll.add(this.links);
|
ll.add(this.links);
|
||||||
if (rechts != null)
|
if (rechts != null)
|
||||||
ll.add(this.rechts);
|
ll.add(this.rechts);
|
||||||
|
|
||||||
sb.append(ll.poll().wert + " ");
|
sb.append(ll.poll().wert + " ");
|
||||||
|
|
||||||
if (ll.size() == 0)
|
if (ll.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ll.peek().breitensuche();
|
ll.peek().breitensuche();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void remove(int wert) {
|
||||||
|
if (wert < this.wert && links != null)
|
||||||
|
if (links.wert == wert) {
|
||||||
|
if (links.links == null && links.rechts == null) // keine Kinder
|
||||||
|
links = null;
|
||||||
|
else if (links.links != null && links.rechts == null) // linkes Kind
|
||||||
|
links = links.links;
|
||||||
|
else if (links.rechts != null && links.links == null) // rechtes Kind
|
||||||
|
links = links.rechts;
|
||||||
|
else {
|
||||||
|
Knoten alt = links;
|
||||||
|
links = links.links.größterImTeilbaum();
|
||||||
|
links.links = alt.links;
|
||||||
|
links.rechts = alt.rechts;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
links.remove(wert);
|
||||||
|
if (wert > this.wert && rechts != null)
|
||||||
|
if (rechts.wert == wert)
|
||||||
|
rechts = null;
|
||||||
|
else
|
||||||
|
rechts.remove(wert);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Knoten größterImTeilbaum() {
|
||||||
|
if (this.rechts != null) {
|
||||||
|
Knoten kind = this.rechts.größterImTeilbaum();
|
||||||
|
if (rechts == kind)
|
||||||
|
rechts = null;
|
||||||
|
|
||||||
|
return kind;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,14 @@ public class Main {
|
||||||
b1.add(42);
|
b1.add(42);
|
||||||
b1.add(21);
|
b1.add(21);
|
||||||
b1.add(84);
|
b1.add(84);
|
||||||
|
b1.add(25);
|
||||||
|
b1.add(27);
|
||||||
|
|
||||||
b1.add(11);
|
b1.add(11);
|
||||||
|
|
||||||
// b1.inorder();
|
b1.remove(21);
|
||||||
|
|
||||||
|
b1.inorder();
|
||||||
|
|
||||||
System.out.println(b1.toString());
|
System.out.println(b1.toString());
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue