Complete the code to perform a single right rotation on a node in an AVL tree.
def right_rotate([1]): y = [1].left T2 = y.right y.right = [1] [1].left = T2 return y
The function takes a node as input and performs a right rotation around it. The variable node is used consistently to refer to the node being rotated.
Complete the code to perform a single left rotation on a node in an AVL tree.
def left_rotate([1]): y = [1].right T2 = y.left y.left = [1] [1].right = T2 return y
The function rotates the given node to the left by adjusting pointers accordingly.
Fix the error in the code that performs a left-right rotation in an AVL tree.
def left_right_rotate([1]): [1].left = left_rotate([1].left) return right_rotate([1])
The parameter node is used consistently to perform the left-right rotation by first rotating the left child to the left, then the node to the right.
Fill both blanks to complete the right-left rotation function in an AVL tree.
def right_left_rotate([1]): [1].right = [2]([1].right) return left_rotate([1])
The function performs a right-left rotation by first rotating the right child to the left, then rotating the node to the left.
Fill all three blanks to complete the balance factor calculation and rotation decision in an AVL tree node insertion.
def insert([1], key): if not [1]: return Node(key) if key < [1].key: [1].left = insert([1].left, key) else: [1].right = insert([1].right, key) balance = height([1].left) - height([1].right) if balance > 1 and key < [1].left.key: return right_rotate([1]) if balance < -1 and key > [1].right.key: return left_rotate([1]) if balance > 1 and key > [1].left.key: [1].left = left_rotate([1].left) return right_rotate([1]) if balance < -1 and key < [1].right.key: [1].right = right_rotate([1].right) return left_rotate([1]) return [1]
The parameter node is used consistently to refer to the current node in the AVL tree during insertion and balancing.