| 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 a binary tree that allows navigation and manipulation. |
| 4 | |
| 5 | The zipper maintains a focus on a specific node in the tree and allows |
| 6 | movement up, left, and right, as well as modification of the tree. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, path): |
| 10 | """ |
| 11 | Initialize a Zipper with a tree and a path. |
| 12 | |
| 13 | :param tree: The tree structure (dict with 'value', 'left', 'right') |
| 14 | :param path: A list of tuples representing the path to the current focus |
| 15 | """ |
| 16 | self.tree = tree |
| 17 | self.path = path |
| 18 | |
| 19 | # Navigate to the focus based on the path |
| 20 | self.focus = self.tree |
| 21 | for direction, parent in self.path: |
| 22 | if direction == 'left': |
| 23 | self.focus = parent['left'] |
| 24 | else: # direction == 'right' |
| 25 | self.focus = parent['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. |
| 31 | |
| 32 | :param tree: The binary tree to create a zipper for |
| 33 | :return: A new Zipper instance |
| 34 | """ |
| 35 | # Edge Case: Empty tree |
| 36 | if tree is None: |
| 37 | return None |
| 38 | return Zipper(tree, []) |
| 39 | |
| 7 | 40 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 41 | """ |
| 42 | Get the value of the focus node. |
| 43 | |
| 44 | :return: The value of the focus node |
| 45 | """ |
| 46 | # Edge Case: Focus is None (should not happen with valid usage) |
| 47 | if self.focus is None: |
| 48 | return None |
| 49 | return self.focus['value'] |
| 50 | |
| 10 | 51 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 52 | """ |
| 53 | Set the value of the focus node and return a new zipper. |
| 54 | |
| 55 | :param value: The new value for the focus node |
| 56 | :return: A new Zipper with the updated value |
| 57 | """ |
| 58 | # Edge Case: Focus is None |
| 59 | if self.focus is None: |
| 60 | return self |
| 61 | |
| 62 | # Create a deep copy of the tree |
| 63 | new_tree = self._deep_copy_tree(self.tree) |
| 64 | |
| 65 | # Navigate to the focus in the new tree |
| 66 | new_focus = new_tree |
| 67 | for direction, parent in self.path: |
| 68 | if direction == 'left': |
| 69 | new_focus = new_focus['left'] |
| 70 | else: # direction == 'right' |
| 71 | new_focus = new_focus['right'] |
| 72 | |
| 73 | # Update the value |
| 74 | new_focus['value'] = value |
| 75 | |
| 76 | return Zipper(new_tree, self.path) |
| 77 | |
| 13 | 78 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 79 | """ |
| 80 | Move the focus to the left child and return a new zipper. |
| 81 | |
| 82 | :return: A new Zipper focused on the left child, or None if no left child |
| 83 | """ |
| 84 | # Edge Case: Focus is None |
| 85 | if self.focus is None: |
| 86 | return None |
| 87 | |
| 88 | # Edge Case: No left child |
| 89 | if self.focus['left'] is None: |
| 90 | return None |
| 91 | |
| 92 | # Create a new path entry |
| 93 | new_path = self.path + [('left', self.focus)] |
| 94 | return Zipper(self.tree, new_path) |
| 95 | |
| 16 | 96 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 97 | """ |
| 98 | Set the left child of the focus node and return a new zipper. |
| 99 | |
| 100 | :param tree: The new left subtree (or None) |
| 101 | :return: A new Zipper with the updated left child |
| 102 | """ |
| 103 | # Edge Case: Focus is None |
| 104 | if self.focus is None: |
| 105 | return self |
| 106 | |
| 107 | # Create a deep copy of the tree |
| 108 | new_tree = self._deep_copy_tree(self.tree) |
| 109 | |
| 110 | # Navigate to the focus in the new tree |
| 111 | new_focus = new_tree |
| 112 | for direction, parent in self.path: |
| 113 | if direction == 'left': |
| 114 | new_focus = new_focus['left'] |
| 115 | else: # direction == 'right' |
| 116 | new_focus = new_focus['right'] |
| 117 | |
| 118 | # Update the left child |
| 119 | new_focus['left'] = tree |
| 120 | |
| 121 | return Zipper(new_tree, self.path) |
| 122 | |
| 19 | 123 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 124 | """ |
| 125 | Move the focus to the right child and return a new zipper. |
| 126 | |
| 127 | :return: A new Zipper focused on the right child, or None if no right child |
| 128 | """ |
| 129 | # Edge Case: Focus is None |
| 130 | if self.focus is None: |
| 131 | return None |
| 132 | |
| 133 | # Edge Case: No right child |
| 134 | if self.focus['right'] is None: |
| 135 | return None |
| 136 | |
| 137 | # Create a new path entry |
| 138 | new_path = self.path + [('right', self.focus)] |
| 139 | return Zipper(self.tree, new_path) |
| 140 | |
| 22 | 141 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 142 | """ |
| 143 | Set the right child of the focus node and return a new zipper. |
| 144 | |
| 145 | :param tree: The new right subtree (or None) |
| 146 | :return: A new Zipper with the updated right child |
| 147 | """ |
| 148 | # Edge Case: Focus is None |
| 149 | if self.focus is None: |
| 150 | return self |
| 151 | |
| 152 | # Create a deep copy of the tree |
| 153 | new_tree = self._deep_copy_tree(self.tree) |
| 154 | |
| 155 | # Navigate to the focus in the new tree |
| 156 | new_focus = new_tree |
| 157 | for direction, parent in self.path: |
| 158 | if direction == 'left': |
| 159 | new_focus = new_focus['left'] |
| 160 | else: # direction == 'right' |
| 161 | new_focus = new_focus['right'] |
| 162 | |
| 163 | # Update the right child |
| 164 | new_focus['right'] = tree |
| 165 | |
| 166 | return Zipper(new_tree, self.path) |
| 167 | |
| 25 | 168 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 169 | """ |
| 170 | Move the focus to the parent and return a new zipper. |
| 171 | |
| 172 | :return: A new Zipper focused on the parent, or None if at root |
| 173 | """ |
| 174 | # Edge Case: Already at root (no parent) |
| 175 | if not self.path: |
| 176 | return None |
| 177 | |
| 178 | # Remove the last path entry to move up |
| 179 | new_path = self.path[:-1] |
| 180 | return Zipper(self.tree, new_path) |
| 181 | |
| 28 | 182 | def to_tree(self) -> dict: |
| 29 | | pass |
| 183 | """ |
| 184 | Get the complete tree from the zipper. |
| 185 | |
| 186 | :return: The complete binary tree |
| 187 | """ |
| 188 | return self.tree |
| 189 | |
| 190 | def _deep_copy_tree(self, tree): |
| 191 | """ |
| 192 | Create a deep copy of a binary tree. |
| 193 | |
| 194 | :param tree: The tree to copy |
| 195 | :return: A deep copy of the tree |
| 196 | """ |
| 197 | if tree is None: |
| 198 | return None |
| 199 | |
| 200 | return { |
| 201 | 'value': tree['value'], |
| 202 | 'left': self._deep_copy_tree(tree['left']), |
| 203 | 'right': self._deep_copy_tree(tree['right']) |
| 204 | } |
| 205 | |
| 206 | # Handled Edge Cases: Empty tree in from_tree, None focus in value/set_value/left/right/set_left/set_right/up, no left/right child in left/right |