| 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: dict, path: list = None): |
| 10 | """ |
| 11 | Initialize a Zipper instance. |
| 12 | |
| 13 | Args: |
| 14 | tree: The binary tree node as a dict with 'value', 'left', 'right' keys |
| 15 | path: List of breadcrumbs representing the path from root to current node |
| 16 | """ |
| 17 | self.tree = tree |
| 18 | self.path = path or [] |
| 19 | |
| 3 | 20 | @staticmethod |
| 4 | 21 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 22 | """ |
| 23 | Create a zipper from a binary tree. |
| 24 | |
| 25 | Args: |
| 26 | tree: A binary tree represented as a dict with 'value', 'left', 'right' keys |
| 27 | |
| 28 | Returns: |
| 29 | A new Zipper instance focused on the root of the tree |
| 30 | |
| 31 | Edge Case: Empty tree (None) - Returns a zipper with None tree |
| 32 | """ |
| 33 | # Edge Case: Empty tree (None) |
| 34 | if tree is None: |
| 35 | return Zipper(None) |
| 36 | return Zipper(tree) |
| 37 | |
| 7 | 38 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 39 | """ |
| 40 | Get the value of the focus node. |
| 41 | |
| 42 | Returns: |
| 43 | The value of the current node |
| 44 | |
| 45 | Edge Case: Focus node is None - Raises AttributeError |
| 46 | """ |
| 47 | # Edge Case: Focus node is None |
| 48 | if self.tree is None: |
| 49 | raise AttributeError("Cannot get value of None node") |
| 50 | return self.tree["value"] |
| 51 | |
| 10 | 52 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 53 | """ |
| 54 | Set the value of the focus node. |
| 55 | |
| 56 | Args: |
| 57 | value: The new value for the focus node |
| 58 | |
| 59 | Returns: |
| 60 | A new Zipper instance with the updated value |
| 61 | |
| 62 | Edge Case: Focus node is None - Returns a new zipper with None tree |
| 63 | """ |
| 64 | # Edge Case: Focus node is None |
| 65 | if self.tree is None: |
| 66 | return Zipper(None, self.path) |
| 67 | |
| 68 | new_tree = self.tree.copy() |
| 69 | new_tree["value"] = value |
| 70 | return Zipper(new_tree, self.path) |
| 71 | |
| 13 | 72 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 73 | """ |
| 74 | Move the focus to the left child of the current node. |
| 75 | |
| 76 | Returns: |
| 77 | A new Zipper focused on the left child, or None if no left child exists |
| 78 | |
| 79 | Edge Case: Focus node is None - Returns None |
| 80 | Edge Case: No left child - Returns None |
| 81 | """ |
| 82 | # Edge Case: Focus node is None |
| 83 | if self.tree is None: |
| 84 | return None |
| 85 | |
| 86 | # Edge Case: No left child |
| 87 | if self.tree["left"] is None: |
| 88 | return None |
| 89 | |
| 90 | # Create a breadcrumb to remember how to reconstruct the parent |
| 91 | breadcrumb = { |
| 92 | "parent": self.tree, |
| 93 | "side": "left", |
| 94 | "sibling": self.tree["right"] |
| 95 | } |
| 96 | |
| 97 | return Zipper(self.tree["left"], self.path + [breadcrumb]) |
| 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 the left child) |
| 105 | |
| 106 | Returns: |
| 107 | A new Zipper instance with the updated left child |
| 108 | |
| 109 | Edge Case: Focus node is None - Returns a new zipper with None tree |
| 110 | """ |
| 111 | # Edge Case: Focus node is None |
| 112 | if self.tree is None: |
| 113 | return Zipper(None, self.path) |
| 114 | |
| 115 | new_tree = self.tree.copy() |
| 116 | new_tree["left"] = tree |
| 117 | return Zipper(new_tree, self.path) |
| 118 | |
| 19 | 119 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 120 | """ |
| 121 | Move the focus to the right child of the current node. |
| 122 | |
| 123 | Returns: |
| 124 | A new Zipper focused on the right child, or None if no right child exists |
| 125 | |
| 126 | Edge Case: Focus node is None - Returns None |
| 127 | Edge Case: No right child - Returns None |
| 128 | """ |
| 129 | # Edge Case: Focus node is None |
| 130 | if self.tree is None: |
| 131 | return None |
| 132 | |
| 133 | # Edge Case: No right child |
| 134 | if self.tree["right"] is None: |
| 135 | return None |
| 136 | |
| 137 | # Create a breadcrumb to remember how to reconstruct the parent |
| 138 | breadcrumb = { |
| 139 | "parent": self.tree, |
| 140 | "side": "right", |
| 141 | "sibling": self.tree["left"] |
| 142 | } |
| 143 | |
| 144 | return Zipper(self.tree["right"], self.path + [breadcrumb]) |
| 145 | |
| 22 | 146 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 147 | """ |
| 148 | Set the right child of the focus node. |
| 149 | |
| 150 | Args: |
| 151 | tree: The new right subtree (or None to remove the right child) |
| 152 | |
| 153 | Returns: |
| 154 | A new Zipper instance with the updated right child |
| 155 | |
| 156 | Edge Case: Focus node is None - Returns a new zipper with None tree |
| 157 | """ |
| 158 | # Edge Case: Focus node is None |
| 159 | if self.tree is None: |
| 160 | return Zipper(None, self.path) |
| 161 | |
| 162 | new_tree = self.tree.copy() |
| 163 | new_tree["right"] = tree |
| 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 node, or None if already at root |
| 172 | |
| 173 | Edge Case: Already at root (no path) - Returns None |
| 174 | Edge Case: No path available - Returns None |
| 175 | """ |
| 176 | # Edge Case: Already at root or no path |
| 177 | if not self.path: |
| 178 | return None |
| 179 | |
| 180 | # Get the last breadcrumb |
| 181 | breadcrumb = self.path[-1] |
| 182 | parent = breadcrumb["parent"].copy() |
| 183 | |
| 184 | # Reconstruct the parent with the current tree in the correct position |
| 185 | if breadcrumb["side"] == "left": |
| 186 | parent["left"] = self.tree |
| 187 | parent["right"] = breadcrumb["sibling"] |
| 188 | else: # breadcrumb["side"] == "right" |
| 189 | parent["right"] = self.tree |
| 190 | parent["left"] = breadcrumb["sibling"] |
| 191 | |
| 192 | return Zipper(parent, self.path[:-1]) |
| 193 | |
| 28 | 194 | def to_tree(self) -> dict: |
| 29 | | pass |
| 195 | """ |
| 196 | Get the complete binary tree from the zipper. |
| 197 | |
| 198 | Returns: |
| 199 | The complete binary tree as a dict |
| 200 | |
| 201 | Edge Case: Current tree is None - Reconstructs from path if possible |
| 202 | """ |
| 203 | # Navigate back to the root to get the complete tree |
| 204 | current = self |
| 205 | |
| 206 | # Keep going up until we can't go further |
| 207 | while True: |
| 208 | up_zipper = current.up() |
| 209 | if up_zipper is None: |
| 210 | break |
| 211 | current = up_zipper |
| 212 | |
| 213 | return current.tree |
| 214 | |
| 215 | # Handled Edge Cases: Empty tree (None), Focus node is None, No left child, No right child, Already at root (no path), No path available |