| 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 a binary tree that allows navigation and manipulation. |
| 4 | |
| 5 | The zipper maintains a focus on a specific node in the tree and allows |
| 6 | movement up, left, and right, as well as modification of the focused node. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, path): |
| 10 | """ |
| 11 | Initialize a Zipper with a tree and a path to the focus node. |
| 12 | |
| 13 | Args: |
| 14 | tree (dict): The complete tree structure |
| 15 | path (list): A list of tuples representing the path to the focus node. |
| 16 | Each tuple contains (parent_node, side) where side is 'left' or 'right' |
| 17 | """ |
| 18 | self.tree = tree |
| 19 | self.path = path |
| 20 | |
| 3 | 21 | @staticmethod |
| 4 | 22 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 23 | """ |
| 24 | Create a zipper from a tree, with focus on the root node. |
| 25 | |
| 26 | Args: |
| 27 | tree (dict): A binary tree represented as a dict with keys "value", "left", and "right" |
| 28 | |
| 29 | Returns: |
| 30 | Zipper: A new zipper focused on the root of the tree |
| 31 | """ |
| 32 | # Edge Case: Handle None tree |
| 33 | if tree is None: |
| 34 | return None |
| 35 | return Zipper(tree, []) |
| 36 | |
| 37 | def _get_focus_node(self): |
| 38 | """ |
| 39 | Get the currently focused node by following the path from root. |
| 40 | |
| 41 | Returns: |
| 42 | dict: The focused node |
| 43 | """ |
| 44 | node = self.tree |
| 45 | for parent, side in self.path: |
| 46 | node = node[side] |
| 47 | return node |
| 48 | |
| 7 | 49 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 50 | """ |
| 51 | Get the value of the focus node. |
| 52 | |
| 53 | Returns: |
| 54 | int: The value of the focus node |
| 55 | """ |
| 56 | return self._get_focus_node()["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 value |
| 69 | if value is None: |
| 70 | raise ValueError("Value cannot be None") |
| 71 | |
| 72 | new_tree = self._copy_tree() |
| 73 | |
| 74 | # Navigate to the focus node in the new tree |
| 75 | node = new_tree |
| 76 | for parent, side in self.path: |
| 77 | node = node[side] |
| 78 | |
| 79 | # Update the value |
| 80 | node["value"] = value |
| 81 | |
| 82 | return Zipper(new_tree, self.path) |
| 83 | |
| 13 | 84 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 85 | """ |
| 86 | Move the focus to the left child of the current node. |
| 87 | |
| 88 | Returns: |
| 89 | Zipper | None: A new zipper focused on the left child, or None if no left child exists |
| 90 | """ |
| 91 | focus = self._get_focus_node() |
| 92 | # Edge Case: No left child |
| 93 | if focus["left"] is None: |
| 94 | return None |
| 95 | |
| 96 | # Edge Case: Empty tree |
| 97 | if not focus: |
| 98 | return None |
| 99 | |
| 100 | new_path = self.path + [(focus, "left")] |
| 101 | return Zipper(self.tree, new_path) |
| 102 | |
| 16 | 103 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 104 | """ |
| 105 | Set the left child of the focus node, returning a new zipper. |
| 106 | |
| 107 | Args: |
| 108 | tree (dict | None): The new left subtree (or None to remove) |
| 109 | |
| 110 | Returns: |
| 111 | Zipper: A new zipper with the updated left child |
| 112 | """ |
| 113 | new_tree = self._copy_tree() |
| 114 | |
| 115 | # Navigate to the focus node in the new tree |
| 116 | node = new_tree |
| 117 | for parent, side in self.path: |
| 118 | node = node[side] |
| 119 | |
| 120 | # Update the left child |
| 121 | node["left"] = tree |
| 122 | |
| 123 | return Zipper(new_tree, self.path) |
| 124 | |
| 19 | 125 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 126 | """ |
| 127 | Move the focus to the right child of the current node. |
| 128 | |
| 129 | Returns: |
| 130 | Zipper | None: A new zipper focused on the right child, or None if no right child exists |
| 131 | """ |
| 132 | focus = self._get_focus_node() |
| 133 | # Edge Case: No right child |
| 134 | if focus["right"] is None: |
| 135 | return None |
| 136 | |
| 137 | # Edge Case: Empty tree |
| 138 | if not focus: |
| 139 | return None |
| 140 | |
| 141 | new_path = self.path + [(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, returning a new zipper. |
| 147 | |
| 148 | Args: |
| 149 | tree (dict | None): The new right subtree (or None to remove) |
| 150 | |
| 151 | Returns: |
| 152 | Zipper: A new zipper with the updated right child |
| 153 | """ |
| 154 | new_tree = self._copy_tree() |
| 155 | |
| 156 | # Navigate to the focus node in the new tree |
| 157 | node = new_tree |
| 158 | for parent, side in self.path: |
| 159 | node = node[side] |
| 160 | |
| 161 | # Update the right child |
| 162 | node["right"] = tree |
| 163 | |
| 164 | return Zipper(new_tree, self.path) |
| 165 | |
| 25 | 166 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 167 | """ |
| 168 | Move the focus to the parent of the current node. |
| 169 | |
| 170 | Returns: |
| 171 | Zipper | None: A new zipper focused on the parent, or None if already at root |
| 172 | """ |
| 173 | # Edge Case: Already at root |
| 174 | if not self.path: |
| 175 | return None |
| 176 | |
| 177 | new_path = self.path[:-1] |
| 178 | return Zipper(self.tree, new_path) |
| 179 | |
| 28 | 180 | def to_tree(self) -> dict: |
| 29 | | pass |
| 181 | """ |
| 182 | Get the complete tree from the zipper. |
| 183 | |
| 184 | Returns: |
| 185 | dict: The complete tree |
| 186 | """ |
| 187 | return self.tree |
| 188 | |
| 189 | def _copy_tree(self): |
| 190 | """ |
| 191 | Create a deep copy of the tree. |
| 192 | |
| 193 | Returns: |
| 194 | dict: A deep copy of the tree |
| 195 | """ |
| 196 | def _deep_copy(node): |
| 197 | if node is None: |
| 198 | return None |
| 199 | return { |
| 200 | "value": node["value"], |
| 201 | "left": _deep_copy(node["left"]), |
| 202 | "right": _deep_copy(node["right"]) |
| 203 | } |
| 204 | |
| 205 | return _deep_copy(self.tree) |
| 206 | |
| 207 | # Handled Edge Cases: None tree in from_tree, None value in set_value, |
| 208 | # No left child in left, Empty tree in left, No right child in right, |
| 209 | # Empty tree in right, Already at root in up |