Add server for running AVLSearchTree

This commit is contained in:
Manuel Thalmann 2022-10-31 20:50:26 +01:00
parent 479cac0393
commit 0a4895c538

View file

@ -0,0 +1,38 @@
package ch.nuth.zhaw.exbox;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class RankingAVLTreeServer implements CommandExecutor {
public Tree<Competitor> createTree(String rankingText) {
SortedBinaryTree<Competitor> result = new AVLSearchTree<>();
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) + "\n\n" +
"Height: " + competitorTree.height() + "\nSize: " + competitorTree.size();
}
}