| 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 | navigation and modification operations while preserving the tree structure. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, path): |
| 10 | """ |
| 11 | Initialize a Zipper instance. |
| 12 | |
| 13 | Args: |
| 14 | tree (dict): The current focus node as a tree dictionary |
| 15 | path (list): The path from root to current focus, each element is a dict with |
| 16 | 'parent' (tree dict), 'side' (str: 'left' or 'right'), |
| 17 | 'left_sibling' (tree dict or None), 'right_sibling' (tree dict or None) |
| 18 | """ |
| 19 | self.tree = tree |
| 20 | self.path = path |
| 21 | |
| 3 | 22 | @staticmethod |
| 4 | 23 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 24 | """ |
| 25 | Create a zipper from a binary tree. |
| 26 | |
| 27 | Args: |
| 28 | tree (dict): A binary tree represented as a nested dictionary |
| 29 | with keys "value", "left", and "right" |
| 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 | |
| 7 | 39 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 40 | """ |
| 41 | Get the value of the focus node. |
| 42 | |
| 43 | Returns: |
| 44 | int: The value stored in the focus node |
| 45 | """ |
| 46 | # Edge Case: Handle None tree (should not happen in normal usage but added for safety) |
| 47 | if self.tree is None: |
| 48 | return None |
| 49 | return self.tree["value"] |
| 50 | |
| 10 | 51 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 52 | """ |
| 53 | Set the value of the focus node. |
| 54 | |
| 55 | Args: |
| 56 | value (int): The new value for the focus node |
| 57 | |
| 58 | Returns: |
| 59 | Zipper: A new zipper with the updated focus node |
| 60 | """ |
| 61 | # Edge Case: Handle None tree |
| 62 | if self.tree is None: |
| 63 | return self |
| 64 | new_tree = { |
| 65 | "value": value, |
| 66 | "left": self.tree["left"], |
| 67 | "right": self.tree["right"] |
| 68 | } |
| 69 | return Zipper(new_tree, self.path) |
| 70 | |
| 13 | 71 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 72 | """ |
| 73 | Move the focus to the left child of the current node. |
| 74 | |
| 75 | Returns: |
| 76 | Zipper | None: A new zipper focused on the left child, or None if no left child exists |
| 77 | """ |
| 78 | # Edge Case: Handle None tree or missing left child |
| 79 | if self.tree is None or self.tree["left"] is None: |
| 80 | return None |
| 81 | |
| 82 | # Create path information for returning to current node |
| 83 | path_info = { |
| 84 | "parent": self.tree, |
| 85 | "side": "left", |
| 86 | "left_sibling": None, |
| 87 | "right_sibling": self.tree["right"] |
| 88 | } |
| 89 | |
| 90 | new_path = self.path + [path_info] |
| 91 | return Zipper(self.tree["left"], new_path) |
| 92 | |
| 16 | 93 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 94 | """ |
| 95 | Set the left child of the focus node. |
| 96 | |
| 97 | Args: |
| 98 | tree (dict | None): The new left subtree or None to remove the left child |
| 99 | |
| 100 | Returns: |
| 101 | Zipper: A new zipper with the updated focus node |
| 102 | """ |
| 103 | # Edge Case: Handle None tree |
| 104 | if self.tree is None: |
| 105 | return self |
| 106 | new_tree = { |
| 107 | "value": self.tree["value"], |
| 108 | "left": tree, |
| 109 | "right": self.tree["right"] |
| 110 | } |
| 111 | return Zipper(new_tree, self.path) |
| 112 | |
| 19 | 113 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 114 | """ |
| 115 | Move the focus to the right child of the current node. |
| 116 | |
| 117 | Returns: |
| 118 | Zipper | None: A new zipper focused on the right child, or None if no right child exists |
| 119 | """ |
| 120 | # Edge Case: Handle None tree or missing right child |
| 121 | if self.tree is None or self.tree["right"] is None: |
| 122 | return None |
| 123 | |
| 124 | # Create path information for returning to current node |
| 125 | path_info = { |
| 126 | "parent": self.tree, |
| 127 | "side": "right", |
| 128 | "left_sibling": self.tree["left"], |
| 129 | "right_sibling": None |
| 130 | } |
| 131 | |
| 132 | new_path = self.path + [path_info] |
| 133 | return Zipper(self.tree["right"], new_path) |
| 134 | |
| 22 | 135 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 136 | """ |
| 137 | Set the right child of the focus node. |
| 138 | |
| 139 | Args: |
| 140 | tree (dict | None): The new right subtree or None to remove the right child |
| 141 | |
| 142 | Returns: |
| 143 | Zipper: A new zipper with the updated focus node |
| 144 | """ |
| 145 | # Edge Case: Handle None tree |
| 146 | if self.tree is None: |
| 147 | return self |
| 148 | new_tree = { |
| 149 | "value": self.tree["value"], |
| 150 | "left": self.tree["left"], |
| 151 | "right": tree |
| 152 | } |
| 153 | return Zipper(new_tree, self.path) |
| 154 | |
| 25 | 155 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 156 | """ |
| 157 | Move the focus to the parent of the current node. |
| 158 | |
| 159 | Returns: |
| 160 | Zipper | None: A new zipper focused on the parent node, or None if already at root |
| 161 | """ |
| 162 | # Edge Case: Handle None tree or already at root (empty path) |
| 163 | if self.tree is None or len(self.path) == 0: |
| 164 | return None |
| 165 | |
| 166 | # Get the last path information |
| 167 | last_path = self.path[-1] |
| 168 | |
| 169 | # Reconstruct the parent with the current tree in the correct position |
| 170 | parent_tree = { |
| 171 | "value": last_path["parent"]["value"], |
| 172 | "left": last_path["left_sibling"], |
| 173 | "right": last_path["right_sibling"] |
| 174 | } |
| 175 | |
| 176 | # Put the current tree in the correct side of the parent |
| 177 | if last_path["side"] == "left": |
| 178 | parent_tree["left"] = self.tree |
| 179 | else: # last_path["side"] == "right" |
| 180 | parent_tree["right"] = self.tree |
| 181 | |
| 182 | return Zipper(parent_tree, self.path[:-1]) |
| 183 | |
| 28 | 184 | def to_tree(self) -> dict: |
| 29 | | pass |
| 185 | """ |
| 186 | Get the complete tree from the zipper. |
| 187 | |
| 188 | Returns: |
| 189 | dict: The complete binary tree with the current zipper state |
| 190 | """ |
| 191 | # Start with the current focus node |
| 192 | result = self.tree |
| 193 | |
| 194 | # Traverse up the path to reconstruct the full tree |
| 195 | for path_info in reversed(self.path): |
| 196 | # Create a new parent node |
| 197 | parent = { |
| 198 | "value": path_info["parent"]["value"], |
| 199 | "left": path_info["left_sibling"], |
| 200 | "right": path_info["right_sibling"] |
| 201 | } |
| 202 | |
| 203 | # Attach the current result to the correct side |
| 204 | if path_info["side"] == "left": |
| 205 | parent["left"] = result |
| 206 | else: # path_info["side"] == "right" |
| 207 | parent["right"] = result |
| 208 | |
| 209 | result = parent |
| 210 | |
| 211 | return result |
| 212 | |
| 213 | # Handled Edge Cases: None tree input, missing children, already at root |