| 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 | traversal and modification operations while preserving immutability. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, breadcrumbs=None): |
| 10 | """ |
| 11 | Initialize a Zipper. |
| 12 | |
| 13 | Args: |
| 14 | tree: The current subtree (focus) |
| 15 | breadcrumbs: Path information to navigate back up the tree |
| 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: A binary tree represented as a dict with keys "value", "left", "right" |
| 27 | |
| 6 | 28 | |
| 29 | Returns: |
| 30 | A new Zipper instance focused on the root of the tree |
| 31 | """ |
| 32 | # Edge Case: Handle None input tree |
| 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 | 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. |
| 53 | |
| 12 | 54 | |
| 55 | Args: |
| 56 | value: The new value for the focus node |
| 57 | |
| 58 | |
| 59 | Returns: |
| 60 | 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. |
| 75 | |
| 15 | 76 | |
| 77 | Returns: |
| 78 | A new Zipper focused on the left child, or None if no left child exists |
| 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 how to get back to this node |
| 85 | breadcrumb = { |
| 86 | "parent": { |
| 87 | "value": self.tree["value"], |
| 88 | "right": self.tree["right"] |
| 89 | }, |
| 90 | "side": "left" |
| 91 | } |
| 92 | |
| 93 | return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb]) |
| 94 | |
| 16 | 95 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 96 | """ |
| 97 | Set the left child of the focus node. |
| 98 | |
| 99 | |
| 100 | Args: |
| 101 | tree: The new left subtree (or None to remove the left child) |
| 102 | |
| 18 | 103 | |
| 104 | Returns: |
| 105 | A new Zipper with the updated left child |
| 106 | """ |
| 107 | # Edge Case: Handle None tree |
| 108 | if self.tree is None: |
| 109 | return self |
| 110 | new_tree = { |
| 111 | "value": self.tree["value"], |
| 112 | "left": tree, |
| 113 | "right": self.tree["right"] |
| 114 | } |
| 115 | return Zipper(new_tree, self.breadcrumbs) |
| 116 | |
| 19 | 117 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 118 | """ |
| 119 | Move the focus to the right child. |
| 120 | |
| 21 | 121 | |
| 122 | Returns: |
| 123 | A new Zipper focused on the right child, or None if no right child exists |
| 124 | """ |
| 125 | # Edge Case: Handle None tree |
| 126 | if self.tree is None or self.tree["right"] is None: |
| 127 | return None |
| 128 | |
| 129 | # Create breadcrumb to remember how to get back to this node |
| 130 | breadcrumb = { |
| 131 | "parent": { |
| 132 | "value": self.tree["value"], |
| 133 | "left": self.tree["left"] |
| 134 | }, |
| 135 | "side": "right" |
| 136 | } |
| 137 | |
| 138 | return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb]) |
| 139 | |
| 22 | 140 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 141 | """ |
| 142 | Set the right child of the focus node. |
| 143 | |
| 24 | 144 | |
| 145 | Args: |
| 146 | tree: The new right subtree (or None to remove the right child) |
| 147 | |
| 148 | |
| 149 | Returns: |
| 150 | A new Zipper with the updated right child |
| 151 | """ |
| 152 | # Edge Case: Handle None tree |
| 153 | if self.tree is None: |
| 154 | return self |
| 155 | new_tree = { |
| 156 | "value": self.tree["value"], |
| 157 | "left": self.tree["left"], |
| 158 | "right": tree |
| 159 | } |
| 160 | return Zipper(new_tree, self.breadcrumbs) |
| 161 | |
| 25 | 162 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 163 | """ |
| 164 | Move the focus to the parent node. |
| 165 | |
| 27 | 166 | |
| 167 | Returns: |
| 168 | A new Zipper focused on the parent, or None if already at the root |
| 169 | """ |
| 170 | # Edge Case: Handle when already at root (no breadcrumbs) |
| 171 | if not self.breadcrumbs: |
| 172 | return None |
| 173 | |
| 174 | # Get the last breadcrumb |
| 175 | last_breadcrumb = self.breadcrumbs[-1] |
| 176 | parent = last_breadcrumb["parent"] |
| 177 | side = last_breadcrumb["side"] |
| 178 | |
| 179 | # Reconstruct the parent tree with the current subtree in the correct position |
| 180 | if side == "left": |
| 181 | new_tree = { |
| 182 | "value": parent["value"], |
| 183 | "left": self.tree, |
| 184 | "right": parent["right"] |
| 185 | } |
| 186 | else: # side == "right" |
| 187 | new_tree = { |
| 188 | "value": parent["value"], |
| 189 | "left": parent["left"], |
| 190 | "right": self.tree |
| 191 | } |
| 192 | |
| 193 | return Zipper(new_tree, self.breadcrumbs[:-1]) |
| 194 | |
| 28 | 195 | def to_tree(self) -> dict: |
| 29 | | pass |
| 196 | """ |
| 197 | Get the complete tree from the zipper. |
| 198 | |
| 199 | |
| 200 | Returns: |
| 201 | The complete binary tree |
| 202 | """ |
| 203 | # Navigate back to the root to get the complete tree |
| 204 | current = self |
| 205 | while True: |
| 206 | parent = current.up() |
| 207 | if parent is None: |
| 208 | break |
| 209 | current = parent |
| 210 | return current.tree |
| 211 | |
| 212 | # Handled Edge Cases: None input tree, None tree in zipper, no left child, no right child, already at root |