[英]how to get the height of avl tree in java
我试过这个功能
private int height(AVLNode t )
{
return t == null ? -1 : t.height;
}
我不知道这种方法有什么作用,谁能解释一下?
默认方法是使用递归来确定高度。
private int height(AVLNode t) {
return t == null ? -1 : 1 + Math.max(height(t.left), height(t.right));
}
它返回 AVLNode 的高度。 如果 AVLNode 为空,则返回 -1。 如果 AVLNode 不为空,则返回 AVLNode 的高度。
该方法返回AVLNode
高度,否则返回 -1。 该行return t == null ? -1 : t.height
return t == null ? -1 : t.height
是一个三元运算符
相当于
if (t == null) {
return -1
} else {
return t.height
}
private int height(AVLNode t) {
if (t == null) {
return 0;
}
return 1 + Math.max((t.getLeft() != null ? t.getLeft().getHeight() : -1),
(t.getRight() != null ? t.getRight().getHeight() : -1));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.