Add methods for calculating size and height
This commit is contained in:
parent
13b54d1e88
commit
630a5c8acb
|
@ -1,5 +1,7 @@
|
||||||
package ch.nuth.zhaw.exbox;
|
package ch.nuth.zhaw.exbox;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
||||||
protected TreeNode<T> root;
|
protected TreeNode<T> root;
|
||||||
|
|
||||||
|
@ -72,8 +74,14 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int calcHeight(TreeNode<T> node) {
|
protected int calcHeight(TreeNode<T> node) {
|
||||||
// TODO Implement
|
int result = 0;
|
||||||
return 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() {
|
public int height() {
|
||||||
|
@ -81,8 +89,17 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int calcSize(TreeNode<T> p) {
|
protected int calcSize(TreeNode<T> p) {
|
||||||
// TODO Implement
|
int result = 1;
|
||||||
return 0;
|
|
||||||
|
if (p.left != null) {
|
||||||
|
result += calcSize(p.left);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p.right != null) {
|
||||||
|
result += calcSize(p.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
|
|
|
@ -51,8 +51,8 @@ public class ADS5_3_test {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateText() {
|
public void testCreateText() {
|
||||||
String[] good = textGood.split("\n");
|
String[] good = textGood.split("[\r\n]*");
|
||||||
String[] test = textTest.split("\n");
|
String[] test = textTest.split("[\r\n]*");
|
||||||
assertEquals(good.length, test.length, "length");
|
assertEquals(good.length, test.length, "length");
|
||||||
for (int i = 0; i < good.length;i++) {
|
for (int i = 0; i < good.length;i++) {
|
||||||
assertEquals(good[i], test[i], "rangliste["+i+"]");
|
assertEquals(good[i], test[i], "rangliste["+i+"]");
|
||||||
|
|
Loading…
Reference in a new issue