From c20fe700152cd63514f0946d22f3398854374b97 Mon Sep 17 00:00:00 2001 From: Oliver Hummel Date: Tue, 20 Dec 2022 13:54:54 +0100 Subject: [PATCH] Implementierung Inorder-Traversierung --- Baum/src/Baum.java | 20 ++++++++++++++++++++ Baum/src/Main.java | 2 ++ 2 files changed, 22 insertions(+) diff --git a/Baum/src/Baum.java b/Baum/src/Baum.java index b64b1ac..981992f 100644 --- a/Baum/src/Baum.java +++ b/Baum/src/Baum.java @@ -10,7 +10,13 @@ public class Baum { wurzel.add(wert); } + + public void inorder() { + wurzel.inorder(); + } + // ---------- + class Knoten { private int wert; private Knoten links, rechts; @@ -34,6 +40,20 @@ public class Baum { else rechts.add(neuerWert); } + + public void inorder() { + // links + if (this.links != null) + this.links.inorder(); + + // ausgabe + System.out.println(this.wert); + + // rechts + if (this.rechts != null) + this.rechts.inorder(); + } } + } diff --git a/Baum/src/Main.java b/Baum/src/Main.java index 106463b..c33701c 100644 --- a/Baum/src/Main.java +++ b/Baum/src/Main.java @@ -10,6 +10,8 @@ public class Main { b1.add(11); + b1.inorder(); + System.out.println(b1); }