AVL:右旋,更新高度

huangapple go评论121阅读模式
英文:

AVL: Right rotation, updating heights

问题

我不理解为什么我们只需要更改这些高度。

例如在这里:
AVL:右旋,更新高度

8、9和11都更改了高度,但根据实现,我理解只会通知9和11的高度。

英文:

I am reading a tutorial in AVL trees https://www.programiz.com/dsa/avl-tree
In the implementation of right rotation
https://www.programiz.com/dsa/avl-tree # Function to perform right rotation

def rightRotate(self, z):
    y = z.left
    T3 = y.right
    y.right = z
    z.left = T3
    z.height = 1 + max(self.getHeight(z.left),
                       self.getHeight(z.right))
    y.height = 1 + max(self.getHeight(y.left),
                       self.getHeight(y.right))
    return y

I do not understand why we need to change only these heights.

For example here:
AVL:右旋,更新高度

Both 8 , 9 and 11 changed heights but according to the implementation only 9's and 11's heights will be informed as I understand.

答案1

得分: 1

左子树和右子树在您的示例中的节点(节点8)将不受右旋影响,因此该节点的高度不需要更改。(因为节点的高度等于1 + 最大(左子树的高度,右子树的高度))。

英文:

The left subtree and the right subtree of the node in your example (node 8) will not be affected by the right rotation، and so the height of that node will not need to change. (Because the height of a node is equal to 1 + max(height of left subtree, height of right subtree)).

huangapple
  • 本文由 发表于 2023年5月8日 01:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定