| 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 tree structure being navigated. |
| 14 | path (list): A list of breadcrumb dictionaries representing the path from root to focus. |
| 15 | """ |
| 16 | self.tree = tree |
| 17 | self.path = path |
| 18 | |
| 19 | # Determine current focus based on path |
| 20 | self.focus = tree |
| 21 | for breadcrumb in self.path: |
| 22 | if breadcrumb['direction'] == 'left': |
| 23 | self.focus = self.focus['left'] |
| 24 | else: # direction == 'right' |
| 25 | self.focus = self.focus['right'] |
| 26 | |
| 3 | 27 | @staticmethod |
| 4 | 28 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 29 | """ |
| 30 | Create a zipper from a binary tree, with focus on the root node. |
| 31 | |
| 32 | Args: |
| 33 | tree (dict): A binary tree represented as nested dictionaries. |
| 34 | |
| 35 | Returns: |
| 36 | Zipper: A new zipper instance focused on the root of the tree. |
| 37 | """ |
| 38 | # Edge Case: Empty tree |
| 39 | if tree is None: |
| 40 | return None |
| 41 | return Zipper(tree, []) |
| 42 | |
| 7 | 43 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 44 | """ |
| 45 | Get the value of the focus node. |
| 46 | |
| 47 | Returns: |
| 48 | int: The value stored in the focus node. |
| 49 | """ |
| 50 | # Edge Case: Focus is None (should not happen with valid usage) |
| 51 | if self.focus is None: |
| 52 | return None |
| 53 | return self.focus['value'] |
| 54 | |
| 10 | 55 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 56 | """ |
| 57 | Set the value of the focus node, returning a new zipper. |
| 58 | |
| 59 | Args: |
| 60 | value (int): The new value to set. |
| 61 | |
| 62 | Returns: |
| 63 | Zipper: A new zipper with the updated value. |
| 64 | """ |
| 65 | # Edge Case: Focus is None |
| 66 | if self.focus is None: |
| 67 | return self |
| 68 | |
| 69 | # Create a new tree with the updated value |
| 70 | new_tree = self._reconstruct_tree() |
| 71 | |
| 72 | # Navigate to the focus position and update the value |
| 73 | current = new_tree |
| 74 | for breadcrumb in self.path: |
| 75 | if breadcrumb['direction'] == 'left': |
| 76 | current = current['left'] |
| 77 | else: # direction == 'right' |
| 78 | current = current['right'] |
| 79 | |
| 80 | current['value'] = value |
| 81 | |
| 82 | return Zipper(new_tree, self.path) |
| 83 | |
| 13 | 84 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 85 | """ |
| 86 | Move the focus to the left child of the current node. |
| 87 | |
| 88 | Returns: |
| 89 | Zipper | None: A new zipper focused on the left child, or None if no left child exists. |
| 90 | """ |
| 91 | # Edge Case: Focus is None |
| 92 | if self.focus is None: |
| 93 | return None |
| 94 | |
| 95 | # Edge Case: No left child |
| 96 | if self.focus['left'] is None: |
| 97 | return None |
| 98 | |
| 99 | # Create a breadcrumb for the current node |
| 100 | breadcrumb = { |
| 101 | 'parent': self.focus, |
| 102 | 'direction': 'left', |
| 103 | 'sibling': self.focus['right'] |
| 104 | } |
| 105 | |
| 106 | # Return a new zipper focused on the left child |
| 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 child of the focus node, returning a new zipper. |
| 112 | |
| 113 | Args: |
| 114 | tree (dict | None): The new left subtree. |
| 115 | |
| 116 | Returns: |
| 117 | Zipper: A new zipper with the updated left child. |
| 118 | """ |
| 119 | # Edge Case: Focus is None |
| 120 | if self.focus is None: |
| 121 | return self |
| 122 | |
| 123 | # Create a new tree with the updated left child |
| 124 | new_tree = self._reconstruct_tree() |
| 125 | |
| 126 | # Navigate to the focus position and update the left child |
| 127 | current = new_tree |
| 128 | for breadcrumb in self.path: |
| 129 | if breadcrumb['direction'] == 'left': |
| 130 | current = current['left'] |
| 131 | else: # direction == 'right' |
| 132 | current = current['right'] |
| 133 | |
| 134 | current['left'] = tree |
| 135 | |
| 136 | return Zipper(new_tree, self.path) |
| 137 | |
| 19 | 138 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 139 | """ |
| 140 | Move the focus to the right child of the current node. |
| 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: Focus is None |
| 146 | if self.focus is None: |
| 147 | return None |
| 148 | |
| 149 | # Edge Case: No right child |
| 150 | if self.focus['right'] is None: |
| 151 | return None |
| 152 | |
| 153 | # Create a breadcrumb for the current node |
| 154 | breadcrumb = { |
| 155 | 'parent': self.focus, |
| 156 | 'direction': 'right', |
| 157 | 'sibling': self.focus['left'] |
| 158 | } |
| 159 | |
| 160 | # Return a new zipper focused on the right child |
| 161 | return Zipper(self.tree, self.path + [breadcrumb]) |
| 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: Focus is None |
| 174 | if self.focus is None: |
| 175 | return self |
| 176 | |
| 177 | # Create a new tree with the updated right child |
| 178 | new_tree = self._reconstruct_tree() |
| 179 | |
| 180 | # Navigate to the focus position and update the right child |
| 181 | current = new_tree |
| 182 | for breadcrumb in self.path: |
| 183 | if breadcrumb['direction'] == 'left': |
| 184 | current = current['left'] |
| 185 | else: # direction == 'right' |
| 186 | current = current['right'] |
| 187 | |
| 188 | current['right'] = tree |
| 189 | |
| 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 already at root. |
| 198 | """ |
| 199 | # Edge Case: Already at root (no path) |
| 200 | if not self.path: |
| 201 | return None |
| 202 | |
| 203 | # Return a new zipper with the last breadcrumb removed |
| 204 | return Zipper(self.tree, self.path[:-1]) |
| 205 | |
| 28 | 206 | def to_tree(self) -> dict: |
| 29 | | pass |
| 207 | """ |
| 208 | Get the complete tree from the zipper. |
| 209 | |
| 210 | Returns: |
| 211 | dict: The complete binary tree. |
| 212 | """ |
| 213 | return self.tree |
| 214 | |
| 215 | def _reconstruct_tree(self): |
| 216 | """ |
| 217 | Create a deep copy of the tree to allow for modifications. |
| 218 | |
| 219 | Returns: |
| 220 | dict: A deep copy of the tree. |
| 221 | """ |
| 222 | def deep_copy(node): |
| 223 | if node is None: |
| 224 | return None |
| 225 | return { |
| 226 | 'value': node['value'], |
| 227 | 'left': deep_copy(node['left']), |
| 228 | 'right': deep_copy(node['right']) |
| 229 | } |
| 230 | |
| 231 | return deep_copy(self.tree) |
| 232 | |
| 233 | # Handled Edge Cases: Empty tree in from_tree, focus is None in value/set_value/left/right/set_left/set_right/up, no left/right child in left/right |