| 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 and allows |
| 6 | functional navigation and modification operations. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, path): |
| 10 | """ |
| 11 | Initialize a Zipper. |
| 12 | |
| 13 | Args: |
| 14 | tree (dict): The tree structure |
| 15 | path (list): The path from root to focus node with context |
| 16 | """ |
| 17 | self.tree = tree |
| 18 | self.path = path |
| 19 | |
| 20 | # Calculate current focus based on path |
| 21 | self.focus = tree |
| 22 | for breadcrumb in path: |
| 23 | direction = breadcrumb['direction'] |
| 24 | if direction == 'left': |
| 25 | self.focus = self.focus['left'] |
| 26 | else: # direction == 'right' |
| 27 | self.focus = self.focus['right'] |
| 28 | |
| 3 | 29 | @staticmethod |
| 4 | 30 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 31 | """ |
| 32 | Create a zipper from a binary tree. |
| 33 | |
| 34 | Args: |
| 35 | tree (dict): A binary tree with keys "value", "left", "right" |
| 36 | |
| 37 | Returns: |
| 38 | Zipper: A new zipper focused on the root node |
| 39 | """ |
| 40 | # Edge Case: Handle None tree input |
| 41 | if tree is None: |
| 42 | return None |
| 43 | return Zipper(tree, []) |
| 44 | |
| 7 | 45 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 46 | """ |
| 47 | Get the value of the focus node. |
| 48 | |
| 49 | Returns: |
| 50 | int: The value of the focus node |
| 51 | """ |
| 52 | # Edge Case: Handle None focus |
| 53 | if self.focus is None: |
| 54 | return None |
| 55 | return self.focus['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 updated value |
| 66 | """ |
| 67 | # Edge Case: Handle None focus |
| 68 | if self.focus is None: |
| 69 | return self |
| 70 | |
| 71 | # Create a new tree with updated value |
| 72 | new_tree = self._reconstruct_tree() |
| 73 | |
| 74 | # Navigate to focus position and update value |
| 75 | current = new_tree |
| 76 | for breadcrumb in self.path: |
| 77 | direction = breadcrumb['direction'] |
| 78 | if direction == 'left': |
| 79 | current = current['left'] |
| 80 | else: # direction == 'right' |
| 81 | current = current['right'] |
| 82 | |
| 83 | # Edge Case: Handle None current node |
| 84 | if current is not None: |
| 85 | current['value'] = value |
| 86 | |
| 87 | return Zipper(new_tree, self.path) |
| 88 | |
| 13 | 89 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 90 | """ |
| 91 | Move focus to the left child. |
| 92 | |
| 93 | Returns: |
| 94 | Zipper | None: A new zipper focused on the left child, or None if no left child |
| 95 | """ |
| 96 | # Edge Case: Handle None focus or no left child |
| 97 | if self.focus is None or self.focus['left'] is None: |
| 98 | return None |
| 99 | |
| 100 | # Create breadcrumb for current node |
| 101 | breadcrumb = { |
| 102 | 'direction': 'left', |
| 103 | 'parent_value': self.focus['value'], |
| 104 | 'right': self.focus['right'] |
| 105 | } |
| 106 | |
| 107 | return Zipper(self.tree, self.path + [breadcrumb]) |
| 108 | |
| 16 | 109 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 110 | """ |
| 111 | Set the left subtree of the focus node. |
| 112 | |
| 113 | Args: |
| 114 | tree (dict | None): The new left subtree |
| 115 | |
| 116 | Returns: |
| 117 | Zipper: A new zipper with updated left subtree |
| 118 | """ |
| 119 | # Create a new tree with updated left subtree |
| 120 | new_tree = self._reconstruct_tree() |
| 121 | |
| 122 | # Navigate to focus position and update left subtree |
| 123 | current = new_tree |
| 124 | for breadcrumb in self.path: |
| 125 | direction = breadcrumb['direction'] |
| 126 | if direction == 'left': |
| 127 | current = current['left'] |
| 128 | else: # direction == 'right' |
| 129 | current = current['right'] |
| 130 | |
| 131 | # Edge Case: Handle None current node |
| 132 | if current is not None: |
| 133 | current['left'] = tree |
| 134 | |
| 135 | return Zipper(new_tree, self.path) |
| 136 | |
| 19 | 137 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 138 | """ |
| 139 | Move focus to the right child. |
| 140 | |
| 141 | Returns: |
| 142 | Zipper | None: A new zipper focused on the right child, or None if no right child |
| 143 | """ |
| 144 | # Edge Case: Handle None focus or no right child |
| 145 | if self.focus is None or self.focus['right'] is None: |
| 146 | return None |
| 147 | |
| 148 | # Create breadcrumb for current node |
| 149 | breadcrumb = { |
| 150 | 'direction': 'right', |
| 151 | 'parent_value': self.focus['value'], |
| 152 | 'left': self.focus['left'] |
| 153 | } |
| 154 | |
| 155 | return Zipper(self.tree, self.path + [breadcrumb]) |
| 156 | |
| 22 | 157 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 158 | """ |
| 159 | Set the right subtree of the focus node. |
| 160 | |
| 161 | Args: |
| 162 | tree (dict | None): The new right subtree |
| 163 | |
| 164 | Returns: |
| 165 | Zipper: A new zipper with updated right subtree |
| 166 | """ |
| 167 | # Create a new tree with updated right subtree |
| 168 | new_tree = self._reconstruct_tree() |
| 169 | |
| 170 | # Navigate to focus position and update right subtree |
| 171 | current = new_tree |
| 172 | for breadcrumb in self.path: |
| 173 | direction = breadcrumb['direction'] |
| 174 | if direction == 'left': |
| 175 | current = current['left'] |
| 176 | else: # direction == 'right' |
| 177 | current = current['right'] |
| 178 | |
| 179 | # Edge Case: Handle None current node |
| 180 | if current is not None: |
| 181 | current['right'] = tree |
| 182 | |
| 183 | return Zipper(new_tree, self.path) |
| 184 | |
| 25 | 185 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 186 | """ |
| 187 | Move focus to the parent node. |
| 188 | |
| 189 | Returns: |
| 190 | Zipper | None: A new zipper focused on the parent, or None if at root |
| 191 | """ |
| 192 | # Edge Case: Handle already at root (no path) |
| 193 | if not self.path: |
| 194 | return None |
| 195 | |
| 196 | # Remove last breadcrumb to move up |
| 197 | new_path = self.path[:-1] |
| 198 | return Zipper(self.tree, new_path) |
| 199 | |
| 28 | 200 | def to_tree(self) -> dict: |
| 29 | | pass |
| 201 | """ |
| 202 | Get the complete tree from the zipper. |
| 203 | |
| 204 | Returns: |
| 205 | dict: The complete binary tree |
| 206 | """ |
| 207 | return self._reconstruct_tree() |
| 208 | |
| 209 | def _reconstruct_tree(self) -> dict: |
| 210 | """ |
| 211 | Reconstruct the complete tree with any modifications. |
| 212 | |
| 213 | Returns: |
| 214 | dict: The reconstructed tree |
| 215 | """ |
| 216 | # Start with a deep copy of the original tree |
| 217 | import copy |
| 218 | new_tree = copy.deepcopy(self.tree) |
| 219 | |
| 220 | # Navigate to focus position and get reference to it |
| 221 | current = new_tree |
| 222 | |
| 223 | # Edge Case: Handle empty path (focus is root) |
| 224 | if not self.path: |
| 225 | return new_tree |
| 226 | |
| 227 | # Navigate through all but the last breadcrumb |
| 228 | for breadcrumb in self.path[:-1]: |
| 229 | direction = breadcrumb['direction'] |
| 230 | if direction == 'left': |
| 231 | current = current['left'] |
| 232 | else: # direction == 'right' |
| 233 | current = current['right'] |
| 234 | |
| 235 | # Apply the last breadcrumb's changes |
| 236 | last_breadcrumb = self.path[-1] |
| 237 | direction = last_breadcrumb['direction'] |
| 238 | |
| 239 | if direction == 'left': |
| 240 | current['left'] = copy.deepcopy(self.focus) |
| 241 | else: # direction == 'right' |
| 242 | current['right'] = copy.deepcopy(self.focus) |
| 243 | |
| 244 | return new_tree |
| 245 | |
| 246 | # Handled Edge Cases: None tree input, None focus, None current node, |
| 247 | # no left/right child, already at root |