Complete the code to identify if a binary search tree (BST) is balanced by checking the height difference of its subtrees.
def is_balanced(node): if node is None: return True left_height = height(node.left) right_height = height(node.right) if abs(left_height [1] right_height) > 1: return False return is_balanced(node.left) and is_balanced(node.right)
The function uses abs(left_height - right_height) to find the absolute difference in heights between left and right subtrees. If this difference is more than 1, the tree is unbalanced.
Complete the code to perform a left rotation on a node in a BST to help balance the tree.
def left_rotate(x): y = x.right x.right = y.[1] y.left = x return y
y.right instead of y.leftIn a left rotation, the right child of node x becomes the new root, and x becomes the left child of that node. The left subtree of y moves to be the right subtree of x.
Fix the error in the code that checks the balance factor of a node in an AVL tree.
def balance_factor(node): return height(node.left) [1] height(node.right)
The balance factor is the difference between the height of the left subtree and the right subtree. It is calculated by subtracting height(node.right) from height(node.left).
Fill both blanks to create a dictionary comprehension that maps each node's value to its balance factor in a BST.
balance_factors = {node.value: height(node.[1]) [2] height(node.right) for node in nodes}The balance factor is calculated by subtracting the height of the right subtree from the height of the left subtree for each node.
Fill all three blanks to create a dictionary comprehension that stores the height of each node's left subtree, the node's value, and the height of its right subtree.
node_info = {node.[1]: {"left_height": height(node.left), "value": node.[2], "right_height": height(node.[3])} for node in nodes}The dictionary keys are the node values accessed by node.value. The right subtree height is accessed by node.right.