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