如何在 java 中获取 avl 树的高度
how to get the height of avl tree in java
我试过这个功能
private int height(AVLNode t )
{
return t == null ? -1 : t.height;
}
我不知道那个方法有什么用谁能解释一下吗?
它returns AVLNode 的高度。如果 AVLNode 为 null,则它 returns -1。如果 AVLNode 不为空,它 returns AVLNode 的高度。
方法returns AVLNode
的高度,否则为-1。 return t == null ? -1 : t.height
行是 ternary operator
相当于
if (t == null) {
return -1
} else {
return t.height
}
默认方法是使用递归来确定高度。
private int height(AVLNode t) {
return t == null ? -1 : 1 + Math.max(height(t.left), height(t.right));
}
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));
}
我试过这个功能
private int height(AVLNode t )
{
return t == null ? -1 : t.height;
}
我不知道那个方法有什么用谁能解释一下吗?
它returns AVLNode 的高度。如果 AVLNode 为 null,则它 returns -1。如果 AVLNode 不为空,它 returns AVLNode 的高度。
方法returns AVLNode
的高度,否则为-1。 return t == null ? -1 : t.height
行是 ternary operator
相当于
if (t == null) {
return -1
} else {
return t.height
}
默认方法是使用递归来确定高度。
private int height(AVLNode t) {
return t == null ? -1 : 1 + Math.max(height(t.left), height(t.right));
}
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));
}