Rudimentäres Einfügen hinzugefügt.
parent
d73783ec8a
commit
f60c7cb552
|
@ -1,7 +1,30 @@
|
|||
|
||||
public class Baum {
|
||||
private Knoten wurzel;
|
||||
|
||||
public void add(int wert) {
|
||||
if (wurzel == null) {
|
||||
wurzel = new Knoten(wert);
|
||||
return;
|
||||
}
|
||||
|
||||
wurzel.add(wert);
|
||||
}
|
||||
|
||||
class Knoten {
|
||||
private int wert;
|
||||
private Knoten links, rechts;
|
||||
|
||||
public Knoten(int wert) {
|
||||
this.wert = wert;
|
||||
}
|
||||
|
||||
public void add(int neuerWert) {
|
||||
if (neuerWert < this.wert)
|
||||
links = new Knoten(neuerWert);
|
||||
else if (neuerWert > this.wert)
|
||||
rechts = new Knoten(neuerWert);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,13 @@
|
|||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
Baum b1 = new Baum();
|
||||
|
||||
b1.add(42);
|
||||
b1.add(21);
|
||||
b1.add(84);
|
||||
|
||||
System.out.println(b1);
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue