Compare commits

...

6 commits

15 changed files with 3761 additions and 10 deletions

3166
RangZuerich.csv Normal file

File diff suppressed because it is too large Load diff

View file

@ -30,7 +30,7 @@ import java.util.Set;
*/
public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
private final int UHDTHRESHOLD = 1920;
private final String STANDARDENCODING = "ISO-8859-1";
private final String STANDARDENCODING = "UTF-8";
private JMenuItem connect, exit, open, test, retest, textView, graphicView, clear;
private JMenu menuServer;

View file

@ -15,26 +15,22 @@ public class HilbertServer implements CommandExecutor {
}
private void hilbert(Turtle turtle, int depth, double dist, double angle) {
if (depth > 0) {
if (depth >= 0) {
turtle.turn(-angle);
hilbert(turtle, depth - 1, dist, -angle);
turtle.move(dist);
turtle.turn(angle);
hilbert(turtle, depth - 1, dist, angle);
turtle.move(dist);
hilbert(turtle, depth - 1, dist, angle);
turtle.turn(angle);
turtle.move(dist);
hilbert(turtle, depth - 1, dist, -angle);
turtle.turn(-angle);
} else {
turtle.turn(-angle);
turtle.move(dist);
turtle.turn(angle);
turtle.move(dist);
turtle.turn(angle);
turtle.move(dist);
turtle.turn(-angle);
}
}
}

View file

@ -0,0 +1,20 @@
package ch.nuth.zhaw.exbox;
/**
* MyVisitor -- Diese Klasse wird für die Tests verwendet
*/
class MyVisitor<T> implements Visitor<T> {
StringBuilder output;
MyVisitor() {
output = new StringBuilder();
}
public void visit(T s) {
output.append(s);
}
public String toString() {
return output.toString();
}
}

View file

@ -0,0 +1,37 @@
package ch.nuth.zhaw.exbox;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class RankingTreeServer implements CommandExecutor {
public Tree<Competitor> createTree(String rankingText) {
SortedBinaryTree<Competitor> result = new SortedBinaryTree<>();
List<Competitor> competitors = new RankingListServer().createList(rankingText);
for (Competitor competitor : competitors) {
result.add(competitor);
}
return result;
}
public String createSortedText(Tree<Competitor> competitorTree) {
AtomicInteger rank = new AtomicInteger(1);
StringBuilder sb = new StringBuilder();
competitorTree.traversal().inorder(
(competitor) -> {
competitor.setRank(rank.getAndIncrement());
sb.append(competitor);
sb.append(System.lineSeparator());
});
return sb.toString();
}
public String execute(String rankingList) {
Tree<Competitor> competitorTree = createTree(rankingList);
return "Rangliste (Tree)\n" + createSortedText(competitorTree);
}
}

View file

@ -0,0 +1,127 @@
package ch.nuth.zhaw.exbox;
public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
protected TreeNode<T> root;
private TreeNode<T> insertAt(TreeNode<T> node, T x) {
if (node == null) {
return new TreeNode<T>(x);
} else {
if (x.compareTo(node.getValue()) <= 0) {
node.left = insertAt(node.left, x);
} else {
node.right = insertAt(node.right, x);
}
return node;
}
}
public void add(T x) {
root = insertAt(root, x);
}
// find node to replace
private TreeNode<T> findRepAt(TreeNode<T> node, TreeNode<T> rep) {
if (node.right != null) {
node.right = findRepAt(node.right, rep);
} else {
rep.values = node.values;
node = node.left;
}
return node;
}
// remove node
private TreeNode<T> removeAt(TreeNode<T> node, T x, TreeNode<T> removed) {
if (node == null) {
return null;
} else {
if (x.compareTo(node.getValue()) == 0) {
// found
removed.values = node.values;
if (node.left == null) {
node = node.right;
} else if (node.right == null) {
node = node.left;
} else {
node.left = findRepAt(node.left, node);
}
} else if (x.compareTo(node.getValue()) < 0) {
// search left
node.left = removeAt(node.left, x, removed);
} else {
// search right
node.right = removeAt(node.right, x, removed);
}
return node;
}
}
public T remove(T x) {
TreeNode<T> removed = new TreeNode<T>(null);
root = removeAt(root, x, removed);
return removed.getValue();
}
public boolean isEmpty() {
return root == null;
}
public Traversal<T> traversal() {
return new TreeTraversal<>(root);
}
protected int calcHeight(TreeNode<T> node) {
// TODO Implement
return 0;
}
public int height() {
return calcHeight(root);
}
protected int calcSize(TreeNode<T> p) {
// TODO Implement
return 0;
}
public int size() {
return calcSize(root);
}
public boolean balanced() {
return false;
}
// only for testing and debugging: show the structure of the tree
public String printTree() {
StringBuilder out = new StringBuilder();
if (root.right != null) {
printTree(root.right, out, true, "");
}
out.append(root.values).append("\n");
if (root.left != null) {
printTree(root.left, out, false, "");
}
return out.toString();
}
private void printTree(TreeNode<T> node, StringBuilder out, boolean isRight, String indent) {
if (node.right != null) {
printTree(node.right, out, true,
indent + (isRight ? " " : " | "));
}
out.append(indent);
if (isRight) {
out.append(" /");
} else {
out.append(" \\");
}
out.append("----- ");
out.append(node.values).append("\n");
if (node.left != null) {
printTree(node.left, out, false,
indent + (isRight ? " | " : " "));
}
}
}

