From 1462cdcabae67483f97eeaef5c1dd02038d8cb45 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Wed, 19 Oct 2022 07:55:29 +0200 Subject: [PATCH] Solved Task 4 --- src/ch/zhaw/ads/RankingTreeServer.java | 2 +- src/ch/zhaw/ads/SortedBinaryTree.java | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/ch/zhaw/ads/RankingTreeServer.java b/src/ch/zhaw/ads/RankingTreeServer.java index df7d4c6..0db5e8e 100644 --- a/src/ch/zhaw/ads/RankingTreeServer.java +++ b/src/ch/zhaw/ads/RankingTreeServer.java @@ -22,7 +22,7 @@ public class RankingTreeServer implements CommandExecutor { competitorTree.traversal().inorder(new Visitor() { @Override public void visit(Competitor obj) { - sb.append(rank.getAndAdd(1) + ". " + obj.getName() + ": " + obj.getTime() + "\n"); + sb.append(rank.getAndAdd(1) + " " + obj.getName() + " " + obj.getTime() + "\n"); } }); return sb.toString(); diff --git a/src/ch/zhaw/ads/SortedBinaryTree.java b/src/ch/zhaw/ads/SortedBinaryTree.java index 26a006f..15a0f4d 100644 --- a/src/ch/zhaw/ads/SortedBinaryTree.java +++ b/src/ch/zhaw/ads/SortedBinaryTree.java @@ -72,8 +72,14 @@ public class SortedBinaryTree> implements Tree { } protected int calcHeight(TreeNode node) { - // TODO Implement - return 0; + int heigthLeft = 0, heigthRight = 0; + if(node.left != null){ + heigthLeft = calcHeight(node.left); + } + if(node.right != null){ + heigthRight = calcSize(node.right); + } + return Math.max(heigthLeft, heigthRight) + 1; } public int height() { @@ -81,8 +87,14 @@ public class SortedBinaryTree> implements Tree { } protected int calcSize(TreeNode p) { - // TODO Implement - return 0; + int sizeLeft = 0, sizeRight = 0; + if(p.left != null){ + sizeLeft = calcSize(p.left); + } + if(p.right != null){ + sizeRight = calcSize(p.right); + } + return sizeLeft + sizeRight + 1; } public int size() {