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