| 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 with a tree and a path to the focus node. |
| 11 | |
| 12 | Args: |
| 13 | tree (dict): The complete tree structure |
| 14 | path (list): A list of tuples representing the path from root to focus |
| 15 | Each tuple contains (parent_node, direction) where direction |
| 16 | is 'left' or 'right' |
| 17 | """ |
| 18 | self.tree = tree |
| 19 | self.path = path |
| 20 | |
| 21 | # Calculate the focus node 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, with focus on the root node. |
| 33 | |
| 34 | Args: |
| 35 | tree (dict): A binary tree with keys "value", "left", and "right" |
| 36 | where left and right are either dicts or None |
| 37 | |
| 38 | Returns: |
| 39 | Zipper: A new zipper with focus on the root of the tree |
| 40 | """ |
| 41 | # Edge Case: Handle None tree |
| 42 | if tree is None: |
| 43 | return None |
| 44 | return Zipper(tree, []) |
| 45 | |
| 7 | 46 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 47 | """ |
| 48 | Get the value of the focus node. |
| 49 | |
| 50 | Returns: |
| 51 | int: The value of the focus node |
| 52 | """ |
| 53 | # Edge Case: Handle None focus |
| 54 | if self.focus is None: |
| 55 | return None |
| 56 | return self.focus['value'] |
| 57 | |
| 10 | 58 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 59 | """ |
| 60 | Set the value of the focus node, returning a new zipper. |
| 61 | |
| 62 | Args: |
| 63 | value (int): The new value for the focus node |
| 64 | |
| 65 | Returns: |
| 66 | Zipper: A new zipper with the updated value |
| 67 | """ |
| 68 | # Edge Case: Handle None focus |
| 69 | if self.focus is None: |
| 70 | return self |
| 71 | |
| 72 | # Create a deep copy of the tree |
| 73 | new_tree = self._deep_copy(self.tree) |
| 74 | |
| 75 | # Navigate to the focus node in the new tree |
| 76 | new_focus = new_tree |
| 77 | for parent, direction in self.path: |
| 78 | if direction == 'left': |
| 79 | new_focus = new_focus['left'] |
| 80 | else: # direction == 'right' |
| 81 | new_focus = new_focus['right'] |
| 82 | |
| 83 | # Update the value |
| 84 | new_focus['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 of the current focus node. |
| 91 | |
| 92 | Returns: |
| 93 | Zipper | None: A new zipper with focus on the left child, or None if no left child |
| 94 | """ |
| 95 | # Edge Case: Handle None focus |
| 96 | if self.focus is None: |
| 97 | return None |
| 98 | |
| 99 | # Edge Case: Handle no left child |
| 100 | if self.focus['left'] is None: |
| 101 | return None |
| 102 | |
| 103 | # Create a new path entry |
| 104 | new_path = self.path + [(self.focus, 'left')] |
| 105 | return Zipper(self.tree, new_path) |
| 106 | |
| 16 | 107 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 108 | """ |
| 109 | Set the left child of the focus node, returning a new zipper. |
| 110 | |
| 111 | Args: |
| 112 | tree (dict | None): The new left subtree |
| 113 | |
| 114 | Returns: |
| 115 | Zipper: A new zipper with the updated left child |
| 116 | """ |
| 117 | # Edge Case: Handle None focus |
| 118 | if self.focus is None: |
| 119 | return self |
| 120 | |
| 121 | # Create a deep copy of the tree |
| 122 | new_tree = self._deep_copy(self.tree) |
| 123 | |
| 124 | # Navigate to the focus node in the new tree |
| 125 | new_focus = new_tree |
| 126 | for parent, direction in self.path: |
| 127 | if direction == 'left': |
| 128 | new_focus = new_focus['left'] |
| 129 | else: # direction == 'right' |
| 130 | new_focus = new_focus['right'] |
| 131 | |
| 132 | # Update the left child |
| 133 | new_focus['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 of the current focus node. |
| 140 | |
| 141 | Returns: |
| 142 | Zipper | None: A new zipper with focus on the right child, or None if no right child |
| 143 | """ |
| 144 | # Edge Case: Handle None focus |
| 145 | if self.focus is None: |
| 146 | return None |
| 147 | |
| 148 | # Edge Case: Handle no right child |
| 149 | if self.focus['right'] is None: |
| 150 | return None |
| 151 | |
| 152 | # Create a new path entry |
| 153 | new_path = self.path + [(self.focus, 'right')] |
| 154 | return Zipper(self.tree, new_path) |
| 155 | |
| 22 | 156 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 157 | """ |
| 158 | Set the right child of the focus node, returning a new zipper. |
| 159 | |
| 160 | Args: |
| 161 | tree (dict | None): The new right subtree |
| 162 | |
| 163 | Returns: |
| 164 | Zipper: A new zipper with the updated right child |
| 165 | """ |
| 166 | # Edge Case: Handle None focus |
| 167 | if self.focus is None: |
| 168 | return self |
| 169 | |
| 170 | # Create a deep copy of the tree |
| 171 | new_tree = self._deep_copy(self.tree) |
| 172 | |
| 173 | # Navigate to the focus node in the new tree |
| 174 | new_focus = new_tree |
| 175 | for parent, direction in self.path: |
| 176 | if direction == 'left': |
| 177 | new_focus = new_focus['left'] |
| 178 | else: # direction == 'right' |
| 179 | new_focus = new_focus['right'] |
| 180 | |
| 181 | # Update the right child |
| 182 | new_focus['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 of the current focus node. |
| 189 | |
| 190 | Returns: |
| 191 | Zipper | None: A new zipper with focus on the parent, or None if at root |
| 192 | """ |
| 193 | # Edge Case: Handle already at root (empty path) |
| 194 | if len(self.path) == 0: |
| 195 | return None |
| 196 | |
| 197 | # Remove the last path entry to move up |
| 198 | new_path = self.path[:-1] |
| 199 | return Zipper(self.tree, new_path) |
| 200 | |
| 28 | 201 | def to_tree(self) -> dict: |
| 29 | | pass |
| 202 | """ |
| 203 | Get the complete tree from the zipper. |
| 204 | |
| 205 | Returns: |
| 206 | dict: The complete binary tree |
| 207 | """ |
| 208 | return self.tree |
| 209 | |
| 210 | def _deep_copy(self, tree): |
| 211 | """ |
| 212 | Create a deep copy of a binary tree. |
| 213 | |
| 214 | Args: |
| 215 | tree (dict): The tree to copy |
| 216 | |
| 217 | Returns: |
| 218 | dict: A deep copy of the tree |
| 219 | """ |
| 220 | if tree is None: |
| 221 | return None |
| 222 | |
| 223 | return { |
| 224 | 'value': tree['value'], |
| 225 | 'left': self._deep_copy(tree['left']), |
| 226 | 'right': self._deep_copy(tree['right']) |
| 227 | } |
| 228 | |
| 229 | # Handled Edge Cases: None tree in from_tree, None focus in value, set_value, left, set_left, right, set_right, up, already at root in up |