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