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