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