From e2dcba16004832916c3b4c76c37bd1fd14e7e2c3 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 3 Jun 2026 16:00:09 -0700 Subject: [PATCH] Add homework for 2026.05.27 --- notes-and-examples/2026.05.27/homework.py | 60 ++++++++++++++++++++--- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/notes-and-examples/2026.05.27/homework.py b/notes-and-examples/2026.05.27/homework.py index f35a66f..c1fb5c9 100644 --- a/notes-and-examples/2026.05.27/homework.py +++ b/notes-and-examples/2026.05.27/homework.py @@ -42,20 +42,55 @@ class Node: - def __init__(self, value, left=None, right=None): + def __init__(self, value, left=None, right=None, parent=None): self.value = value self.left: Node | None = left self.right: Node | None = right + # required to re-set the child later + self.parent: Node | None = parent + def rotate_left(node: Node) -> Node: - # TODO - return Node(0) + if node.right is None: + return node + + original_parent = node.parent + new_parent = node.right + new_right_child = node.right.left + + node.right = new_right_child + node.parent = new_parent + new_parent.left = node + + if original_parent is not None: + if original_parent.left == node: + original_parent.left = new_parent + elif original_parent.right == node: + original_parent.right = new_parent + + return new_parent def rotate_right(node: Node) -> Node: - # TODO - return Node(0) + if node.left is None: + return node + + original_parent = node.parent + new_parent = node.left + new_left_child = node.left.right + + node.left = new_left_child + node.parent = new_parent + new_parent.right = node + + if original_parent is not None: + if original_parent.left == node: + original_parent.left = new_parent + elif original_parent.right == node: + original_parent.right = new_parent + + return new_parent # --- Tests --------------------------------------------------------------- @@ -67,7 +102,12 @@ def to_tuple(node: Node | None): """Turn a tree into nested (value, left, right) tuples for easy comparison.""" if node is None: return None - return (node.value, to_tuple(node.left), to_tuple(node.right)) + return ( + node.value, + to_tuple(node.left), + to_tuple(node.right), + node.parent.value if node.parent is not None else None, + ) def trees_equal(a: Node | None, b: Node | None) -> bool: @@ -110,12 +150,20 @@ if __name__ == "__main__": expected = Node(1, left=Node(0), right=Node(3, left=Node(2), right=Node(4))) return rotate_right(rotate_left(original)), expected + def test_rotate_with_parent(): + # run it on 33 instead of the root + root = Node(16, right=Node(33, left=Node(22), right=Node(55))) + original = root.right + expected = Node(55, left=Node(33, left=Node(22))) + return rotate_left(original), expected # pyright: ignore[reportArgumentType] + tests = [ ("rotate_right on worksheet example (10)", test_rotate_right_example), ("rotate_left on worksheet example (16)", test_rotate_left_example), ("rotate_right on two nodes", test_rotate_right_two_nodes), ("rotate_left on two nodes", test_rotate_left_two_nodes), ("rotate_left then rotate_right restores the tree", test_round_trip), + ("rotate_left works when there is a parent", test_rotate_with_parent), ] passed = 0