From 630a5c8acb79fe865d4fa87940fb6a91ac0c076f Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 18 Oct 2022 14:10:29 +0200 Subject: [PATCH] Add methods for calculating size and height --- .../ch/nuth/zhaw/exbox/SortedBinaryTree.java | 25 ++++++++++++++++--- .../java/ch/nuth/zhaw/exbox/ADS5_3_test.java | 4 +-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/SortedBinaryTree.java b/app/src/main/java/ch/nuth/zhaw/exbox/SortedBinaryTree.java index 3cc9f7a..6f9d877 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/SortedBinaryTree.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/SortedBinaryTree.java @@ -1,5 +1,7 @@ package ch.nuth.zhaw.exbox; +import java.util.stream.Stream; + public class SortedBinaryTree> implements Tree { protected TreeNode root; @@ -72,8 +74,14 @@ public class SortedBinaryTree> implements Tree { } protected int calcHeight(TreeNode node) { - // TODO Implement - return 0; + int result = 0; + + if (node.left != null || node.right != null) { + result = Stream.of(node.left, node.right).filter( + (innerNode) -> innerNode != null).mapToInt((innerNode) -> calcHeight(innerNode)).max().getAsInt(); + } + + return result + 1; } public int height() { @@ -81,8 +89,17 @@ public class SortedBinaryTree> implements Tree { } protected int calcSize(TreeNode p) { - // TODO Implement - return 0; + int result = 1; + + if (p.left != null) { + result += calcSize(p.left); + } + + if (p.right != null) { + result += calcSize(p.right); + } + + return result; } public int size() { diff --git a/app/src/test/java/ch/nuth/zhaw/exbox/ADS5_3_test.java b/app/src/test/java/ch/nuth/zhaw/exbox/ADS5_3_test.java index 410e54d..db606a8 100644 --- a/app/src/test/java/ch/nuth/zhaw/exbox/ADS5_3_test.java +++ b/app/src/test/java/ch/nuth/zhaw/exbox/ADS5_3_test.java @@ -51,8 +51,8 @@ public class ADS5_3_test { @Test public void testCreateText() { - String[] good = textGood.split("\n"); - String[] test = textTest.split("\n"); + String[] good = textGood.split("[\r\n]*"); + String[] test = textTest.split("[\r\n]*"); assertEquals(good.length, test.length, "length"); for (int i = 0; i < good.length;i++) { assertEquals(good[i], test[i], "rangliste["+i+"]");