def rotate_right(root, y):
    """Apply right rotation on y, and return the new root."""
    assert root and y and y.left
    x = y.left
    y.left = x.right
    if y.left:
        y.left.parent = y
    x.right = y
    x.parent = y.parent
    y.parent = x
    if not x.parent:
        root = x
    elif x.parent.right == y:
        x.parent.right = x
    elif x.parent.left == y:
        x.parent.left = x
    return root
