Solved Tasks 1-3

This commit is contained in:
schrom01 2022-10-18 14:26:33 +02:00
parent 6c38eefcbe
commit 547f7ae499
3 changed files with 80 additions and 6 deletions

View File

@ -1,17 +1,30 @@
package ch.zhaw.ads; package ch.zhaw.ads;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class RankingTreeServer implements CommandExecutor { public class RankingTreeServer implements CommandExecutor {
public Tree<Competitor> createTree(String rankingText) { public Tree<Competitor> createTree(String rankingText) {
// TODO Implement Tree<Competitor> tree = new SortedBinaryTree<>();
String[] competitorsString = rankingText.split("\n");
for (String competitorString : competitorsString){
String[] values = competitorString.split(";");
tree.add(new Competitor(0, values[0], values[1]));
}
return tree;
} }
public String createSortedText(Tree<Competitor> competitorTree) { public String createSortedText(Tree<Competitor> competitorTree) {
AtomicInteger rank = new AtomicInteger(1); AtomicInteger rank = new AtomicInteger(1);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
competitorTree.traversal() // TODO Implement competitorTree.traversal().inorder(new Visitor<Competitor>() {
@Override
public void visit(Competitor obj) {
sb.append(rank.getAndAdd(1) + ". " + obj.getName() + ": " + obj.getTime() + "\n");
}
});
return sb.toString(); return sb.toString();
} }

View File

@ -68,11 +68,12 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
} }
public Traversal<T> traversal() { public Traversal<T> traversal() {
// TODO Implement return new TreeTraversal<T>(root);
} }
protected int calcHeight(TreeNode<T> node) { protected int calcHeight(TreeNode<T> node) {
// TODO Implement // TODO Implement
return 0;
} }
public int height() { public int height() {
@ -81,6 +82,7 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
protected int calcSize(TreeNode<T> p) { protected int calcSize(TreeNode<T> p) {
// TODO Implement // TODO Implement
return 0;
} }
public int size() { public int size() {

View File

@ -1,5 +1,9 @@
package ch.zhaw.ads; package ch.zhaw.ads;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> { public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
private final TreeNode<T> root; private final TreeNode<T> root;
@ -7,15 +11,70 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
this.root = root; this.root = root;
} }
@Override
public void interval(T min, T max, Visitor<T> vistor) {
}
public void inorder(Visitor<T> vis) { public void inorder(Visitor<T> vis) {
// TODO Implement inorderRec(vis, root);
}
private void inorderRec(Visitor<T> vis, TreeNode<T> root){
if(root.left != null){
inorderRec(vis, root.left);
}
vis.visit(root.getValue());
if(root.right != null){
inorderRec(vis, root.right);
}
} }
public void preorder(Visitor<T> vis) { public void preorder(Visitor<T> vis) {
// TODO Implement Stack<TreeNode<T>> stack = new Stack();
TreeNode<T> node = root;
stack.push(node);
while(!stack.isEmpty()) {
node = stack.pop();
vis.visit(node.getValue());
if(node.right != null){
stack.push(node.right);
}
if(node.left != null) {
stack.push(node.left);
}
}
} }
public void postorder(Visitor<T> vis) { public void postorder(Visitor<T> vis) {
// TODO Implement postorderRec(vis, root);
}
public void postorderRec(Visitor<T> vis, TreeNode<T> root){
inorderRec(vis, root.left);
inorderRec(vis, root.right);
vis.visit(root.getValue());
}
public void levelorder(Visitor<T> vis){
List<TreeNode<T>> nodes = new ArrayList<>();
nodes.add(root);
}
public void levelorderRec(Visitor<T> vis, List<TreeNode<T>> nodes){
List<TreeNode<T>> newNodes = new ArrayList<>();
for (TreeNode<T> node : nodes){
if(node.right != null){
newNodes.add(node.right);
}
if(node.left != null) {
newNodes.add(node.left);
}
vis.visit(node.getValue());
}
if(newNodes.size() > 0){
levelorderRec(vis, newNodes);
}
} }
} }