我很难理解为什么下面的树旋转代码有效。如果T2
指向y.left
和y.left
指向x
,这不是使最后一个赋值x.right = T2
等于x.right = x
吗?指针不应该指向初始T2
吗?
Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;
// Perform rotation
y.left = x;
x.right = T2;
// Update heights
x.height = max(height(x.left), height(x.right)) + 1;
y.height = max(height(y.left), height(y.right)) + 1;
// Return new root
return y;
}