View file

@ -0,0 +1,15 @@
package ch.nuth.zhaw.exbox;
/** interface of Traversal ADT */
public interface Traversal<T> {
/** traverse elements of tree in preorder */
void preorder(Visitor<T> vistor);
/** traverse elements of tree in inorder */
void inorder(Visitor<T> vistor);
/** traverse elements of tree in postorder */
void postorder(Visitor<T> vistor);
/** traverse elements of tree in levelorder */
void levelorder(Visitor<T> vistor);
/** traverse elements of tree interval */
void interval(T min, T max, Visitor<T> vistor);
}

View file

@ -0,0 +1,25 @@
package ch.nuth.zhaw.exbox;
/** interface of Tree ADT */
public interface Tree<T extends Comparable<T>> {
/** add an element to the tree */
void add(T o);
/** remove an element; returns the element if found else return null */
T remove(T o);
/** test if tree is empty */
boolean isEmpty();
/** returns instance of class that implements traversal interface */
Traversal<T> traversal();
/** number of elements */
int size();
/** height of the tree */
int height();
/** is the tree balanced */
boolean balanced();
}

View file

@ -0,0 +1,22 @@
package ch.nuth.zhaw.exbox;
import java.util.LinkedList;
import java.util.List;
class TreeNode<T extends Comparable<T>> {
List<T> values; // implemented as list because of doublette handling in AVL
TreeNode<T> left, right;
int height; // for AVL
TreeNode(T value){
this.values = new LinkedList<>();
this.values.add(value);
this.height = 1;
}
TreeNode(T value, TreeNode<T> left, TreeNode<T> right){
this(value); this.left = left; this.right = right;
}
T getValue(){return values.get(0);}
}

View file

@ -0,0 +1,74 @@
package ch.nuth.zhaw.exbox;
import java.util.Deque;
import java.util.LinkedList;
public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
private final TreeNode<T> root;
public TreeTraversal(TreeNode<T> root) {
this.root = root;
}
public void inorder(Visitor<T> vis) {
if (root.left != null) {
new TreeTraversal<>(root.left).inorder(vis);
}
vis.visit(root.getValue());
if (root.right != null) {
new TreeTraversal<>(root.right).inorder(vis);
}
}
public void preorder(Visitor<T> vis) {
vis.visit(root.getValue());
if (root.left != null) {
new TreeTraversal<>(root.left).preorder(vis);
}
if (root.right != null) {
new TreeTraversal<>(root.right).preorder(vis);
}
}
public void postorder(Visitor<T> vis) {
if (root.left != null) {
new TreeTraversal<>(root.left).postorder(vis);
}
if (root.right != null) {
new TreeTraversal<>(root.right).postorder(vis);
}
vis.visit(root.getValue());
}
@Override
public void levelorder(Visitor<T> vistor) {
Deque<TreeNode<T>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode<T> node = queue.poll();
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
vistor.visit(node.getValue());
}
}
@Override
public void interval(T min, T max, Visitor<T> vistor) {
// TODO Auto-generated method stub
}
}

View file

@ -0,0 +1,7 @@
package ch.nuth.zhaw.exbox;
/** interface of visitor ADT */
public interface Visitor<T> {
/** called for each element in the tree */
void visit(T obj);
}

View file

