diff --git a/src/ch/zhaw/ads/RankingTreeServer.java b/src/ch/zhaw/ads/RankingTreeServer.java index 7406eb5..df7d4c6 100644 --- a/src/ch/zhaw/ads/RankingTreeServer.java +++ b/src/ch/zhaw/ads/RankingTreeServer.java @@ -1,17 +1,30 @@ package ch.zhaw.ads; +import java.util.LinkedList; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; public class RankingTreeServer implements CommandExecutor { public Tree createTree(String rankingText) { - // TODO Implement + Tree 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 competitorTree) { AtomicInteger rank = new AtomicInteger(1); StringBuilder sb = new StringBuilder(); - competitorTree.traversal() // TODO Implement + competitorTree.traversal().inorder(new Visitor() { + @Override + public void visit(Competitor obj) { + 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 bcc146d..26a006f 100644 --- a/src/ch/zhaw/ads/SortedBinaryTree.java +++ b/src/ch/zhaw/ads/SortedBinaryTree.java @@ -68,11 +68,12 @@ public class SortedBinaryTree> implements Tree { } public Traversal traversal() { - // TODO Implement + return new TreeTraversal(root); } protected int calcHeight(TreeNode node) { // TODO Implement + return 0; } public int height() { @@ -81,6 +82,7 @@ public class SortedBinaryTree> implements Tree { protected int calcSize(TreeNode p) { // TODO Implement + return 0; } public int size() { diff --git a/src/ch/zhaw/ads/TreeTraversal.java b/src/ch/zhaw/ads/TreeTraversal.java index 4b9b681..1fdecef 100644 --- a/src/ch/zhaw/ads/TreeTraversal.java +++ b/src/ch/zhaw/ads/TreeTraversal.java @@ -1,5 +1,9 @@ package ch.zhaw.ads; +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + public class TreeTraversal> implements Traversal { private final TreeNode root; @@ -7,15 +11,70 @@ public class TreeTraversal> implements Traversal { this.root = root; } + @Override + public void interval(T min, T max, Visitor vistor) { + + } + public void inorder(Visitor vis) { - // TODO Implement + inorderRec(vis, root); + } + + private void inorderRec(Visitor vis, TreeNode 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 vis) { - // TODO Implement + Stack> stack = new Stack(); + TreeNode 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 vis) { - // TODO Implement + postorderRec(vis, root); + } + + public void postorderRec(Visitor vis, TreeNode root){ + inorderRec(vis, root.left); + inorderRec(vis, root.right); + vis.visit(root.getValue()); + } + + public void levelorder(Visitor vis){ + List> nodes = new ArrayList<>(); + nodes.add(root); + } + + public void levelorderRec(Visitor vis, List> nodes){ + List> newNodes = new ArrayList<>(); + for (TreeNode 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); + } + } }