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