Add homework for 2026.05.27

This commit is contained in:
2026-06-03 16:00:09 -07:00
parent 093e2e980d
commit e2dcba1600

View File

@@ -42,20 +42,55 @@
class Node: class Node:
def __init__(self, value, left=None, right=None): def __init__(self, value, left=None, right=None, parent=None):
self.value = value self.value = value
self.left: Node | None = left self.left: Node | None = left
self.right: Node | None = right self.right: Node | None = right
# required to re-set the child later
self.parent: Node | None = parent
def rotate_left(node: Node) -> Node: def rotate_left(node: Node) -> Node:
# TODO if node.right is None:
return Node(0) 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: def rotate_right(node: Node) -> Node:
# TODO if node.left is None:
return Node(0) 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 --------------------------------------------------------------- # --- Tests ---------------------------------------------------------------
@@ -67,7 +102,12 @@ def to_tuple(node: Node | None):
"""Turn a tree into nested (value, left, right) tuples for easy comparison.""" """Turn a tree into nested (value, left, right) tuples for easy comparison."""
if node is None: if node is None:
return 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: 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))) expected = Node(1, left=Node(0), right=Node(3, left=Node(2), right=Node(4)))
return rotate_right(rotate_left(original)), expected 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 = [ tests = [
("rotate_right on worksheet example (10)", test_rotate_right_example), ("rotate_right on worksheet example (10)", test_rotate_right_example),
("rotate_left on worksheet example (16)", test_rotate_left_example), ("rotate_left on worksheet example (16)", test_rotate_left_example),
("rotate_right on two nodes", test_rotate_right_two_nodes), ("rotate_right on two nodes", test_rotate_right_two_nodes),
("rotate_left on two nodes", test_rotate_left_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 then rotate_right restores the tree", test_round_trip),
("rotate_left works when there is a parent", test_rotate_with_parent),
] ]
passed = 0 passed = 0