@ -0,0 +1,84 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @(#)TreeTest.java
*
*
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS5_2_test {
Tree<String> tree;
@BeforeEach
public void setUp() {
tree = new SortedBinaryTree<>();
tree.add("B");
tree.add("A");
tree.add("C");
tree.add("D");
}
@Test
public void testInorder() {
Visitor<String> v = new MyVisitor<>();
tree.traversal().inorder(v);
assertEquals("ABCD", v.toString(), "inorder");
}
@Test
public void testPreorder() {
Visitor<String> v = new MyVisitor<>();
tree.traversal().preorder(v);
assertEquals("BACD", v.toString(), "preorder");
}
@Test
public void testPostorder() {
Visitor<String> v = new MyVisitor<>();
tree.traversal().postorder(v);
assertEquals("ADCB", v.toString(), "postorder");
}
@Test
public void testLevelorder() {
Visitor<String> v = new MyVisitor<>();
tree.traversal().levelorder(v);
assertEquals("BACD", v.toString(), "levelorder");
}
@Test
public void testMixed() {
Tree<String> tree = new SortedBinaryTree<>();
List<String> list = new LinkedList<>();
for (int i = 0; i < 1000; i++) {
char c = (char) ('A' + (Math.random() * 26));
int op = (int) (Math.random() * 2);
switch (op) {
case 0:
list.add(Character.toString(c));
tree.add(Character.toString(c));
break;
case 1:
list.remove(Character.toString(c));
tree.remove(Character.toString(c));
break;
}
}
Collections.sort(list);
String expected = String.join("", list);
Visitor<String> v = new MyVisitor<>();
tree.traversal().inorder(v);
assertEquals(expected, v.toString(), "mixed");
}
}

View file

@ -0,0 +1,61 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ADS5_3_test {
Tree<Competitor> rankGood;
Tree<Competitor> rankTest;
String textGood;
String textTest;
public Tree<Competitor> createTree(String rankingText) {
Tree<Competitor> competitorTree = new SortedBinaryTree<>();
String[] lines = rankingText.split("\n");
for (String line : lines) {
String name = line.split(";")[0];
String time = line.split(";")[1];
competitorTree.add(new Competitor(0, name, time));
}
return competitorTree;
}
public String createSortedText(Tree<Competitor> competitorTree) {
AtomicInteger rank = new AtomicInteger(1);
StringBuilder sb = new StringBuilder();
competitorTree.traversal().inorder(c -> {c.setRank(rank.getAndIncrement()); sb.append(c).append("\n");});
return sb.toString();
}
@BeforeEach
public void setUp() {
String rangliste = "Mueller Stefan;02:31:14\n" +
"Marti Adrian;02:30:09\n" +
"Kiptum Daniel;02:11:31\n" +
"Ancay Tarcis;02:20:02\n" +
"Kreibuhl Christian;02:21:47\n" +
"Ott Michael;02:33:48\n" +
"Menzi Christoph;02:27:26\n" +
"Oliver Ruben;02:32:12\n" +
"Elmer Beat;02:33:53\n" +
"Kuehni Martin;02:33:36\n";
rankGood = createTree(rangliste);
textGood = createSortedText(rankGood);
rankTest = new RankingTreeServer().createTree(rangliste);
textTest = new RankingTreeServer().createSortedText(rankTest);
}
@Test
public void testCreateText() {
String[] good = textGood.split("\n");
String[] test = textTest.split("\n");
assertEquals(good.length, test.length, "length");
for (int i = 0; i < good.length;i++) {
assertEquals(good[i], test[i], "rangliste["+i+"]");
}
}
}

View file

@ -0,0 +1,61 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.LinkedList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @(#)TreeTest.java
*
*
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS5_4_test {
Tree<String> tree;
@BeforeEach
public void setUp() {
tree = new SortedBinaryTree<>();
tree.add("B");
tree.add("A");
tree.add("C");
tree.add("D");
}
@Test
public void testHeight() {
assertEquals(3, tree.height(), "height");
}
@Test
public void testSize() {
assertEquals(4, tree.size(), "size");
}
@Test
public void testSizeMixed() {
Tree<String> tree = new SortedBinaryTree<>();
List<String> list = new LinkedList<>();
for (int i = 0; i < 1000; i++) {
char c = (char) ('A' + (Math.random() * 26));
int op = (int) (Math.random() * 2);
switch (op) {
case 0:
list.add(Character.toString(c));
tree.add(Character.toString(c));
break;
case 1:
list.remove(Character.toString(c));
tree.remove(Character.toString(c));
break;
}
}
assertEquals(list.size(), tree.size());
}
}

View file

@ -0,0 +1,56 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @(#)TreeTest.java
*
*
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS5_5_test {
Tree<String> tree;
@BeforeEach
public void setUp() {
tree = new SortedBinaryTree<>();
tree.add("B");
tree.add("A");
tree.add("C");
tree.add("D");
}
@Test
public void testInterval() {
char left = 'K';
char right = 'O';
for (int i = 0; i < 200; i++) {
char c = (char) ('A' + (Math.random() * 26));
tree.add(Character.toString(c));
}
// get all elements with inorder
Visitor<String> v = new MyVisitor<>();
tree.traversal().inorder(v);
int count = 0;
String s = v.toString();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= left && s.charAt(i) <= right) count++;
}
// now interval
v = new MyVisitor<>();
tree.traversal().interval(Character.toString(left), Character.toString(right), v);
s = v.toString();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
assertTrue(c >= left && c <= right, c + " in interval "+left+" "+right);
}
assertEquals(count, s.length(), "size");
}
}