| 1 | 1 | class Zipper: |
| 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 | |
| 2 | 7 | # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None) |
| 3 | 8 | @staticmethod |
| 4 | 9 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 10 | # Edge Case: Empty tree passed to from_tree |
| 11 | if tree is None: |
| 12 | return None |
| 13 | return Zipper(tree, []) |
| 14 | |
| 7 | 15 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 16 | # Edge Case: Called on a None zipper |
| 17 | if self.tree is None: |
| 18 | return None |
| 19 | node = self._get_focus_node() |
| 20 | # Edge Case: Focus node is None |
| 21 | if node is None: |
| 22 | return None |
| 23 | return node["value"] |
| 24 | |
| 10 | 25 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 26 | # Edge Case: Called on a None zipper |
| 27 | if self.tree is None: |
| 28 | return None |
| 29 | # Create a new tree with the updated value |
| 30 | new_tree = self._copy_tree() |
| 31 | focus_path = self.path[:] |
| 32 | node = new_tree |
| 33 | # Navigate to the focus node |
| 34 | for direction, parent in focus_path: |
| 35 | if direction == "left": |
| 36 | node = node["left"] |
| 37 | else: # direction == "right" |
| 38 | node = node["right"] |
| 39 | # Update the value |
| 40 | node["value"] = value |
| 41 | return Zipper(new_tree, self.path) |
| 42 | |
| 13 | 43 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 44 | # Edge Case: Called on a None zipper |
| 45 | if self.tree is None: |
| 46 | return None |
| 47 | node = self._get_focus_node() |
| 48 | # Edge Case: Focus node is None |
| 49 | if node is None or node["left"] is None: |
| 50 | return None |
| 51 | # Create a new path with the left move added |
| 52 | new_path = self.path + [("left", self._copy_node(node))] |
| 53 | return Zipper(self.tree, new_path) |
| 54 | |
| 16 | 55 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 56 | # Edge Case: Called on a None zipper |
| 57 | if self.tree is None: |
| 58 | return None |
| 59 | # Create a new tree with the updated left subtree |
| 60 | new_tree = self._copy_tree() |
| 61 | focus_path = self.path[:] |
| 62 | node = new_tree |
| 63 | # Navigate to the focus node |
| 64 | for direction, parent in focus_path: |
| 65 | if direction == "left": |
| 66 | node = node["left"] |
| 67 | else: # direction == "right" |
| 68 | node = node["right"] |
| 69 | # Update the left subtree |
| 70 | node["left"] = tree |
| 71 | return Zipper(new_tree, self.path) |
| 72 | |
| 19 | 73 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 74 | # Edge Case: Called on a None zipper |
| 75 | if self.tree is None: |
| 76 | return None |
| 77 | node = self._get_focus_node() |
| 78 | # Edge Case: Focus node is None |
| 79 | if node is None or node["right"] is None: |
| 80 | return None |
| 81 | # Create a new path with the right move added |
| 82 | new_path = self.path + [("right", self._copy_node(node))] |
| 83 | return Zipper(self.tree, new_path) |
| 84 | |
| 22 | 85 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 86 | # Edge Case: Called on a None zipper |
| 87 | if self.tree is None: |
| 88 | return None |
| 89 | # Create a new tree with the updated right subtree |
| 90 | new_tree = self._copy_tree() |
| 91 | focus_path = self.path[:] |
| 92 | node = new_tree |
| 93 | # Navigate to the focus node |
| 94 | for direction, parent in focus_path: |
| 95 | if direction == "left": |
| 96 | node = node["left"] |
| 97 | else: # direction == "right" |
| 98 | node = node["right"] |
| 99 | # Update the right subtree |
| 100 | node["right"] = tree |
| 101 | return Zipper(new_tree, self.path) |
| 102 | |
| 25 | 103 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 104 | # Edge Case: Called on a None zipper |
| 105 | if self.tree is None: |
| 106 | return None |
| 107 | # Edge Case: Already at the root (no path) |
| 108 | if not self.path: |
| 109 | return None |
| 110 | # Create a new path without the last move |
| 111 | new_path = self.path[:-1] |
| 112 | return Zipper(self.tree, new_path) |
| 113 | |
| 28 | 114 | def to_tree(self) -> dict: |
| 29 | | pass |
| 115 | # Edge Case: Called on a None zipper |
| 116 | if self.tree is None: |
| 117 | return None |
| 118 | return self.tree |
| 119 | |
| 120 | def _get_focus_node(self): |
| 121 | # Helper method to get the focus node based on the path |
| 122 | node = self.tree |
| 123 | for direction, parent in self.path: |
| 124 | if direction == "left": |
| 125 | node = node["left"] |
| 126 | else: # direction == "right" |
| 127 | node = node["right"] |
| 128 | return node |
| 129 | |
| 130 | def _copy_tree(self): |
| 131 | # Helper method to create a deep copy of the tree |
| 132 | def copy_node(node): |
| 133 | if node is None: |
| 134 | return None |
| 135 | return { |
| 136 | "value": node["value"], |
| 137 | "left": copy_node(node["left"]), |
| 138 | "right": copy_node(node["right"]) |
| 139 | } |
| 140 | return copy_node(self.tree) |
| 141 | |
| 142 | def _copy_node(self, node): |
| 143 | # Helper method to create a copy of a node without its children |
| 144 | if node is None: |
| 145 | return None |
| 146 | return { |
| 147 | "value": node["value"], |
| 148 | "left": node["left"], |
| 149 | "right": node["right"] |
| 150 | } |
| 151 | # Handled Edge Cases: Empty tree passed to from_tree, Called on a None zipper, Focus node is None, Already at the root (no path) |