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