| 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 and allows |
| 6 | navigation and modification operations while preserving the tree structure. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, path): |
| 10 | """ |
| 11 | Initialize a Zipper instance. |
| 12 | |
| 13 | Args: |
| 14 | tree (dict): The tree structure being navigated |
| 15 | path (list): Path from root to current focus node with context |
| 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 with focus on the root node. |
| 24 | |
| 25 | Args: |
| 26 | tree (dict): A binary tree with keys "value", "left", "right" |
| 27 | |
| 28 | Returns: |
| 29 | Zipper: A new zipper instance focused on the root node |
| 30 | """ |
| 31 | # Edge Case: Empty tree |
| 32 | if tree is None: |
| 33 | return None |
| 34 | return Zipper(tree, []) |
| 35 | |
| 36 | def _current_node(self): |
| 37 | """ |
| 38 | Get the current focus node based on the path. |
| 39 | |
| 40 | Returns: |
| 41 | dict: The current node in focus |
| 42 | """ |
| 43 | # Edge Case: Root node (empty path) |
| 44 | if not self.path: |
| 45 | return self.tree |
| 46 | |
| 47 | node = self.tree |
| 48 | for direction, parent, sibling in self.path: |
| 49 | if direction == 'left': |
| 50 | node = node['left'] |
| 51 | else: # direction == 'right' |
| 52 | node = node['right'] |
| 53 | return node |
| 54 | |
| 7 | 55 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 56 | """ |
| 57 | Get the value of the focus node. |
| 58 | |
| 59 | Returns: |
| 60 | int: The value of the current focus node |
| 61 | """ |
| 62 | return self._current_node()['value'] |
| 63 | |
| 10 | 64 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 65 | """ |
| 66 | Set the value of the focus node and return a new zipper. |
| 67 | |
| 68 | Args: |
| 69 | value (int): The new value for the focus node |
| 70 | |
| 71 | Returns: |
| 72 | Zipper: A new zipper with updated value |
| 73 | """ |
| 74 | # Edge Case: Empty path (root node) |
| 75 | if not self.path: |
| 76 | new_tree = {'value': value, 'left': self.tree['left'], 'right': self.tree['right']} |
| 77 | return Zipper(new_tree, []) |
| 78 | |
| 79 | # Create a deep copy of the tree |
| 80 | new_tree = self._deep_copy(self.tree) |
| 81 | |
| 82 | # Navigate to the node and update its value |
| 83 | node = new_tree |
| 84 | for direction, parent, sibling in self.path: |
| 85 | if direction == 'left': |
| 86 | node = node['left'] |
| 87 | else: # direction == 'right' |
| 88 | node = node['right'] |
| 89 | node['value'] = value |
| 90 | |
| 91 | return Zipper(new_tree, list(self.path)) |
| 92 | |
| 13 | 93 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 94 | """ |
| 95 | Move the focus to the left child of the current node. |
| 96 | |
| 97 | Returns: |
| 98 | Zipper | None: A new zipper focused on the left child, or None if no left child |
| 99 | """ |
| 100 | current = self._current_node() |
| 101 | # Edge Case: No left child |
| 102 | if current['left'] is None: |
| 103 | return None |
| 104 | |
| 105 | # Create a new path entry for moving to the left child |
| 106 | new_path = list(self.path) |
| 107 | new_path.append(('left', self._deep_copy(current), self._deep_copy(current['right']))) |
| 108 | |
| 109 | return Zipper(self.tree, new_path) |
| 110 | |
| 16 | 111 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 112 | """ |
| 113 | Set the left child of the focus node and return a new zipper. |
| 114 | |
| 115 | Args: |
| 116 | tree (dict | None): The new left subtree |
| 117 | |
| 118 | Returns: |
| 119 | Zipper: A new zipper with updated left child |
| 120 | """ |
| 121 | # Edge Case: Empty path (root node) |
| 122 | if not self.path: |
| 123 | new_tree = {'value': self.tree['value'], 'left': tree, 'right': self.tree['right']} |
| 124 | return Zipper(new_tree, []) |
| 125 | |
| 126 | # Create a deep copy of the tree |
| 127 | new_tree = self._deep_copy(self.tree) |
| 128 | |
| 129 | # Navigate to the node and update its left child |
| 130 | node = new_tree |
| 131 | for direction, parent, sibling in self.path: |
| 132 | if direction == 'left': |
| 133 | node = node['left'] |
| 134 | else: # direction == 'right' |
| 135 | node = node['right'] |
| 136 | node['left'] = tree |
| 137 | |
| 138 | return Zipper(new_tree, list(self.path)) |
| 139 | |
| 19 | 140 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 141 | """ |
| 142 | Move the focus to the right child of the current node. |
| 143 | |
| 144 | Returns: |
| 145 | Zipper | None: A new zipper focused on the right child, or None if no right child |
| 146 | """ |
| 147 | current = self._current_node() |
| 148 | # Edge Case: No right child |
| 149 | if current['right'] is None: |
| 150 | return None |
| 151 | |
| 152 | # Create a new path entry for moving to the right child |
| 153 | new_path = list(self.path) |
| 154 | new_path.append(('right', self._deep_copy(current), self._deep_copy(current['left']))) |
| 155 | |
| 156 | return Zipper(self.tree, new_path) |
| 157 | |
| 22 | 158 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 159 | """ |
| 160 | Set the right child of the focus node and return a new zipper. |
| 161 | |
| 162 | Args: |
| 163 | tree (dict | None): The new right subtree |
| 164 | |
| 165 | Returns: |
| 166 | Zipper: A new zipper with updated right child |
| 167 | """ |
| 168 | # Edge Case: Empty path (root node) |
| 169 | if not self.path: |
| 170 | new_tree = {'value': self.tree['value'], 'left': self.tree['left'], 'right': tree} |
| 171 | return Zipper(new_tree, []) |
| 172 | |
| 173 | # Create a deep copy of the tree |
| 174 | new_tree = self._deep_copy(self.tree) |
| 175 | |
| 176 | # Navigate to the node and update its right child |
| 177 | node = new_tree |
| 178 | for direction, parent, sibling in self.path: |
| 179 | if direction == 'left': |
| 180 | node = node['left'] |
| 181 | else: # direction == 'right' |
| 182 | node = node['right'] |
| 183 | node['right'] = tree |
| 184 | |
| 185 | return Zipper(new_tree, list(self.path)) |
| 186 | |
| 25 | 187 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 188 | """ |
| 189 | Move the focus to the parent of the current node. |
| 190 | |
| 191 | Returns: |
| 192 | Zipper | None: A new zipper focused on the parent, or None if at root |
| 193 | """ |
| 194 | # Edge Case: Already at root (no parent) |
| 195 | if not self.path: |
| 196 | return None |
| 197 | |
| 198 | # Create a new path without the last element |
| 199 | new_path = list(self.path[:-1]) |
| 200 | |
| 201 | return Zipper(self.tree, new_path) |
| 202 | |
| 28 | 203 | def to_tree(self) -> dict: |
| 29 | | pass |
| 204 | """ |
| 205 | Get the complete tree from the zipper. |
| 206 | |
| 207 | Returns: |
| 208 | dict: The complete binary tree |
| 209 | """ |
| 210 | return self.tree |
| 211 | |
| 212 | def _deep_copy(self, obj): |
| 213 | """ |
| 214 | Create a deep copy of a tree structure. |
| 215 | |
| 216 | Args: |
| 217 | obj: The object to copy (dict, None, or other) |
| 218 | |
| 219 | Returns: |
| 220 | A deep copy of the object |
| 221 | """ |
| 222 | # Edge Case: None value |
| 223 | if obj is None: |
| 224 | return None |
| 225 | # Edge Case: Dictionary (tree node) |
| 226 | if isinstance(obj, dict): |
| 227 | return {key: self._deep_copy(value) for key, value in obj.items()} |
| 228 | # Edge Case: Primitive value |
| 229 | return obj |
| 230 | |
| 231 | # Handled Edge Cases: Empty tree, root node operations, missing children, None values, primitive values |