| 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 immutability. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, breadcrumbs=None): |
| 10 | """ |
| 11 | Initialize a Zipper with a tree and optional breadcrumbs. |
| 12 | |
| 13 | Args: |
| 14 | tree (dict): The current subtree with keys 'value', 'left', 'right' |
| 15 | breadcrumbs (list): List of breadcrumb dictionaries for navigation |
| 16 | """ |
| 17 | self.tree = tree |
| 18 | self.breadcrumbs = breadcrumbs or [] |
| 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 with keys 'value', 'left', 'right' |
| 27 | |
| 6 | 28 | |
| 29 | Returns: |
| 30 | Zipper: A new zipper focused on the root of the tree |
| 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 current node |
| 44 | """ |
| 45 | # Edge Case: Handle None tree |
| 46 | if self.tree is None: |
| 47 | return None |
| 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 | |
| 12 | 54 | |
| 55 | Args: |
| 56 | value (int): The new value for the node |
| 57 | |
| 58 | |
| 59 | Returns: |
| 60 | Zipper: A new zipper with the updated value |
| 61 | """ |
| 62 | # Edge Case: Handle None tree |
| 63 | if self.tree is None: |
| 64 | return self |
| 65 | new_tree = { |
| 66 | "value": value, |
| 67 | "left": self.tree["left"], |
| 68 | "right": self.tree["right"] |
| 69 | } |
| 70 | return Zipper(new_tree, self.breadcrumbs) |
| 71 | |
| 13 | 72 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 73 | """ |
| 74 | Move the focus to the left child, returning a new zipper. |
| 75 | |
| 15 | 76 | |
| 77 | Returns: |
| 78 | Zipper | None: A new zipper focused on the left child, or None if no left child |
| 79 | """ |
| 80 | # Edge Case: Handle None tree |
| 81 | if self.tree is None or self.tree["left"] is None: |
| 82 | return None |
| 83 | |
| 84 | # Create breadcrumb to remember current node when going back up |
| 85 | breadcrumb = { |
| 86 | "parent": { |
| 87 | "value": self.tree["value"], |
| 88 | "right": self.tree["right"] |
| 89 | }, |
| 90 | "side": "left" |
| 91 | } |
| 92 | |
| 93 | new_breadcrumbs = [breadcrumb] + self.breadcrumbs |
| 94 | return Zipper(self.tree["left"], new_breadcrumbs) |
| 95 | |
| 16 | 96 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 97 | """ |
| 98 | Set the left child of the focus node, returning a new zipper. |
| 99 | |
| 100 | |
| 101 | Args: |
| 102 | tree (dict | None): The new left subtree |
| 103 | |
| 18 | 104 | |
| 105 | Returns: |
| 106 | Zipper: A new zipper with the updated left child |
| 107 | """ |
| 108 | # Edge Case: Handle None tree |
| 109 | if self.tree is None: |
| 110 | return self |
| 111 | new_tree = { |
| 112 | "value": self.tree["value"], |
| 113 | "left": tree, |
| 114 | "right": self.tree["right"] |
| 115 | } |
| 116 | return Zipper(new_tree, self.breadcrumbs) |
| 117 | |
| 19 | 118 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 119 | """ |
| 120 | Move the focus to the right child, returning a new zipper. |
| 121 | |
| 21 | 122 | |
| 123 | Returns: |
| 124 | Zipper | None: A new zipper focused on the right child, or None if no right child |
| 125 | """ |
| 126 | # Edge Case: Handle None tree |
| 127 | if self.tree is None or self.tree["right"] is None: |
| 128 | return None |
| 129 | |
| 130 | # Create breadcrumb to remember current node when going back up |
| 131 | breadcrumb = { |
| 132 | "parent": { |
| 133 | "value": self.tree["value"], |
| 134 | "left": self.tree["left"] |
| 135 | }, |
| 136 | "side": "right" |
| 137 | } |
| 138 | |
| 139 | new_breadcrumbs = [breadcrumb] + self.breadcrumbs |
| 140 | return Zipper(self.tree["right"], new_breadcrumbs) |
| 141 | |
| 22 | 142 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 143 | """ |
| 144 | Set the right child of the focus node, returning a new zipper. |
| 145 | |
| 24 | 146 | |
| 147 | Args: |
| 148 | tree (dict | None): The new right subtree |
| 149 | |
| 150 | |
| 151 | Returns: |
| 152 | Zipper: A new zipper with the updated right child |
| 153 | """ |
| 154 | # Edge Case: Handle None tree |
| 155 | if self.tree is None: |
| 156 | return self |
| 157 | new_tree = { |
| 158 | "value": self.tree["value"], |
| 159 | "left": self.tree["left"], |
| 160 | "right": tree |
| 161 | } |
| 162 | return Zipper(new_tree, self.breadcrumbs) |
| 163 | |
| 25 | 164 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 165 | """ |
| 166 | Move the focus to the parent node, returning a new zipper. |
| 167 | |
| 27 | 168 | |
| 169 | Returns: |
| 170 | Zipper | None: A new zipper focused on the parent, or None if at root |
| 171 | """ |
| 172 | # Edge Case: Handle when already at root (no breadcrumbs) |
| 173 | if not self.breadcrumbs: |
| 174 | return None |
| 175 | |
| 176 | # Get the most recent breadcrumb |
| 177 | breadcrumb = self.breadcrumbs[0] |
| 178 | parent = breadcrumb["parent"] |
| 179 | side = breadcrumb["side"] |
| 180 | |
| 181 | # Reconstruct the parent node with the current tree in the correct position |
| 182 | if side == "left": |
| 183 | new_tree = { |
| 184 | "value": parent["value"], |
| 185 | "left": self.tree, |
| 186 | "right": parent["right"] |
| 187 | } |
| 188 | else: # side == "right" |
| 189 | new_tree = { |
| 190 | "value": parent["value"], |
| 191 | "left": parent["left"], |
| 192 | "right": self.tree |
| 193 | } |
| 194 | |
| 195 | # Return a new zipper with the reconstructed parent and remaining breadcrumbs |
| 196 | return Zipper(new_tree, self.breadcrumbs[1:]) |
| 197 | |
| 28 | 198 | def to_tree(self) -> dict: |
| 29 | | pass |
| 199 | """ |
| 200 | Get the entire binary tree from the zipper. |
| 201 | |
| 202 | |
| 203 | Returns: |
| 204 | dict: The complete binary tree |
| 205 | """ |
| 206 | # Start with the current tree |
| 207 | result = self.tree |
| 208 | |
| 209 | # Work through breadcrumbs to rebuild the full tree |
| 210 | for breadcrumb in self.breadcrumbs: |
| 211 | parent = breadcrumb["parent"] |
| 212 | side = breadcrumb["side"] |
| 213 | |
| 214 | # Reconstruct the parent with the current result in the correct position |
| 215 | if side == "left": |
| 216 | result = { |
| 217 | "value": parent["value"], |
| 218 | "left": result, |
| 219 | "right": parent["right"] |
| 220 | } |
| 221 | else: # side == "right" |
| 222 | result = { |
| 223 | "value": parent["value"], |
| 224 | "left": parent["left"], |
| 225 | "right": result |
| 226 | } |
| 227 | |
| 228 | return result |
| 229 | |
| 230 | # Handled Edge Cases: None tree input, None left/right children, Already at root |
| 231 | # Handled Edge Cases: None tree, None left child, Already at root |
| 232 | # Handled Edge Cases: None tree |
| 233 | # Handled Edge Cases: None tree, None left child, Already at root |
| 234 | # Handled Edge Cases: None tree |
| 235 | # Handled Edge Cases: None tree, None right child, Already at root |
| 236 | # Handled Edge Cases: None tree |
| 237 | # Handled Edge Cases: Already at root (no breadcrumbs) |
| 238 | # Handled Edge Cases: None |