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