| 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 | navigation and modification operations while preserving the tree structure. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, breadcrumbs=None): |
| 10 | """ |
| 11 | Initialize a Zipper instance. |
| 12 | |
| 13 | Args: |
| 14 | tree (dict): The current subtree with keys 'value', 'left', 'right' |
| 15 | breadcrumbs (list): List of parent contexts 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 | return self.tree["value"] |
| 46 | |
| 10 | 47 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 48 | """ |
| 49 | Set the value of the focus node. |
| 50 | |
| 12 | 51 | |
| 52 | Args: |
| 53 | value (int): The new value for the focus node |
| 54 | |
| 55 | |
| 56 | Returns: |
| 57 | Zipper: A new zipper with the updated value |
| 58 | """ |
| 59 | # Edge Case: Handle None tree |
| 60 | if self.tree is None: |
| 61 | return self |
| 62 | |
| 63 | new_tree = { |
| 64 | "value": value, |
| 65 | "left": self.tree["left"], |
| 66 | "right": self.tree["right"] |
| 67 | } |
| 68 | return Zipper(new_tree, self.breadcrumbs) |
| 69 | |
| 13 | 70 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 71 | """ |
| 72 | Move the focus to the left child of the current node. |
| 73 | |
| 15 | 74 | |
| 75 | Returns: |
| 76 | Zipper | None: A new zipper focused on the left child, or None if no left child |
| 77 | """ |
| 78 | # Edge Case: Handle None tree or missing left child |
| 79 | if self.tree is None or self.tree["left"] is None: |
| 80 | return None |
| 81 | |
| 82 | # Create breadcrumb to remember current node when going down |
| 83 | breadcrumb = { |
| 84 | "parent": { |
| 85 | "value": self.tree["value"], |
| 86 | "right": self.tree["right"] |
| 87 | }, |
| 88 | "side": "left" |
| 89 | } |
| 90 | |
| 91 | return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb]) |
| 92 | |
| 16 | 93 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 94 | """ |
| 95 | Set the left child of the focus node. |
| 96 | |
| 97 | |
| 98 | Args: |
| 99 | tree (dict | None): The new left subtree or None |
| 100 | |
| 18 | 101 | |
| 102 | Returns: |
| 103 | Zipper: A new zipper with the updated left child |
| 104 | """ |
| 105 | # Edge Case: Handle None tree |
| 106 | if self.tree is None: |
| 107 | return self |
| 108 | |
| 109 | new_tree = { |
| 110 | "value": self.tree["value"], |
| 111 | "left": tree, |
| 112 | "right": self.tree["right"] |
| 113 | } |
| 114 | return Zipper(new_tree, self.breadcrumbs) |
| 115 | |
| 19 | 116 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 117 | """ |
| 118 | Move the focus to the right child of the current node. |
| 119 | |
| 21 | 120 | |
| 121 | Returns: |
| 122 | Zipper | None: A new zipper focused on the right child, or None if no right child |
| 123 | """ |
| 124 | # Edge Case: Handle None tree or missing right child |
| 125 | if self.tree is None or self.tree["right"] is None: |
| 126 | return None |
| 127 | |
| 128 | # Create breadcrumb to remember current node when going down |
| 129 | breadcrumb = { |
| 130 | "parent": { |
| 131 | "value": self.tree["value"], |
| 132 | "left": self.tree["left"] |
| 133 | }, |
| 134 | "side": "right" |
| 135 | } |
| 136 | |
| 137 | return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb]) |
| 138 | |
| 22 | 139 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 140 | """ |
| 141 | Set the right child of the focus node. |
| 142 | |
| 143 | |
| 144 | Args: |
| 145 | tree (dict | None): The new right subtree or None |
| 146 | |
| 24 | 147 | |
| 148 | Returns: |
| 149 | Zipper: A new zipper with the updated right child |
| 150 | """ |
| 151 | # Edge Case: Handle None tree |
| 152 | if self.tree is None: |
| 153 | return self |
| 154 | |
| 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 of the current node. |
| 165 | |
| 27 | 166 | |
| 167 | Returns: |
| 168 | Zipper | None: A new zipper focused on the parent, or None if at 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 (most recent parent) |
| 175 | parent_breadcrumb = self.breadcrumbs[-1] |
| 176 | parent = parent_breadcrumb["parent"] |
| 177 | side = parent_breadcrumb["side"] |
| 178 | |
| 179 | # Reconstruct the parent tree with 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 entire tree from the zipper. |
| 198 | |
| 199 | |
| 200 | Returns: |
| 201 | dict: The complete tree rooted at the top of the zipper |
| 202 | """ |
| 203 | # Navigate up to the root of the tree |
| 204 | current = self |
| 205 | while True: |
| 206 | parent = current.up() |
| 207 | if parent is None: |
| 208 | break |
| 209 | current = parent |
| 210 | |
| 211 | return current.tree |
| 212 | |
| 213 | # Handled Edge Cases: None tree input, missing children, already at root |