| 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 | efficient navigation and modification operations. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, path): |
| 10 | """ |
| 11 | Initialize a Zipper instance. |
| 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 | # Edge Case: Handle None focus node |
| 59 | focus = self._get_focus_node() |
| 60 | if focus is None: |
| 61 | raise Exception("Focus node is None") |
| 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 for the focus node |
| 70 | |
| 71 | Returns: |
| 72 | Zipper: A new zipper with the updated value |
| 73 | """ |
| 74 | # Edge Case: Handle None focus node |
| 75 | if self._get_focus_node() is None: |
| 76 | return self |
| 77 | |
| 78 | new_tree = self._copy_tree(self.tree) |
| 79 | path = self.path[:] |
| 80 | |
| 81 | node = new_tree |
| 82 | for direction, parent, sibling in path: |
| 83 | node = node[direction] |
| 84 | node["value"] = value |
| 85 | |
| 86 | return Zipper(new_tree, 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 focused on the left child, or None if no left child |
| 94 | """ |
| 95 | focus = self._get_focus_node() |
| 96 | # Edge Case: Handle None focus node |
| 97 | if focus is None: |
| 98 | return None |
| 99 | # Edge Case: Handle focus node with no left child |
| 100 | if focus["left"] is None: |
| 101 | return None |
| 102 | |
| 103 | new_path = self.path + [("left", {"value": focus["value"], "left": focus["left"], "right": focus["right"]}, focus["right"])] |
| 104 | return Zipper(self.tree, new_path) |
| 105 | |
| 16 | 106 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 107 | """ |
| 108 | Set the left child of the focus node. |
| 109 | |
| 110 | Args: |
| 111 | tree (dict | None): The new left subtree |
| 112 | |
| 113 | Returns: |
| 114 | Zipper: A new zipper with the updated left child |
| 115 | """ |
| 116 | # Edge Case: Handle None focus node |
| 117 | if self._get_focus_node() is None: |
| 118 | return self |
| 119 | |
| 120 | new_tree = self._copy_tree(self.tree) |
| 121 | path = self.path[:] |
| 122 | |
| 123 | node = new_tree |
| 124 | for direction, parent, sibling in path: |
| 125 | node = node[direction] |
| 126 | node["left"] = tree |
| 127 | |
| 128 | return Zipper(new_tree, path) |
| 129 | |
| 19 | 130 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 131 | """ |
| 132 | Move the focus to the right child of the current focus node. |
| 133 | |
| 134 | Returns: |
| 135 | Zipper | None: A new zipper focused on the right child, or None if no right child |
| 136 | """ |
| 137 | focus = self._get_focus_node() |
| 138 | # Edge Case: Handle None focus node |
| 139 | if focus is None: |
| 140 | return None |
| 141 | # Edge Case: Handle focus node with no right child |
| 142 | if focus["right"] is None: |
| 143 | return None |
| 144 | |
| 145 | new_path = self.path + [("right", {"value": focus["value"], "left": focus["left"], "right": focus["right"]}, focus["left"])] |
| 146 | return Zipper(self.tree, new_path) |
| 147 | |
| 22 | 148 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 149 | """ |
| 150 | Set the right child of the focus node. |
| 151 | |
| 152 | Args: |
| 153 | tree (dict | None): The new right subtree |
| 154 | |
| 155 | Returns: |
| 156 | Zipper: A new zipper with the updated right child |
| 157 | """ |
| 158 | # Edge Case: Handle None focus node |
| 159 | if self._get_focus_node() is None: |
| 160 | return self |
| 161 | |
| 162 | new_tree = self._copy_tree(self.tree) |
| 163 | path = self.path[:] |
| 164 | |
| 165 | node = new_tree |
| 166 | for direction, parent, sibling in path: |
| 167 | node = node[direction] |
| 168 | node["right"] = tree |
| 169 | |
| 170 | return Zipper(new_tree, path) |
| 171 | |
| 25 | 172 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 173 | """ |
| 174 | Move the focus to the parent of the current focus node. |
| 175 | |
| 176 | Returns: |
| 177 | Zipper | None: A new zipper focused on the parent, or None if at root |
| 178 | """ |
| 179 | # Edge Case: Handle zipper at root (no parent) |
| 180 | if not self.path: |
| 181 | return None |
| 182 | return Zipper(self.tree, self.path[:-1]) |
| 183 | |
| 28 | 184 | def to_tree(self) -> dict: |
| 29 | | pass |
| 185 | """ |
| 186 | Get the full tree from the zipper. |
| 187 | |
| 188 | Returns: |
| 189 | dict: The full binary tree |
| 190 | """ |
| 191 | return self.tree |
| 192 | |
| 193 | def _copy_tree(self, tree): |
| 194 | """ |
| 195 | Create a deep copy of a tree. |
| 196 | |
| 197 | Args: |
| 198 | tree (dict): The tree to copy |
| 199 | |
| 200 | Returns: |
| 201 | dict: A deep copy of the tree |
| 202 | """ |
| 203 | if tree is None: |
| 204 | return None |
| 205 | return { |
| 206 | "value": tree["value"], |
| 207 | "left": self._copy_tree(tree["left"]), |
| 208 | "right": self._copy_tree(tree["right"]) |
| 209 | } |
| 210 | |
| 211 | # Handled Edge Cases: None tree in from_tree, None focus node in value/set_value/left/right/set_left/set_right, no left child in left, no right child in right, zipper at root in up |