diff --git a/06/src/main/java/ch/zhaw/ads/AVLSearchTree.java b/06/src/main/java/ch/zhaw/ads/AVLSearchTree.java index 5715966..57e636d 100644 --- a/06/src/main/java/ch/zhaw/ads/AVLSearchTree.java +++ b/06/src/main/java/ch/zhaw/ads/AVLSearchTree.java @@ -2,11 +2,15 @@ package ch.zhaw.ads; public class AVLSearchTree> extends SortedBinaryTree { - /** - * Return the height of node t, or 0, if null. - */ - private int height(TreeNode t) { - return t == null ? 0 : t.height; + // /** + // * Return the height of node t, or 0, if null. + // */ + // private int height(TreeNode t) { + // return t == null ? 0 : t.height; + // } + + public int height(TreeNode root) { + return calcHeight(root); } /** diff --git a/06/src/main/java/ch/zhaw/ads/BinaryTreeRankingServer.java b/06/src/main/java/ch/zhaw/ads/BinaryTreeRankingServer.java new file mode 100644 index 0000000..a674a38 --- /dev/null +++ b/06/src/main/java/ch/zhaw/ads/BinaryTreeRankingServer.java @@ -0,0 +1,76 @@ +package ch.zhaw.ads; + +import java.lang.ArrayIndexOutOfBoundsException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class BinaryTreeRankingServer implements CommandExecutor { + SortedBinaryTree competitors; + int startingRank; + StringBuffer result; + + @Override + public String execute(String input) { + this.competitors = parseCSV(input); + + result = new StringBuffer(100); + startingRank = 1; + result.append("\n\nSorted Competitors (by time):\n"); + competitors.traversal().inorder(visitor); + + return result.toString(); + } + + // This works only because of + // https://stackoverflow.com/questions/29029849/why-do-java-8-lambdas-allow-access-to-non-final-class-variables + Visitor visitor = competitor -> { + competitor.setRank(startingRank++); + result.append(competitor.toString() + "\n"); + }; + + /** + * NOTE ABOUT CSV FORMAT (from `man virt-diff`) + * Comma-separated values (CSV) is a deceptive format. It seems like it should be easy to parse, + * but it is definitely not easy to parse. + * + * Myth: Just split fields at commas. Reality: This does not work reliably. This example has two + * columns: + * + * "foo,bar",baz + * + * Myth: Read the file one line at a time. Reality: This does not work reliably. This example has + * one row: + * + * "foo + * bar",baz + */ + private SortedBinaryTree parseCSV(String csv) { + int startNr, jg; + String name, country, time; + + SortedBinaryTree competitors = new SortedBinaryTree(); + for (String record : csv.split("\\r?\\n")) { + String[] values = record.split(";"); + + try { + startNr = Integer.valueOf(values[0]); + name = values[1]; + jg = Integer.valueOf(values[2]); + country = values[3]; + time = values[4]; + competitors.add(new Competitor(startNr, name, jg, country, time)); + } catch (NumberFormatException | ParseException | ArrayIndexOutOfBoundsException e) { + System.err.println("Failed to add competitor."); + continue; + } + } + return competitors; + } + + public Tree getTree() { + return competitors; + } +} diff --git a/06/src/main/java/ch/zhaw/ads/RankingTreeServer.java b/06/src/main/java/ch/zhaw/ads/RankingTreeServer.java index b25477d..53cd58c 100644 --- a/06/src/main/java/ch/zhaw/ads/RankingTreeServer.java +++ b/06/src/main/java/ch/zhaw/ads/RankingTreeServer.java @@ -36,4 +36,8 @@ public class RankingTreeServer implements CommandExecutor { trav.inorder(vis); return buf.toString(); } + + public Tree getTree() { + return tree; + } } diff --git a/06/src/main/java/ch/zhaw/ads/SortedBinaryTree.java b/06/src/main/java/ch/zhaw/ads/SortedBinaryTree.java index 43c2d64..11e41b2 100644 --- a/06/src/main/java/ch/zhaw/ads/SortedBinaryTree.java +++ b/06/src/main/java/ch/zhaw/ads/SortedBinaryTree.java @@ -78,7 +78,7 @@ public class SortedBinaryTree> implements Tree { if (node == null) { return 0; } else { - return 1 + Math.max(calcHeight(node.left),calcHeight(node.right)); + return 1 + Math.max(calcHeight(node.left), calcHeight(node.right)); } } diff --git a/06/src/test/java/ch/zhaw/ads/RankingTreeServerTest.java b/06/src/test/java/ch/zhaw/ads/RankingTreeServerTest.java index 4929c8b..39b453f 100644 --- a/06/src/test/java/ch/zhaw/ads/RankingTreeServerTest.java +++ b/06/src/test/java/ch/zhaw/ads/RankingTreeServerTest.java @@ -8,18 +8,25 @@ import java.io.*; public class RankingTreeServerTest { - RankingTreeServer rankingServer; + BinaryTreeRankingServer binaryTreeRankingServer; + RankingTreeServer rankingTreeServer; String csv; @Before public void setUp() throws Exception { - rankingServer = new RankingTreeServer(); + binaryTreeRankingServer = new BinaryTreeRankingServer(); + rankingTreeServer = new RankingTreeServer(); csv = openFile("./build/resources/main/RangZuerich.csv"); } @Test public void test1() throws Exception { - System.out.println(rankingServer.execute(csv)); + rankingTreeServer.execute(csv); + binaryTreeRankingServer.execute(csv); + + System.out.println(rankingTreeServer.getTree().height()); + System.out.println(binaryTreeRankingServer.getTree().height()); + // The AVL tree constantly rebalances itself when new items are inserted, therefore its height remains rather small the whole time. On the other hand, the unbalanced sorted binary tree does not do any rebalancing, leading to it growing in height. } // helper functions