Refactor method for calculating height

This commit is contained in:
Manuel Thalmann 2022-10-31 21:04:55 +01:00
parent 0a4895c538
commit 63da19757e

View file

@ -74,14 +74,19 @@ public class SortedBinaryTree<T extends Comparable<T>> implements Tree<T> {
} }
protected int calcHeight(TreeNode<T> node) { protected int calcHeight(TreeNode<T> node) {
int result = 0; if (node == null) {
return 0;
} else {
int result = 1;
if (node.left != null || node.right != null) { if (node.left != null || node.right != null) {
result = Stream.of(node.left, node.right).filter( result += Stream.of(node.left, node.right).filter(
(innerNode) -> innerNode != null).mapToInt((innerNode) -> calcHeight(innerNode)).max().getAsInt(); (innerNode) -> innerNode != null).mapToInt((innerNode) -> calcHeight(innerNode)).max()
.getAsInt();
} }
return result + 1; return result;
}
} }
public int height() { public int height() {