英文:
AVL: Right rotation, updating heights
问题
我不理解为什么我们只需要更改这些高度。
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.
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)).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论