| 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, 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 to empty list if None is provided |
| 21 | self.path = path if path is not None else [] |
| 22 | |
| 3 | 23 | @staticmethod |
| 4 | 24 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 25 | """ |
| 26 | Create a zipper from a binary tree. |
| 27 | |
| 28 | Args: |
| 29 | tree (dict): A binary tree with keys "value", "left", and "right" |
| 30 | |
| 31 | Returns: |
| 32 | Zipper: A new zipper focused on the root of the tree |
| 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 | Returns: |
| 44 | dict: The focus node in the tree |
| 45 | """ |
| 46 | node = self.tree |
| 47 | for parent, direction in self.path: |
| 48 | node = node[direction] |
| 49 | return node |
| 50 | |
| 7 | 51 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 52 | """ |
| 53 | Get the value of the focus node. |
| 54 | |
| 55 | Returns: |
| 56 | int: The value of the focus node |
| 57 | """ |
| 58 | return self._get_focus_node()["value"] |
| 59 | |
| 10 | 60 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 61 | """ |
| 62 | Set the value of the focus node and return a new zipper. |
| 63 | |
| 64 | Args: |
| 65 | value (int): The new value for the focus node |
| 66 | |
| 67 | Returns: |
| 68 | Zipper: A new zipper with the updated value |
| 69 | """ |
| 70 | # Edge Case: Handle None value input |
| 71 | if value is None: |
| 72 | raise ValueError("Value cannot be None") |
| 73 | |
| 74 | new_tree = self._copy_tree() |
| 75 | focus = new_tree |
| 76 | |
| 77 | # Navigate to the focus node in the copied tree |
| 78 | for parent, direction in self.path: |
| 79 | focus = focus[direction] |
| 80 | |
| 81 | # Update the value |
| 82 | focus["value"] = value |
| 83 | |
| 84 | return Zipper(new_tree, list(self.path)) |
| 85 | |
| 13 | 86 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 87 | """ |
| 88 | Move the focus to the left child of the current node. |
| 89 | |
| 90 | Returns: |
| 91 | Zipper | None: A new zipper focused on the left child, or None if no left child exists |
| 92 | """ |
| 93 | focus = self._get_focus_node() |
| 94 | # Edge Case: Return None if there's no left child |
| 95 | if focus["left"] is None: |
| 96 | return None |
| 97 | |
| 98 | new_path = list(self.path) |
| 99 | new_path.append((focus, "left")) |
| 100 | return Zipper(self.tree, new_path) |
| 101 | |
| 16 | 102 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 103 | """ |
| 104 | Set the left child of the focus node and return a new zipper. |
| 105 | |
| 106 | Args: |
| 107 | tree (dict | None): The new left subtree or None |
| 108 | |
| 109 | Returns: |
| 110 | Zipper: A new zipper with the updated left child |
| 111 | """ |
| 112 | new_tree = self._copy_tree() |
| 113 | focus = new_tree |
| 114 | |
| 115 | # Navigate to the focus node in the copied tree |
| 116 | for parent, direction in self.path: |
| 117 | focus = focus[direction] |
| 118 | |
| 119 | # Update the left child |
| 120 | focus["left"] = tree |
| 121 | |
| 122 | return Zipper(new_tree, list(self.path)) |
| 123 | |
| 19 | 124 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 125 | """ |
| 126 | Move the focus to the right child of the current node. |
| 127 | |
| 128 | Returns: |
| 129 | Zipper | None: A new zipper focused on the right child, or None if no right child exists |
| 130 | """ |
| 131 | focus = self._get_focus_node() |
| 132 | # Edge Case: Return None if there's no right child |
| 133 | if focus["right"] is None: |
| 134 | return None |
| 135 | |
| 136 | new_path = list(self.path) |
| 137 | new_path.append((focus, "right")) |
| 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 and return a new zipper. |
| 143 | |
| 144 | Args: |
| 145 | tree (dict | None): The new right subtree or None |
| 146 | |
| 147 | Returns: |
| 148 | Zipper: A new zipper with the updated right child |
| 149 | """ |
| 150 | new_tree = self._copy_tree() |
| 151 | focus = new_tree |
| 152 | |
| 153 | # Navigate to the focus node in the copied tree |
| 154 | for parent, direction in self.path: |
| 155 | focus = focus[direction] |
| 156 | |
| 157 | # Update the right child |
| 158 | focus["right"] = tree |
| 159 | |
| 160 | return Zipper(new_tree, list(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 | Zipper | None: A new zipper focused on the parent, or None if already at root |
| 168 | """ |
| 169 | # Edge Case: Return None if already at the root (no parent) |
| 170 | if not self.path: |
| 171 | return None |
| 172 | |
| 173 | new_path = list(self.path) |
| 174 | new_path.pop() # Remove the last step to go up |
| 175 | return Zipper(self.tree, new_path) |
| 176 | |
| 28 | 177 | def to_tree(self) -> dict: |
| 29 | | pass |
| 178 | """ |
| 179 | Get the complete tree from the zipper. |
| 180 | |
| 181 | Returns: |
| 182 | dict: The complete binary tree |
| 183 | """ |
| 184 | return self.tree |
| 185 | |
| 186 | def _copy_tree(self): |
| 187 | """ |
| 188 | Create a deep copy of the tree. |
| 189 | |
| 190 | Returns: |
| 191 | dict: A deep copy of the tree |
| 192 | """ |
| 193 | def _copy_node(node): |
| 194 | if node is None: |
| 195 | return None |
| 196 | return { |
| 197 | "value": node["value"], |
| 198 | "left": _copy_node(node["left"]), |
| 199 | "right": _copy_node(node["right"]) |
| 200 | } |
| 201 | |
| 202 | return _copy_node(self.tree) |
| 203 | |
| 204 | # Handled Edge Cases: None tree input, None value input, No left child, No right child, Already at root |