| 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 maintains a focus on a specific node in the tree, allowing for |
| 6 | functional navigation and modification operations. |
| 7 | |
| 8 | Attributes: |
| 9 | _focus (dict): The current node in focus |
| 10 | _path (list): A list of tuples representing the path from root to focus, |
| 11 | each tuple contains (parent_node, side) where side is 'left' or 'right' |
| 12 | """ |
| 13 | |
| 14 | def __init__(self, focus, path=None): |
| 15 | """ |
| 16 | Initialize a Zipper. |
| 17 | |
| 18 | Args: |
| 19 | focus (dict): The node currently in focus |
| 20 | path (list): The path from root to focus |
| 21 | """ |
| 22 | self._focus = focus |
| 23 | self._path = path or [] |
| 24 | |
| 3 | 25 | @staticmethod |
| 4 | 26 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 27 | """ |
| 28 | Create a zipper from a binary tree. |
| 29 | |
| 30 | Args: |
| 31 | tree (dict): A binary tree represented as a dict with keys "value", "left", "right" |
| 32 | |
| 6 | 33 | |
| 34 | Returns: |
| 35 | Zipper: A new zipper with focus on the root node |
| 36 | |
| 37 | |
| 38 | Edge Case: Empty tree (None) - Returns a zipper with None focus |
| 39 | """ |
| 40 | # Edge Case: Empty tree |
| 41 | if tree is None: |
| 42 | return Zipper(None, []) |
| 43 | return Zipper(tree, []) |
| 44 | |
| 7 | 45 | def value(self) -> int: |
| 8 | | pass |
| 46 | """ |
| 47 | Get the value of the focus node. |
| 48 | |
| 49 | |
| 50 | Returns: |
| 51 | int: The value of the focus node |
| 52 | |
| 9 | 53 | |
| 54 | Edge Case: Focus is None - Raises AttributeError |
| 55 | """ |
| 56 | # Edge Case: Focus is None |
| 57 | if self._focus is None: |
| 58 | raise AttributeError("Cannot get value of None focus") |
| 59 | return self._focus["value"] |
| 60 | |
| 10 | 61 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 62 | """ |
| 63 | Set the value of the focus node. |
| 64 | |
| 65 | |
| 66 | Args: |
| 67 | value (int): The new value for the focus node |
| 68 | |
| 12 | 69 | |
| 70 | Returns: |
| 71 | Zipper: A new zipper with updated focus node |
| 72 | |
| 73 | |
| 74 | Edge Case: Focus is None - Returns a new zipper with None focus |
| 75 | """ |
| 76 | # Edge Case: Focus is None |
| 77 | if self._focus is None: |
| 78 | return Zipper(None, self._path) |
| 79 | |
| 80 | new_focus = { |
| 81 | "value": value, |
| 82 | "left": self._focus["left"], |
| 83 | "right": self._focus["right"] |
| 84 | } |
| 85 | return Zipper(new_focus, self._path) |
| 86 | |
| 13 | 87 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 88 | """ |
| 89 | Move the focus to the left child of the current focus node. |
| 90 | |
| 91 | |
| 92 | Returns: |
| 93 | Zipper | None: A new zipper with focus on the left child, or None if no left child |
| 94 | |
| 15 | 95 | |
| 96 | Edge Case: Focus is None - Returns None |
| 97 | Edge Case: No left child - Returns None |
| 98 | """ |
| 99 | # Edge Case: Focus is None |
| 100 | if self._focus is None: |
| 101 | return None |
| 102 | |
| 103 | # Edge Case: No left child |
| 104 | if self._focus["left"] is None: |
| 105 | return None |
| 106 | |
| 107 | |
| 108 | new_path = self._path + [(self._focus, "left")] |
| 109 | return Zipper(self._focus["left"], new_path) |
| 110 | |
| 16 | 111 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 112 | """ |
| 113 | Set the left child of the focus node. |
| 114 | |
| 115 | |
| 116 | Args: |
| 117 | tree (dict | None): The new left subtree |
| 118 | |
| 18 | 119 | |
| 120 | Returns: |
| 121 | Zipper: A new zipper with updated focus node |
| 122 | |
| 123 | |
| 124 | Edge Case: Focus is None - Returns a new zipper with None focus |
| 125 | """ |
| 126 | # Edge Case: Focus is None |
| 127 | if self._focus is None: |
| 128 | return Zipper(None, self._path) |
| 129 | |
| 130 | new_focus = { |
| 131 | "value": self._focus["value"], |
| 132 | "left": tree, |
| 133 | "right": self._focus["right"] |
| 134 | } |
| 135 | return Zipper(new_focus, self._path) |
| 136 | |
| 19 | 137 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 138 | """ |
| 139 | Move the focus to the right child of the current focus node. |
| 140 | |
| 141 | |
| 142 | Returns: |
| 143 | Zipper | None: A new zipper with focus on the right child, or None if no right child |
| 144 | |
| 21 | 145 | |
| 146 | Edge Case: Focus is None - Returns None |
| 147 | Edge Case: No right child - Returns None |
| 148 | """ |
| 149 | # Edge Case: Focus is None |
| 150 | if self._focus is None: |
| 151 | return None |
| 152 | |
| 153 | # Edge Case: No right child |
| 154 | if self._focus["right"] is None: |
| 155 | return None |
| 156 | |
| 157 | |
| 158 | new_path = self._path + [(self._focus, "right")] |
| 159 | return Zipper(self._focus["right"], new_path) |
| 160 | |
| 22 | 161 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 162 | """ |
| 163 | Set the right child of the focus node. |
| 164 | |
| 165 | |
| 166 | Args: |
| 167 | tree (dict | None): The new right subtree |
| 168 | |
| 24 | 169 | |
| 170 | Returns: |
| 171 | Zipper: A new zipper with updated focus node |
| 172 | |
| 173 | |
| 174 | Edge Case: Focus is None - Returns a new zipper with None focus |
| 175 | """ |
| 176 | # Edge Case: Focus is None |
| 177 | if self._focus is None: |
| 178 | return Zipper(None, self._path) |
| 179 | |
| 180 | new_focus = { |
| 181 | "value": self._focus["value"], |
| 182 | "left": self._focus["left"], |
| 183 | "right": tree |
| 184 | } |
| 185 | return Zipper(new_focus, self._path) |
| 186 | |
| 25 | 187 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 188 | """ |
| 189 | Move the focus to the parent of the current focus node. |
| 190 | |
| 191 | |
| 192 | Returns: |
| 193 | Zipper | None: A new zipper with focus on the parent, or None if at root |
| 194 | |
| 27 | 195 | |
| 196 | Edge Case: At root (empty path) - Returns None |
| 197 | Edge Case: Focus is None - Returns None |
| 198 | """ |
| 199 | # Edge Case: Focus is None or at root |
| 200 | if self._focus is None or not self._path: |
| 201 | return None |
| 202 | |
| 203 | parent, side = self._path[-1] |
| 204 | new_focus = dict(parent) # Create a copy |
| 205 | |
| 206 | # Update the appropriate child reference |
| 207 | if side == "left": |
| 208 | new_focus["left"] = self._focus |
| 209 | else: # side == "right" |
| 210 | new_focus["right"] = self._focus |
| 211 | |
| 212 | |
| 213 | return Zipper(new_focus, self._path[:-1]) |
| 214 | |
| 28 | 215 | def to_tree(self) -> dict: |
| 29 | | pass |
| 216 | """ |
| 217 | Get the complete binary tree from the zipper. |
| 218 | |
| 219 | |
| 220 | Returns: |
| 221 | dict: The complete binary tree |
| 222 | |
| 223 | |
| 224 | Edge Case: Focus is None - Reconstructs tree from path |
| 225 | Edge Case: At root - Returns focus directly |
| 226 | """ |
| 227 | # Start with current focus |
| 228 | current = self._focus |
| 229 | |
| 230 | # Rebuild the tree by going up the path |
| 231 | for parent, side in reversed(self._path): |
| 232 | new_parent = dict(parent) # Create a copy |
| 233 | if side == "left": |
| 234 | new_parent["left"] = current |
| 235 | else: # side == "right" |
| 236 | new_parent["right"] = current |
| 237 | current = new_parent |
| 238 | |
| 239 | |
| 240 | return current |
| 241 | |
| 242 | # Handled Edge Cases: Empty tree, Focus is None, No left child, No right child, At root |