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