| 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 | A zipper contains a data structure and a pointer (focus) into that structure. |
| 6 | It supports moving through the tree and modifying it in a functional way. |
| 7 | |
| 8 | Attributes: |
| 9 | _focus (dict): The current node that is focused. |
| 10 | _path (list): A list of tuples representing the path from root to focus. |
| 11 | Each tuple contains (parent_node, direction) where direction is 'left' or 'right'. |
| 12 | _root (dict): Reference to the root of the tree for to_tree operation. |
| 13 | """ |
| 14 | |
| 15 | def __init__(self, focus, path=None, root=None): |
| 16 | """ |
| 17 | Initialize a Zipper. |
| 18 | |
| 19 | Args: |
| 20 | focus (dict): The node that is currently focused. |
| 21 | path (list): Path from root to focus. |
| 22 | root (dict): Reference to the root of the tree. |
| 23 | """ |
| 24 | self._focus = focus |
| 25 | self._path = path or [] |
| 26 | self._root = root or focus |
| 27 | |
| 3 | 28 | @staticmethod |
| 4 | 29 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 30 | """ |
| 31 | Create a zipper from a binary tree. |
| 32 | |
| 33 | Args: |
| 34 | tree (dict): A binary tree represented as a dict with keys "value", "left", and "right". |
| 35 | Each node is a dict or None. |
| 36 | |
| 37 | Returns: |
| 38 | Zipper: A new zipper focused on the root of the tree. |
| 39 | |
| 40 | Edge Case: Empty tree (None) - returns None |
| 41 | """ |
| 42 | # Edge Case: Empty tree |
| 43 | if tree is None: |
| 44 | return None |
| 45 | return Zipper(tree) |
| 46 | |
| 7 | 47 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 48 | """ |
| 49 | Get the value of the focus node. |
| 50 | |
| 51 | Returns: |
| 52 | int: The value of the focused node. |
| 53 | |
| 54 | Edge Case: Focus is None - This shouldn't happen with proper usage but we check anyway |
| 55 | """ |
| 56 | # Edge Case: Focus is None |
| 57 | if self._focus is None: |
| 58 | return None |
| 59 | return self._focus["value"] |
| 60 | |
| 10 | 61 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 62 | """ |
| 63 | Set the value of the focus node, returning a new zipper. |
| 64 | |
| 65 | Args: |
| 66 | value (int): The new value for the focus node. |
| 67 | |
| 68 | Returns: |
| 69 | Zipper: A new zipper with the updated value. |
| 70 | |
| 71 | Edge Case: Focus is None - Returns a copy of the current zipper |
| 72 | """ |
| 73 | # Edge Case: Focus is None |
| 74 | if self._focus is None: |
| 75 | return Zipper(self._focus, self._path[:], self._root) |
| 76 | |
| 77 | new_focus = { |
| 78 | "value": value, |
| 79 | "left": self._focus["left"], |
| 80 | "right": self._focus["right"] |
| 81 | } |
| 82 | |
| 83 | # Reconstruct the tree with the new focus |
| 84 | new_root = self._reconstruct_tree(new_focus) |
| 85 | |
| 86 | # Find the new focus in the reconstructed tree |
| 87 | new_path = self._path[:] |
| 88 | new_focus_in_new_tree = new_root |
| 89 | for parent, direction in new_path: |
| 90 | new_focus_in_new_tree = new_focus_in_new_tree[direction] |
| 91 | |
| 92 | return Zipper(new_focus_in_new_tree, new_path, new_root) |
| 93 | |
| 13 | 94 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 95 | """ |
| 96 | Move the focus to the left child, returning a new zipper. |
| 97 | |
| 98 | Returns: |
| 99 | Zipper | None: A new zipper focused on the left child, or None if no left child exists. |
| 100 | |
| 101 | Edge Case: Focus is None - Returns None |
| 102 | Edge Case: No left child - Returns None |
| 103 | """ |
| 104 | # Edge Case: Focus is None |
| 105 | if self._focus is None: |
| 106 | return None |
| 107 | |
| 108 | # Edge Case: No left child |
| 109 | if self._focus["left"] is None: |
| 110 | return None |
| 111 | |
| 112 | new_path = self._path[:] + [(self._focus, "left")] |
| 113 | return Zipper(self._focus["left"], new_path, self._root) |
| 114 | |
| 16 | 115 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 116 | """ |
| 117 | Set the left child of the focus node, returning a new zipper. |
| 118 | |
| 119 | Args: |
| 120 | tree (dict | None): The new left subtree. |
| 121 | |
| 122 | Returns: |
| 123 | Zipper: A new zipper with the updated left child. |
| 124 | |
| 125 | Edge Case: Focus is None - Returns a copy of the current zipper |
| 126 | """ |
| 127 | # Edge Case: Focus is None |
| 128 | if self._focus is None: |
| 129 | return Zipper(self._focus, self._path[:], self._root) |
| 130 | |
| 131 | new_focus = { |
| 132 | "value": self._focus["value"], |
| 133 | "left": tree, |
| 134 | "right": self._focus["right"] |
| 135 | } |
| 136 | |
| 137 | # Reconstruct the tree with the new focus |
| 138 | new_root = self._reconstruct_tree(new_focus) |
| 139 | |
| 140 | # Find the new focus in the reconstructed tree |
| 141 | new_path = self._path[:] |
| 142 | new_focus_in_new_tree = new_root |
| 143 | for parent, direction in new_path: |
| 144 | new_focus_in_new_tree = new_focus_in_new_tree[direction] |
| 145 | |
| 146 | return Zipper(new_focus_in_new_tree, new_path, new_root) |
| 147 | |
| 19 | 148 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 149 | """ |
| 150 | Move the focus to the right child, returning a new zipper. |
| 151 | |
| 152 | Returns: |
| 153 | Zipper | None: A new zipper focused on the right child, or None if no right child exists. |
| 154 | |
| 155 | Edge Case: Focus is None - Returns None |
| 156 | Edge Case: No right child - Returns None |
| 157 | """ |
| 158 | # Edge Case: Focus is None |
| 159 | if self._focus is None: |
| 160 | return None |
| 161 | |
| 162 | # Edge Case: No right child |
| 163 | if self._focus["right"] is None: |
| 164 | return None |
| 165 | |
| 166 | new_path = self._path[:] + [(self._focus, "right")] |
| 167 | return Zipper(self._focus["right"], new_path, self._root) |
| 168 | |
| 22 | 169 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 170 | """ |
| 171 | Set the right child of the focus node, returning a new zipper. |
| 172 | |
| 173 | Args: |
| 174 | tree (dict | None): The new right subtree. |
| 175 | |
| 176 | Returns: |
| 177 | Zipper: A new zipper with the updated right child. |
| 178 | |
| 179 | Edge Case: Focus is None - Returns a copy of the current zipper |
| 180 | """ |
| 181 | # Edge Case: Focus is None |
| 182 | if self._focus is None: |
| 183 | return Zipper(self._focus, self._path[:], self._root) |
| 184 | |
| 185 | new_focus = { |
| 186 | "value": self._focus["value"], |
| 187 | "left": self._focus["left"], |
| 188 | "right": tree |
| 189 | } |
| 190 | |
| 191 | # Reconstruct the tree with the new focus |
| 192 | new_root = self._reconstruct_tree(new_focus) |
| 193 | |
| 194 | # Find the new focus in the reconstructed tree |
| 195 | new_path = self._path[:] |
| 196 | new_focus_in_new_tree = new_root |
| 197 | for parent, direction in new_path: |
| 198 | new_focus_in_new_tree = new_focus_in_new_tree[direction] |
| 199 | |
| 200 | return Zipper(new_focus_in_new_tree, new_path, new_root) |
| 201 | |
| 25 | 202 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 203 | """ |
| 204 | Move the focus to the parent, returning a new zipper. |
| 205 | |
| 206 | Returns: |
| 207 | Zipper | None: A new zipper focused on the parent, or None if at the root. |
| 208 | |
| 209 | Edge Case: At root (no parent) - Returns None |
| 210 | Edge Case: Focus is None - Returns None |
| 211 | """ |
| 212 | # Edge Case: Focus is None |
| 213 | if self._focus is None: |
| 214 | return None |
| 215 | |
| 216 | # Edge Case: At root (no parent) |
| 217 | if not self._path: |
| 218 | return None |
| 219 | |
| 220 | new_path = self._path[:-1] |
| 221 | if not new_path: |
| 222 | return Zipper(self._root, [], self._root) |
| 223 | |
| 224 | # Find the new focus in the tree |
| 225 | new_focus = self._root |
| 226 | for parent, direction in new_path: |
| 227 | new_focus = new_focus[direction] |
| 228 | |
| 229 | return Zipper(new_focus, new_path, self._root) |
| 230 | |
| 28 | 231 | def to_tree(self) -> dict: |
| 29 | | pass |
| 232 | """ |
| 233 | Get the entire tree from the zipper. |
| 234 | |
| 235 | Returns: |
| 236 | dict: The complete tree. |
| 237 | |
| 238 | Edge Case: Root is None - Returns None |
| 239 | """ |
| 240 | # Edge Case: Root is None |
| 241 | if self._root is None: |
| 242 | return None |
| 243 | return self._root |
| 244 | |
| 245 | def _reconstruct_tree(self, new_focus): |
| 246 | """ |
| 247 | Reconstruct the tree with a new focus node. |
| 248 | |
| 249 | Args: |
| 250 | new_focus (dict): The new focus node. |
| 251 | |
| 252 | Returns: |
| 253 | dict: The reconstructed tree. |
| 254 | """ |
| 255 | if not self._path: |
| 256 | return new_focus |
| 257 | |
| 258 | # Build from the bottom up |
| 259 | current = new_focus |
| 260 | for i in range(len(self._path) - 1, -1, -1): |
| 261 | parent, direction = self._path[i] |
| 262 | new_parent = { |
| 263 | "value": parent["value"], |
| 264 | "left": parent["left"], |
| 265 | "right": parent["right"] |
| 266 | } |
| 267 | new_parent[direction] = current |
| 268 | current = new_parent |
| 269 | |
| 270 | return current |
| 271 | |
| 272 | # Handled Edge Cases: Empty tree, focus is None, no left child, no right child, at root |