| 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 and allows |
| 6 | navigation to parent, left child, and right child nodes, as well as |
| 7 | modification of the tree structure. |
| 8 | """ |
| 9 | |
| 10 | def __init__(self, tree, path): |
| 11 | """ |
| 12 | Initialize a Zipper instance. |
| 13 | |
| 14 | Args: |
| 15 | tree (dict): The complete tree structure |
| 16 | path (list): Path from root to focus node, containing tuples of (parent_node, branch) |
| 17 | where branch is 'left' or 'right' |
| 18 | """ |
| 19 | self.tree = tree |
| 20 | self.path = path |
| 21 | |
| 22 | # Calculate current focus based on path |
| 23 | self.focus = tree |
| 24 | for parent, branch in self.path: |
| 25 | if self.focus is None: |
| 26 | break |
| 27 | self.focus = self.focus.get(branch) |
| 28 | |
| 3 | 29 | @staticmethod |
| 4 | 30 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 31 | """ |
| 32 | Create a zipper from a binary tree. |
| 33 | |
| 34 | Args: |
| 35 | tree (dict): A binary tree with keys "value", "left", and "right" |
| 36 | |
| 37 | Returns: |
| 38 | Zipper: A new zipper with focus on the root node |
| 39 | """ |
| 40 | # Edge Case: Handle None tree input |
| 41 | if tree is None: |
| 42 | return Zipper(None, []) |
| 43 | return Zipper(tree, []) |
| 44 | |
| 7 | 45 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 46 | """ |
| 47 | Get the value of the focus node. |
| 48 | |
| 49 | Returns: |
| 50 | int: The value of the focus node |
| 51 | |
| 52 | Raises: |
| 53 | Exception: If the focus is None (empty tree) |
| 54 | """ |
| 55 | # Edge Case: Handle when focus is None |
| 56 | if self.focus is None: |
| 57 | raise Exception("Cannot get value of None focus") |
| 58 | return self.focus["value"] |
| 59 | |
| 10 | 60 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 61 | """ |
| 62 | Set the value of the focus node. |
| 63 | |
| 64 | Args: |
| 65 | value (int): The new value for the focus node |
| 66 | |
| 67 | Returns: |
| 68 | Zipper: A new zipper with the updated value |
| 69 | """ |
| 70 | # Edge Case: Handle when focus is None |
| 71 | if self.focus is None: |
| 72 | raise Exception("Cannot set value of None focus") |
| 73 | |
| 74 | # Create a new tree with the updated value |
| 75 | new_tree = self._reconstruct_tree() |
| 76 | |
| 77 | # Navigate to the focus position in the new tree |
| 78 | current = new_tree |
| 79 | for _, branch in self.path: |
| 80 | current = current[branch] |
| 81 | |
| 82 | # Update the value |
| 83 | current["value"] = value |
| 84 | |
| 85 | return Zipper(new_tree, self.path) |
| 86 | |
| 13 | 87 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 88 | """ |
| 89 | Move the focus to the left child of the current focus node. |
| 90 | |
| 91 | Returns: |
| 92 | Zipper | None: A new zipper with focus on the left child, or None if no left child exists |
| 93 | """ |
| 94 | # Edge Case: Handle when focus is None |
| 95 | if self.focus is None or self.focus.get("left") is None: |
| 96 | return None |
| 97 | |
| 98 | # Create a new path with the current focus added |
| 99 | new_path = self.path + [(self.focus, "left")] |
| 100 | return Zipper(self.tree, new_path) |
| 101 | |
| 16 | 102 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 103 | """ |
| 104 | Set the left child of the focus node. |
| 105 | |
| 106 | Args: |
| 107 | tree (dict | None): The new left subtree or None |
| 108 | |
| 109 | Returns: |
| 110 | Zipper: A new zipper with the updated left child |
| 111 | """ |
| 112 | # Edge Case: Handle when focus is None |
| 113 | if self.focus is None: |
| 114 | raise Exception("Cannot set left of None focus") |
| 115 | |
| 116 | # Create a new tree with the updated left child |
| 117 | new_tree = self._reconstruct_tree() |
| 118 | |
| 119 | # Navigate to the focus position in the new tree |
| 120 | current = new_tree |
| 121 | for _, branch in self.path: |
| 122 | current = current[branch] |
| 123 | |
| 124 | # Update the left child |
| 125 | current["left"] = tree |
| 126 | |
| 127 | return Zipper(new_tree, self.path) |
| 128 | |
| 19 | 129 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 130 | """ |
| 131 | Move the focus to the right child of the current focus node. |
| 132 | |
| 133 | Returns: |
| 134 | Zipper | None: A new zipper with focus on the right child, or None if no right child exists |
| 135 | """ |
| 136 | # Edge Case: Handle when focus is None |
| 137 | if self.focus is None or self.focus.get("right") is None: |
| 138 | return None |
| 139 | |
| 140 | # Create a new path with the current focus added |
| 141 | new_path = self.path + [(self.focus, "right")] |
| 142 | return Zipper(self.tree, new_path) |
| 143 | |
| 22 | 144 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 145 | """ |
| 146 | Set the right child of the focus node. |
| 147 | |
| 148 | Args: |
| 149 | tree (dict | None): The new right subtree or None |
| 150 | |
| 151 | Returns: |
| 152 | Zipper: A new zipper with the updated right child |
| 153 | """ |
| 154 | # Edge Case: Handle when focus is None |
| 155 | if self.focus is None: |
| 156 | raise Exception("Cannot set right of None focus") |
| 157 | |
| 158 | # Create a new tree with the updated right child |
| 159 | new_tree = self._reconstruct_tree() |
| 160 | |
| 161 | # Navigate to the focus position in the new tree |
| 162 | current = new_tree |
| 163 | for _, branch in self.path: |
| 164 | current = current[branch] |
| 165 | |
| 166 | # Update the right child |
| 167 | current["right"] = tree |
| 168 | |
| 169 | return Zipper(new_tree, self.path) |
| 170 | |
| 25 | 171 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 172 | """ |
| 173 | Move the focus to the parent of the current focus node. |
| 174 | |
| 175 | Returns: |
| 176 | Zipper | None: A new zipper with focus on the parent, or None if at root |
| 177 | """ |
| 178 | # Edge Case: Handle when at root (no parent) |
| 179 | if len(self.path) == 0: |
| 180 | return None |
| 181 | |
| 182 | # Create a new path without the last element |
| 183 | new_path = self.path[:-1] |
| 184 | return Zipper(self.tree, new_path) |
| 185 | |
| 28 | 186 | def to_tree(self) -> dict: |
| 29 | | pass |
| 187 | """ |
| 188 | Get the complete tree from the zipper. |
| 189 | |
| 190 | Returns: |
| 191 | dict: The complete binary tree |
| 192 | """ |
| 193 | # Edge Case: Handle when tree is None |
| 194 | if self.tree is None: |
| 195 | return None |
| 196 | return self.tree |
| 197 | |
| 198 | def _reconstruct_tree(self) -> dict: |
| 199 | """ |
| 200 | Create a deep copy of the tree to enable modifications. |
| 201 | |
| 202 | Returns: |
| 203 | dict: A deep copy of the tree |
| 204 | """ |
| 205 | def deep_copy(node): |
| 206 | if node is None: |
| 207 | return None |
| 208 | return { |
| 209 | "value": node["value"], |
| 210 | "left": deep_copy(node.get("left")), |
| 211 | "right": deep_copy(node.get("right")) |
| 212 | } |
| 213 | |
| 214 | return deep_copy(self.tree) |
| 215 | |
| 216 | # Handled Edge Cases: None tree input, None focus, setting value/children of None focus, moving from None focus, moving up from root |