Compare commits

...

2 commits

3 changed files with 46 additions and 10 deletions

View file

@ -1,5 +1,7 @@
package ch.nuth.zhaw.exbox;
import java.util.stream.Stream;
public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
protected TreeNode<T> root;
@ -72,8 +74,14 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
}
protected int calcHeight(TreeNode<T> node) {
// TODO Implement
return 0;
int result = 0;
if (node.left != null || node.right != null) {
result = Stream.of(node.left, node.right).filter(
(innerNode) -> innerNode != null).mapToInt((innerNode) -> calcHeight(innerNode)).max().getAsInt();
}
return result + 1;
}
public int height() {
@ -81,8 +89,17 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
}
protected int calcSize(TreeNode<T> p) {
// TODO Implement
return 0;
int result = 1;
if (p.left != null) {
result += calcSize(p.left);
}
if (p.right != null) {
result += calcSize(p.right);
}
return result;
}
public int size() {

View file

@ -52,7 +52,7 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
queue.add(root);
while (!queue.isEmpty()) {
TreeNode<T> node = queue.poll();
TreeNode<T> node = queue.pop();
if (node.left != null) {
queue.add(node.left);
@ -67,8 +67,27 @@ public class TreeTraversal<T extends Comparable<T>> implements Traversal<T> {
}
@Override
public void interval(T min, T max, Visitor<T> vistor) {
// TODO Auto-generated method stub
public void interval(T min, T max, Visitor<T> visitor) {
Deque<TreeNode<T>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode<T> node = queue.pop();
if (node != null) {
T value = node.getValue();
if (value.compareTo(min) >= 0 && value.compareTo(max) <= 0) {
visitor.visit(value);
queue.add(node.left);
queue.add(node.right);
} else if (value.compareTo(max) > 0) {
queue.add(node.left);
}
else if (value.compareTo(min) < 0) {
queue.add(node.right);
}
}
}
}
}

View file

@ -51,8 +51,8 @@ public class ADS5_3_test {
@Test
public void testCreateText() {
String[] good = textGood.split("\n");
String[] test = textTest.split("\n");
String[] good = textGood.split("[\r\n]*");
String[] test = textTest.split("[\r\n]*");
assertEquals(good.length, test.length, "length");
for (int i = 0; i < good.length;i++) {
assertEquals(good[i], test[i], "rangliste["+i+"]");