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