Complete the code to find the left child of a node in a BST.
def left_child(node): return node.[1]
The left child of a node is accessed using node.left.
Complete the code to find the minimum node in a BST subtree.
def find_min(node): current = node while current.[1] is not None: current = current.left return current
To find the minimum node, we keep going to the left child until it is None.
Fix the error in the code to find the inorder successor when the node has no right child.
def inorder_successor(node): if node.right: return find_min(node.right) parent = node.[1] while parent and node == parent.right: node = parent parent = parent.parent return parent
To move up the tree, we use the parent attribute.
Fill both blanks to complete the code that finds the inorder successor of a node in a BST.
def inorder_successor(node): if node.right: return find_min(node.[1]) parent = node.[2] while parent and node == parent.right: node = parent parent = parent.parent return parent
The successor is the minimum node in the right subtree, so use right. If no right child, move up using parent.
Fill all three blanks to complete the code that returns the inorder successor's value or None if no successor.
def inorder_successor_value(node): successor = None if node.[1]: successor = find_min(node.[2]) else: parent = node.[3] while parent and node == parent.right: node = parent parent = parent.parent successor = parent return successor.value if successor else None
If the node has a right child, the successor is the minimum node in the right subtree (right). Otherwise, move up using parent.
