| 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 contains a data structure and a pointer (focus) into that structure. |
| 6 | It supports moving through the tree and modifying it in a functional way. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, breadcrumbs=None): |
| 10 | """ |
| 11 | Initialize a Zipper. |
| 12 | |
| 13 | Args: |
| 14 | tree: The current subtree (focus) as a dict with keys "value", "left", "right" |
| 15 | breadcrumbs: A list of breadcrumb dicts tracking the path to the focus |
| 16 | """ |
| 17 | self.tree = tree |
| 18 | self.breadcrumbs = breadcrumbs 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 as a dict with keys "value", "left", "right" |
| 27 | |
| 28 | Returns: |
| 29 | A new Zipper with focus on the root node |
| 30 | |
| 31 | Edge Cases: |
| 32 | - Empty tree (None) is handled by returning None in other operations |
| 33 | """ |
| 34 | # Edge Case: Handle None tree |
| 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 | The value of the focus node |
| 45 | |
| 46 | Edge Cases: |
| 47 | - If the zipper is None, this method won't be called |
| 48 | - Assumes the tree structure is valid |
| 49 | """ |
| 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 with the updated value |
| 61 | |
| 62 | Edge Cases: |
| 63 | - Creates a new zipper, preserving immutability |
| 64 | """ |
| 65 | # Edge Case: Create a new tree with updated value |
| 66 | new_tree = { |
| 67 | "value": value, |
| 68 | "left": self.tree["left"], |
| 69 | "right": self.tree["right"] |
| 70 | } |
| 71 | return Zipper(new_tree, self.breadcrumbs) |
| 72 | |
| 13 | 73 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 74 | """ |
| 75 | Move the focus to the left child of the current node. |
| 76 | |
| 77 | Returns: |
| 78 | A new Zipper focused on the left child, or None if no left child exists |
| 79 | |
| 80 | Edge Cases: |
| 81 | - Returns None if there is no left child |
| 82 | - Preserves immutability by creating a new zipper |
| 83 | """ |
| 84 | # Edge Case: No left child |
| 85 | if self.tree["left"] is None: |
| 86 | return None |
| 87 | |
| 88 | # Create a breadcrumb to remember how to reconstruct the parent |
| 89 | breadcrumb = { |
| 90 | "parent": { |
| 91 | "value": self.tree["value"], |
| 92 | "right": self.tree["right"] |
| 93 | }, |
| 94 | "is_left": True |
| 95 | } |
| 96 | |
| 97 | return Zipper(self.tree["left"], self.breadcrumbs + [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 with the updated left child |
| 108 | |
| 109 | Edge Cases: |
| 110 | - Handles setting left child to None |
| 111 | - Preserves immutability |
| 112 | """ |
| 113 | # Edge Case: Create a new tree with updated left child |
| 114 | new_tree = { |
| 115 | "value": self.tree["value"], |
| 116 | "left": tree, |
| 117 | "right": self.tree["right"] |
| 118 | } |
| 119 | return Zipper(new_tree, self.breadcrumbs) |
| 120 | |
| 19 | 121 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 122 | """ |
| 123 | Move the focus to the right child of the current node. |
| 124 | |
| 125 | Returns: |
| 126 | A new Zipper focused on the right child, or None if no right child exists |
| 127 | |
| 128 | Edge Cases: |
| 129 | - Returns None if there is no right child |
| 130 | - Preserves immutability by creating a new zipper |
| 131 | """ |
| 132 | # Edge Case: No right child |
| 133 | if self.tree["right"] is None: |
| 134 | return None |
| 135 | |
| 136 | # Create a breadcrumb to remember how to reconstruct the parent |
| 137 | breadcrumb = { |
| 138 | "parent": { |
| 139 | "value": self.tree["value"], |
| 140 | "left": self.tree["left"] |
| 141 | }, |
| 142 | "is_left": False |
| 143 | } |
| 144 | |
| 145 | return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb]) |
| 146 | |
| 22 | 147 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 148 | """ |
| 149 | Set the right child of the focus node. |
| 150 | |
| 151 | Args: |
| 152 | tree: The new right subtree (or None to remove the right child) |
| 153 | |
| 154 | Returns: |
| 155 | A new Zipper with the updated right child |
| 156 | |
| 157 | Edge Cases: |
| 158 | - Handles setting right child to None |
| 159 | - Preserves immutability |
| 160 | """ |
| 161 | # Edge Case: Create a new tree with updated right child |
| 162 | new_tree = { |
| 163 | "value": self.tree["value"], |
| 164 | "left": self.tree["left"], |
| 165 | "right": tree |
| 166 | } |
| 167 | return Zipper(new_tree, self.breadcrumbs) |
| 168 | |
| 25 | 169 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 170 | """ |
| 171 | Move the focus to the parent of the current node. |
| 172 | |
| 173 | Returns: |
| 174 | A new Zipper focused on the parent, or None if already at the root |
| 175 | |
| 176 | Edge Cases: |
| 177 | - Returns None if already at the root (no breadcrumbs) |
| 178 | - Reconstructs the parent tree correctly based on breadcrumb information |
| 179 | """ |
| 180 | # Edge Case: Already at root (no breadcrumbs) |
| 181 | if not self.breadcrumbs: |
| 182 | return None |
| 183 | |
| 184 | # Get the last breadcrumb |
| 185 | breadcrumb = self.breadcrumbs[-1] |
| 186 | rest_breadcrumbs = self.breadcrumbs[:-1] |
| 187 | |
| 188 | parent_info = breadcrumb["parent"] |
| 189 | |
| 190 | # Reconstruct the parent tree |
| 191 | if breadcrumb["is_left"]: |
| 192 | # Current focus was the left child |
| 193 | new_tree = { |
| 194 | "value": parent_info["value"], |
| 195 | "left": self.tree, |
| 196 | "right": parent_info["right"] |
| 197 | } |
| 198 | else: |
| 199 | # Current focus was the right child |
| 200 | new_tree = { |
| 201 | "value": parent_info["value"], |
| 202 | "left": parent_info["left"], |
| 203 | "right": self.tree |
| 204 | } |
| 205 | |
| 206 | return Zipper(new_tree, rest_breadcrumbs) |
| 207 | |
| 28 | 208 | def to_tree(self) -> dict: |
| 29 | | pass |
| 209 | """ |
| 210 | Get the complete binary tree from the zipper. |
| 211 | |
| 212 | Returns: |
| 213 | The complete binary tree as a dict |
| 214 | |
| 215 | Edge Cases: |
| 216 | - Navigates back to root before returning the tree |
| 217 | - Handles empty tree case |
| 218 | """ |
| 219 | # Edge Case: Navigate back to root |
| 220 | current = self |
| 221 | while current.breadcrumbs: |
| 222 | current = current.up() |
| 223 | # Edge Case: Handle potential None return from up() |
| 224 | if current is None: |
| 225 | break |
| 226 | |
| 227 | # Edge Case: Return the tree at root |
| 228 | return current.tree if current else None |
| 229 | |
| 230 | # Handled Edge Cases: Empty tree, None children, immutability, navigation boundaries |
| 231 | # Handled Edge Cases: Value setting, child setting, tree reconstruction |
| 232 | # Handled Edge Cases: Root navigation, breadcrumb management |