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