简体   繁体   English

如何在java中获取avl树的高度

[英]how to get the height of avl tree in java

i tried this function我试过这个功能

private int height(AVLNode t )
{
    return t == null ? -1 : t.height;
}

i don't know what that method do can anyone explain it ?我不知道这种方法有什么作用,谁能解释一下?

The default approach is to use recursion to determine the height.默认方法是使用递归来确定高度。

private int height(AVLNode t) {
    return t == null ? -1 : 1 + Math.max(height(t.left), height(t.right));
}

It returns the height of the AVLNode.它返回 AVLNode 的高度。 If the AVLNode is null it returns -1.如果 AVLNode 为空,则返回 -1。 If the AVLNode is not null it returns the height of the AVLNode.如果 AVLNode 不为空,则返回 AVLNode 的高度。

The method returns height of the AVLNode , -1 otherwise.该方法返回AVLNode高度,否则返回 -1。 The line return t == null ? -1 : t.height该行return t == null ? -1 : t.height return t == null ? -1 : t.height is a ternary operator return t == null ? -1 : t.height是一个三元运算符

Is equivalent to相当于

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM