solved s06 ex03

This commit is contained in:
navid.sassan 2020-11-02 16:54:24 +01:00
parent b251e0e33a
commit 01498f094e
5 changed files with 100 additions and 9 deletions

View File

@ -2,11 +2,15 @@ package ch.zhaw.ads;
public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T> { public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T> {
/** // /**
* Return the height of node t, or 0, if null. // * Return the height of node t, or 0, if null.
*/ // */
private int height(TreeNode t) { // private int height(TreeNode t) {
return t == null ? 0 : t.height; // return t == null ? 0 : t.height;
// }
public int height(TreeNode root) {
return calcHeight(root);
} }
/** /**

View File

@ -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<Competitor> 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<Competitor> 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<Competitor> parseCSV(String csv) {
int startNr, jg;
String name, country, time;
SortedBinaryTree<Competitor> competitors = new SortedBinaryTree<Competitor>();
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;
}
}

View File

@ -36,4 +36,8 @@ public class RankingTreeServer implements CommandExecutor {
trav.inorder(vis); trav.inorder(vis);
return buf.toString(); return buf.toString();
} }
public Tree getTree() {
return tree;
}
} }

View File

@ -78,7 +78,7 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
if (node == null) { if (node == null) {
return 0; return 0;
} else { } else {
return 1 + Math.max(calcHeight(node.left),calcHeight(node.right)); return 1 + Math.max(calcHeight(node.left), calcHeight(node.right));
} }
} }

View File

@ -8,18 +8,25 @@ import java.io.*;
public class RankingTreeServerTest { public class RankingTreeServerTest {
RankingTreeServer rankingServer; BinaryTreeRankingServer binaryTreeRankingServer;
RankingTreeServer rankingTreeServer;
String csv; String csv;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
rankingServer = new RankingTreeServer(); binaryTreeRankingServer = new BinaryTreeRankingServer();
rankingTreeServer = new RankingTreeServer();
csv = openFile("./build/resources/main/RangZuerich.csv"); csv = openFile("./build/resources/main/RangZuerich.csv");
} }
@Test @Test
public void test1() throws Exception { 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 // helper functions