From f60c7cb5524ef568fe0fd17dc25839295886ac64 Mon Sep 17 00:00:00 2001 From: Oliver Hummel Date: Tue, 20 Dec 2022 13:11:45 +0100 Subject: [PATCH] =?UTF-8?q?Rudiment=C3=A4res=20Einf=C3=BCgen=20hinzugef?= =?UTF-8?q?=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Baum/src/Baum.java | 23 +++++++++++++++++++++++ Baum/src/Main.java | 8 +++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Baum/src/Baum.java b/Baum/src/Baum.java index c330b02..3357678 100644 --- a/Baum/src/Baum.java +++ b/Baum/src/Baum.java @@ -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); + } + } } diff --git a/Baum/src/Main.java b/Baum/src/Main.java index 671bb51..f6cfcad 100644 --- a/Baum/src/Main.java +++ b/Baum/src/Main.java @@ -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); }