我正在处理一项要求我实现 AVL 树的任务。我很确定我的轮换方法是正确的,但我无法确定何时使用它们。
例如,书中的解释说我应该爬上与插入节点/元素相同的路径。但是,我不能有任何父指针。
最新代码:
public BinaryNode<T> insert(BinaryNode<T> node) {
if (this.getElement().compareTo(node.getElement()) > 0) {
if (this.getLeftChild() != null) {
BinaryNode<T> b = this.getLeftChild().insert(node);
if(!this.isBalanced()) {
this.balance();
}
return b;
} else {
this.setLeftChild(node);
}
} else if (this.getElement().compareTo(node.getElement()) < 0) {
if (this.getRightChild() != null) {
return this.getRightChild().insert(node);
} else {
this.setRightChild(node);
}
}
return this;
}
我在这里要做的是爬回树上,但它只能在插入节点后检查平衡。因此,这在 else 子句中。
我还尝试将余额代码放在 R Samuel Klatchko 建议的位置,但检查了每个插件的余额。例如:如果连续插入 7、9、5、3 和 1,尝试插入 1 时会出现空指针异常。
编辑:上述的一个原因可能与我做高度的方式有关。如果我每次都用 height() 计算高度,但它会破坏 AVL 树的 O(log(n)) 时间,那么它在一次右旋转时效果很好。
关于如何做到这一点的任何想法?