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