| 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 binary trees that allows navigation and manipulation. |
| 4 | |
| 5 | The zipper maintains a focus on a specific node in the tree and allows |
| 6 | movement through the tree structure while keeping track of the path |
| 7 | taken to reach the current focus. |
| 8 | """ |
| 9 | |
| 10 | def __init__(self, tree, path): |
| 11 | """ |
| 12 | Initialize a Zipper with a tree and a path to the focus node. |
| 13 | |
| 14 | Args: |
| 15 | tree (dict): The complete tree structure |
| 16 | path (list): List of breadcrumbs representing the path to focus |
| 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 binary tree with focus on the root node. |
| 25 | |
| 26 | Args: |
| 27 | tree (dict): A binary tree with keys "value", "left", "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 current focus node by following the path from root. |
| 40 | |
| 41 | Returns: |
| 42 | dict: The current focus node |
| 43 | """ |
| 44 | node = self.tree |
| 45 | for direction, parent, sibling in self.path: |
| 46 | if direction == 'left': |
| 47 | node = node['left'] |
| 48 | else: # direction == 'right' |
| 49 | node = node['right'] |
| 50 | return node |
| 51 | |
| 7 | 52 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 53 | """ |
| 54 | Get the value of the focus node. |
| 55 | |
| 56 | Returns: |
| 57 | int: The value of the focus node |
| 58 | """ |
| 59 | return self._get_focus_node()['value'] |
| 60 | |
| 10 | 61 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 62 | """ |
| 63 | Set the value of the focus node, returning a new zipper. |
| 64 | |
| 65 | Args: |
| 66 | value (int): The new value for the focus node |
| 67 | |
| 68 | Returns: |
| 69 | Zipper: A new zipper with updated value |
| 70 | """ |
| 71 | # Edge Case: Handle None value |
| 72 | if value is None: |
| 73 | raise ValueError("Value cannot be None") |
| 74 | |
| 75 | new_tree = self._copy_tree() |
| 76 | focus = self._navigate_to_focus(new_tree) |
| 77 | focus['value'] = value |
| 78 | return Zipper(new_tree, self.path[:]) |
| 79 | |
| 13 | 80 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 81 | """ |
| 82 | Move the focus to the left child of the current node. |
| 83 | |
| 84 | Returns: |
| 85 | Zipper | None: A new zipper focused on the left child, or None if no left child |
| 86 | """ |
| 87 | focus = self._get_focus_node() |
| 88 | # Edge Case: No left child |
| 89 | if focus['left'] is None: |
| 90 | return None |
| 91 | |
| 92 | new_path = self.path[:] + [('left', focus, focus['right'])] |
| 93 | return Zipper(self.tree, new_path) |
| 94 | |
| 16 | 95 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 96 | """ |
| 97 | Set the left subtree of the focus node, returning a new zipper. |
| 98 | |
| 99 | Args: |
| 100 | tree (dict | None): The new left subtree |
| 101 | |
| 102 | Returns: |
| 103 | Zipper: A new zipper with updated left subtree |
| 104 | """ |
| 105 | new_tree = self._copy_tree() |
| 106 | focus = self._navigate_to_focus(new_tree) |
| 107 | focus['left'] = tree |
| 108 | return Zipper(new_tree, self.path[:]) |
| 109 | |
| 19 | 110 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 111 | """ |
| 112 | Move the focus to the right child of the current node. |
| 113 | |
| 114 | Returns: |
| 115 | Zipper | None: A new zipper focused on the right child, or None if no right child |
| 116 | """ |
| 117 | focus = self._get_focus_node() |
| 118 | # Edge Case: No right child |
| 119 | if focus['right'] is None: |
| 120 | return None |
| 121 | |
| 122 | new_path = self.path[:] + [('right', focus, focus['left'])] |
| 123 | return Zipper(self.tree, new_path) |
| 124 | |
| 22 | 125 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 126 | """ |
| 127 | Set the right subtree of the focus node, returning a new zipper. |
| 128 | |
| 129 | Args: |
| 130 | tree (dict | None): The new right subtree |
| 131 | |
| 132 | Returns: |
| 133 | Zipper: A new zipper with updated right subtree |
| 134 | """ |
| 135 | new_tree = self._copy_tree() |
| 136 | focus = self._navigate_to_focus(new_tree) |
| 137 | focus['right'] = tree |
| 138 | return Zipper(new_tree, self.path[:]) |
| 139 | |
| 25 | 140 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 141 | """ |
| 142 | Move the focus to the parent of the current node. |
| 143 | |
| 144 | Returns: |
| 145 | Zipper | None: A new zipper focused on the parent, or None if at root |
| 146 | """ |
| 147 | # Edge Case: Already at root (no parent) |
| 148 | if not self.path: |
| 149 | return None |
| 150 | |
| 151 | new_path = self.path[:-1] |
| 152 | return Zipper(self.tree, new_path) |
| 153 | |
| 28 | 154 | def to_tree(self) -> dict: |
| 29 | | pass |
| 155 | """ |
| 156 | Get the complete tree from the zipper. |
| 157 | |
| 158 | Returns: |
| 159 | dict: The complete binary tree |
| 160 | """ |
| 161 | return self.tree |
| 162 | |
| 163 | def _copy_tree(self): |
| 164 | """ |
| 165 | Create a deep copy of the tree. |
| 166 | |
| 167 | Returns: |
| 168 | dict: A deep copy of the tree |
| 169 | """ |
| 170 | def copy_node(node): |
| 171 | if node is None: |
| 172 | return None |
| 173 | return { |
| 174 | 'value': node['value'], |
| 175 | 'left': copy_node(node['left']), |
| 176 | 'right': copy_node(node['right']) |
| 177 | } |
| 178 | return copy_node(self.tree) |
| 179 | |
| 180 | def _navigate_to_focus(self, tree): |
| 181 | """ |
| 182 | Navigate to the focus node in a given tree. |
| 183 | |
| 184 | Args: |
| 185 | tree (dict): The tree to navigate |
| 186 | |
| 187 | Returns: |
| 188 | dict: The focus node in the given tree |
| 189 | """ |
| 190 | node = tree |
| 191 | for direction, _, _ in self.path: |
| 192 | if direction == 'left': |
| 193 | node = node['left'] |
| 194 | else: # direction == 'right' |
| 195 | node = node['right'] |
| 196 | return node |
| 197 | |
| 198 | # Handled Edge Cases: None tree in from_tree, None value in set_value, No left child in left, No right child in right, Already at root in up |