| 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, allowing for |
| 6 | efficient navigation and modification operations. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, breadcrumbs=None): |
| 10 | """ |
| 11 | Initialize a Zipper instance. |
| 12 | |
| 13 | Args: |
| 14 | tree (dict): The current subtree focused on. |
| 15 | breadcrumbs (list): A list of breadcrumb dictionaries representing the path from root to 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 (dict): A binary tree represented as a nested dictionary. |
| 27 | |
| 28 | Returns: |
| 29 | Zipper: A new zipper with focus on the root node. |
| 30 | |
| 31 | Edge Case: Empty tree (None) is handled by creating an empty zipper. |
| 32 | """ |
| 33 | # Edge Case: Handle None tree |
| 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 | int: The value of the focus node. |
| 44 | |
| 45 | Edge Case: If the current tree is None, this would raise an error, but we assume valid state. |
| 46 | """ |
| 47 | # Handled Edge Cases: Assumes valid state (tree is not None) |
| 48 | return self.tree["value"] |
| 49 | |
| 10 | 50 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 51 | """ |
| 52 | Set the value of the focus node. |
| 53 | |
| 54 | Args: |
| 55 | value (int): The new value for the focus node. |
| 56 | |
| 57 | Returns: |
| 58 | Zipper: A new zipper with the updated value. |
| 59 | |
| 60 | Edge Case: If the current tree is None, creates a new node with the value. |
| 61 | """ |
| 62 | # Edge Case: Handle None tree |
| 63 | if self.tree is None: |
| 64 | new_tree = {"value": value, "left": None, "right": None} |
| 65 | else: |
| 66 | new_tree = {"value": value, "left": self.tree["left"], "right": self.tree["right"]} |
| 67 | return Zipper(new_tree, self.breadcrumbs) |
| 68 | |
| 13 | 69 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 70 | """ |
| 71 | Move the focus to the left child of the current node. |
| 72 | |
| 73 | Returns: |
| 74 | Zipper | None: A new zipper focused on the left child, or None if no left child exists. |
| 75 | |
| 76 | Edge Case: If there's no left child or current tree is None, returns None. |
| 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 a breadcrumb to remember how to reconstruct the parent |
| 83 | breadcrumb = { |
| 84 | "parent": { |
| 85 | "value": self.tree["value"], |
| 86 | "right": self.tree["right"] |
| 87 | }, |
| 88 | "is_left": True |
| 89 | } |
| 90 | |
| 91 | return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb]) |
| 92 | |
| 16 | 93 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 94 | """ |
| 95 | Set the left child of the focus node. |
| 96 | |
| 97 | Args: |
| 98 | tree (dict | None): The new left subtree. |
| 99 | |
| 100 | Returns: |
| 101 | Zipper: A new zipper with the updated left child. |
| 102 | |
| 103 | Edge Case: If the current tree is None, creates a new node with the provided left subtree. |
| 104 | """ |
| 105 | # Edge Case: Handle None tree |
| 106 | if self.tree is None: |
| 107 | new_tree = {"value": 0, "left": tree, "right": None} # Default value 0 |
| 108 | else: |
| 109 | new_tree = {"value": self.tree["value"], "left": tree, "right": self.tree["right"]} |
| 110 | return Zipper(new_tree, self.breadcrumbs) |
| 111 | |
| 19 | 112 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 113 | """ |
| 114 | Move the focus to the right child of the current node. |
| 115 | |
| 116 | Returns: |
| 117 | Zipper | None: A new zipper focused on the right child, or None if no right child exists. |
| 118 | |
| 119 | Edge Case: If there's no right child or current tree is None, returns None. |
| 120 | """ |
| 121 | # Edge Case: Handle None tree or missing right child |
| 122 | if self.tree is None or self.tree["right"] is None: |
| 123 | return None |
| 124 | |
| 125 | # Create a breadcrumb to remember how to reconstruct the parent |
| 126 | breadcrumb = { |
| 127 | "parent": { |
| 128 | "value": self.tree["value"], |
| 129 | "left": self.tree["left"] |
| 130 | }, |
| 131 | "is_left": False |
| 132 | } |
| 133 | |
| 134 | return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb]) |
| 135 | |
| 22 | 136 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 137 | """ |
| 138 | Set the right child of the focus node. |
| 139 | |
| 140 | Args: |
| 141 | tree (dict | None): The new right subtree. |
| 142 | |
| 143 | Returns: |
| 144 | Zipper: A new zipper with the updated right child. |
| 145 | |
| 146 | Edge Case: If the current tree is None, creates a new node with the provided right subtree. |
| 147 | """ |
| 148 | # Edge Case: Handle None tree |
| 149 | if self.tree is None: |
| 150 | new_tree = {"value": 0, "left": None, "right": tree} # Default value 0 |
| 151 | else: |
| 152 | new_tree = {"value": self.tree["value"], "left": self.tree["left"], "right": tree} |
| 153 | return Zipper(new_tree, self.breadcrumbs) |
| 154 | |
| 25 | 155 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 156 | """ |
| 157 | Move the focus to the parent of the current node. |
| 158 | |
| 159 | Returns: |
| 160 | Zipper | None: A new zipper focused on the parent, or None if already at root. |
| 161 | |
| 162 | Edge Case: If already at root (no breadcrumbs), returns None. |
| 163 | """ |
| 164 | # Edge Case: Handle already at root |
| 165 | if not self.breadcrumbs: |
| 166 | return None |
| 167 | |
| 168 | # Get the last breadcrumb |
| 169 | last_breadcrumb = self.breadcrumbs[-1] |
| 170 | parent = last_breadcrumb["parent"] |
| 171 | is_left = last_breadcrumb["is_left"] |
| 172 | |
| 173 | # Reconstruct the parent tree with the current tree in the correct position |
| 174 | if is_left: |
| 175 | reconstructed_tree = { |
| 176 | "value": parent["value"], |
| 177 | "left": self.tree, |
| 178 | "right": parent["right"] |
| 179 | } |
| 180 | else: |
| 181 | reconstructed_tree = { |
| 182 | "value": parent["value"], |
| 183 | "left": parent["left"], |
| 184 | "right": self.tree |
| 185 | } |
| 186 | |
| 187 | return Zipper(reconstructed_tree, self.breadcrumbs[:-1]) |
| 188 | |
| 28 | 189 | def to_tree(self) -> dict: |
| 29 | | pass |
| 190 | """ |
| 191 | Get the entire binary tree from the zipper. |
| 192 | |
| 193 | Returns: |
| 194 | dict: The complete binary tree. |
| 195 | |
| 196 | Edge Case: If the zipper is focused on a None node, reconstructs the tree from breadcrumbs. |
| 197 | """ |
| 198 | # Start with the current tree |
| 199 | current = self.tree |
| 200 | |
| 201 | # Work backwards through breadcrumbs to reconstruct the full tree |
| 202 | for breadcrumb in reversed(self.breadcrumbs): |
| 203 | parent = breadcrumb["parent"] |
| 204 | is_left = breadcrumb["is_left"] |
| 205 | |
| 206 | if is_left: |
| 207 | current = { |
| 208 | "value": parent["value"], |
| 209 | "left": current, |
| 210 | "right": parent["right"] |
| 211 | } |
| 212 | else: |
| 213 | current = { |
| 214 | "value": parent["value"], |
| 215 | "left": parent["left"], |
| 216 | "right": current |
| 217 | } |
| 218 | |
| 219 | return current |
| 220 | |
| 221 | # Handled Edge Cases: value(), set_value(), left(), set_left(), right(), set_right(), up(), to_tree() |