| 1 | 1 | class Zipper: |
| 2 | | # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None) |
| 2 | def __init__(self, tree, path): |
| 3 | # Initialize a Zipper with a tree and a path |
| 4 | self.tree = tree |
| 5 | self.path = path |
| 6 | |
| 7 | # Edge Case: Handle empty tree by returning an empty Zipper |
| 3 | 8 | @staticmethod |
| 4 | 9 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 10 | return Zipper(tree, []) |
| 11 | |
| 12 | # Edge Case: Return the value of the current focus node |
| 7 | 13 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 14 | return self.tree["value"] |
| 15 | |
| 16 | # Edge Case: Create a new Zipper with updated value in the focus node |
| 10 | 17 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 18 | new_tree = {"value": value, "left": self.tree["left"], "right": self.tree["right"]} |
| 19 | return Zipper(new_tree, self.path) |
| 20 | |
| 21 | # Edge Case: Navigate to the left child, return None if no left child exists |
| 13 | 22 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 23 | if self.tree["left"] is None: |
| 24 | return None |
| 25 | # Store breadcrumb to navigate back up |
| 26 | new_path = self.path + [{"parent": self.tree, "side": "left"}] |
| 27 | return Zipper(self.tree["left"], new_path) |
| 28 | |
| 29 | # Edge Case: Set the left child of the focus node and return a new Zipper |
| 16 | 30 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 31 | new_tree = {"value": self.tree["value"], "left": tree, "right": self.tree["right"]} |
| 32 | return Zipper(new_tree, self.path) |
| 33 | |
| 34 | # Edge Case: Navigate to the right child, return None if no right child exists |
| 19 | 35 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 36 | if self.tree["right"] is None: |
| 37 | return None |
| 38 | # Store breadcrumb to navigate back up |
| 39 | new_path = self.path + [{"parent": self.tree, "side": "right"}] |
| 40 | return Zipper(self.tree["right"], new_path) |
| 41 | |
| 42 | # Edge Case: Set the right child of the focus node and return a new Zipper |
| 22 | 43 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 44 | new_tree = {"value": self.tree["value"], "left": self.tree["left"], "right": tree} |
| 45 | return Zipper(new_tree, self.path) |
| 46 | |
| 47 | # Edge Case: Navigate up to the parent node, return None if already at root |
| 25 | 48 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 49 | if not self.path: |
| 50 | return None |
| 51 | |
| 52 | # Get the last breadcrumb |
| 53 | parent_info = self.path[-1] |
| 54 | parent = parent_info["parent"] |
| 55 | side = parent_info["side"] |
| 56 | |
| 57 | # Create a copy of the parent with the current tree in the correct position |
| 58 | new_parent = { |
| 59 | "value": parent["value"], |
| 60 | "left": self.tree if side == "left" else parent["left"], |
| 61 | "right": self.tree if side == "right" else parent["right"] |
| 62 | } |
| 63 | |
| 64 | # Return a new Zipper with the parent as the focus and a shortened path |
| 65 | return Zipper(new_parent, self.path[:-1]) |
| 66 | |
| 67 | # Edge Case: Reconstruct the entire tree from the current Zipper state |
| 28 | 68 | def to_tree(self) -> dict: |
| 29 | | pass |
| 69 | # Navigate to the root to get the complete tree |
| 70 | current = self |
| 71 | while True: |
| 72 | parent = current.up() |
| 73 | if parent is None: |
| 74 | break |
| 75 | current = parent |
| 76 | return current.tree |
| 77 | |
| 78 | # Handled Edge Cases: Empty tree, value retrieval, setting values, navigating left/right children, setting children, navigating up from root, reconstructing the tree |