| 1 | 1 | class Zipper: |
| 2 | | # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None) |
| 2 | """ |
| 3 | A zipper for navigating and manipulating a binary tree. |
| 4 | |
| 5 | The zipper maintains a focus on a specific node in the tree, allowing |
| 6 | navigation and modification operations while preserving immutability. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, path): |
| 10 | """ |
| 11 | Initialize a Zipper instance. |
| 12 | |
| 13 | Args: |
| 14 | tree (dict): The tree structure |
| 15 | path (list): Path from root to current focus node |
| 16 | """ |
| 17 | self.tree = tree |
| 18 | self.path = path |
| 19 | |
| 3 | 20 | @staticmethod |
| 4 | 21 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 22 | """ |
| 23 | Create a zipper from a binary tree. |
| 24 | |
| 25 | Args: |
| 26 | tree (dict): A binary tree with keys "value", "left", and "right" |
| 27 | |
| 28 | Returns: |
| 29 | Zipper: A new zipper focused on the root of the tree |
| 30 | """ |
| 31 | # Edge Case: Empty tree |
| 32 | if tree is None: |
| 33 | return None |
| 34 | return Zipper(tree, []) |
| 35 | |
| 36 | def _get_focus_node(self): |
| 37 | """ |
| 38 | Get the current focus node by following the path from root. |
| 39 | |
| 40 | Returns: |
| 41 | dict: The focus node |
| 42 | """ |
| 43 | node = self.tree |
| 44 | for direction, parent in self.path: |
| 45 | node = node[direction] |
| 46 | return node |
| 47 | |
| 7 | 48 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 49 | """ |
| 50 | Get the value of the focus node. |
| 51 | |
| 52 | Returns: |
| 53 | int: The value of the focus node |
| 54 | """ |
| 55 | return self._get_focus_node()["value"] |
| 56 | |
| 10 | 57 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 58 | """ |
| 59 | Set the value of the focus node. |
| 60 | |
| 61 | Args: |
| 62 | value (int): The new value |
| 63 | |
| 64 | Returns: |
| 65 | Zipper: A new zipper with the updated value |
| 66 | """ |
| 67 | # Edge Case: None value |
| 68 | if value is None: |
| 69 | raise ValueError("Value cannot be None") |
| 70 | |
| 71 | new_tree = self._copy_tree() |
| 72 | focus = new_tree |
| 73 | |
| 74 | for direction, parent in self.path: |
| 75 | focus = focus[direction] |
| 76 | |
| 77 | focus["value"] = value |
| 78 | |
| 79 | return Zipper(new_tree, self.path) |
| 80 | |
| 13 | 81 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 82 | """ |
| 83 | Move focus to the left child of the current node. |
| 84 | |
| 85 | Returns: |
| 86 | Zipper | None: A new zipper focused on the left child, or None if no left child |
| 87 | """ |
| 88 | focus = self._get_focus_node() |
| 89 | |
| 90 | # Edge Case: No left child |
| 91 | if focus["left"] is None: |
| 92 | return None |
| 93 | |
| 94 | new_path = self.path + [("left", focus)] |
| 95 | return Zipper(self.tree, new_path) |
| 96 | |
| 16 | 97 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 98 | """ |
| 99 | Set the left child of the focus node. |
| 100 | |
| 101 | Args: |
| 102 | tree (dict | None): The new left subtree |
| 103 | |
| 104 | Returns: |
| 105 | Zipper: A new zipper with the updated left child |
| 106 | """ |
| 107 | new_tree = self._copy_tree() |
| 108 | focus = new_tree |
| 109 | |
| 110 | for direction, parent in self.path: |
| 111 | focus = focus[direction] |
| 112 | |
| 113 | focus["left"] = tree |
| 114 | |
| 115 | return Zipper(new_tree, self.path) |
| 116 | |
| 19 | 117 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 118 | """ |
| 119 | Move focus to the right child of the current node. |
| 120 | |
| 121 | Returns: |
| 122 | Zipper | None: A new zipper focused on the right child, or None if no right child |
| 123 | """ |
| 124 | focus = self._get_focus_node() |
| 125 | |
| 126 | # Edge Case: No right child |
| 127 | if focus["right"] is None: |
| 128 | return None |
| 129 | |
| 130 | new_path = self.path + [("right", focus)] |
| 131 | return Zipper(self.tree, new_path) |
| 132 | |
| 22 | 133 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 134 | """ |
| 135 | Set the right child of the focus node. |
| 136 | |
| 137 | Args: |
| 138 | tree (dict | None): The new right subtree |
| 139 | |
| 140 | Returns: |
| 141 | Zipper: A new zipper with the updated right child |
| 142 | """ |
| 143 | new_tree = self._copy_tree() |
| 144 | focus = new_tree |
| 145 | |
| 146 | for direction, parent in self.path: |
| 147 | focus = focus[direction] |
| 148 | |
| 149 | focus["right"] = tree |
| 150 | |
| 151 | return Zipper(new_tree, self.path) |
| 152 | |
| 25 | 153 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 154 | """ |
| 155 | Move focus to the parent of the current node. |
| 156 | |
| 157 | Returns: |
| 158 | Zipper | None: A new zipper focused on the parent, or None if at root |
| 159 | """ |
| 160 | # Edge Case: Already at root |
| 161 | if not self.path: |
| 162 | return None |
| 163 | |
| 164 | new_path = self.path[:-1] |
| 165 | return Zipper(self.tree, new_path) |
| 166 | |
| 28 | 167 | def to_tree(self) -> dict: |
| 29 | | pass |
| 168 | """ |
| 169 | Get the complete tree from the zipper. |
| 170 | |
| 171 | Returns: |
| 172 | dict: The complete tree |
| 173 | """ |
| 174 | return self.tree |
| 175 | |
| 176 | def _copy_tree(self): |
| 177 | """ |
| 178 | Create a deep copy of the tree. |
| 179 | |
| 180 | Returns: |
| 181 | dict: A deep copy of the tree |
| 182 | """ |
| 183 | def _deep_copy(node): |
| 184 | if node is None: |
| 185 | return None |
| 186 | return { |
| 187 | "value": node["value"], |
| 188 | "left": _deep_copy(node["left"]), |
| 189 | "right": _deep_copy(node["right"]) |
| 190 | } |
| 191 | |
| 192 | return _deep_copy(self.tree) |
| 193 | |
| 194 | # Handled Edge Cases: Empty tree in from_tree, None value in set_value, No left child in left, No right child in right, Already at root in up |