| 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, value: int, left: dict | None, right: dict | None, path: list): |
| 10 | """ |
| 11 | Initialize a Zipper instance. |
| 12 | |
| 13 | Args: |
| 14 | value: The value of the current focus node |
| 15 | left: The left subtree of the current focus node |
| 16 | right: The right subtree of the current focus node |
| 17 | path: A list of breadcrumbs representing the path from root to current node |
| 18 | """ |
| 19 | self._value = value |
| 20 | self._left = left |
| 21 | self._right = right |
| 22 | self._path = path |
| 23 | |
| 3 | 24 | @staticmethod |
| 4 | 25 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 26 | """ |
| 27 | Create a zipper from a binary tree. |
| 28 | |
| 29 | Args: |
| 30 | tree: A dictionary representation of a binary tree with keys 'value', 'left', and 'right' |
| 31 | |
| 32 | Returns: |
| 33 | A new Zipper instance focused on the root of the tree |
| 34 | |
| 35 | Edge Case: Empty tree (None) - Returns None |
| 36 | """ |
| 37 | # Edge Case: Empty tree |
| 38 | if tree is None: |
| 39 | return None |
| 40 | return Zipper(tree["value"], tree["left"], tree["right"], []) |
| 41 | |
| 7 | 42 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 43 | """ |
| 44 | Get the value of the focus node. |
| 45 | |
| 46 | Returns: |
| 47 | The value of the current focus node |
| 48 | """ |
| 49 | return self._value |
| 50 | |
| 10 | 51 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 52 | """ |
| 53 | Set the value of the focus node, returning a new zipper. |
| 54 | |
| 55 | Args: |
| 56 | value: The new value for the focus node |
| 57 | |
| 58 | Returns: |
| 59 | A new Zipper instance with the updated value |
| 60 | """ |
| 61 | return Zipper(value, self._left, self._right, self._path) |
| 62 | |
| 13 | 63 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 64 | """ |
| 65 | Move the focus to the left child of the current node. |
| 66 | |
| 67 | Returns: |
| 68 | A new Zipper focused on the left child, or None if no left child exists |
| 69 | |
| 70 | Edge Case: No left child - Returns None |
| 71 | """ |
| 72 | # Edge Case: No left child |
| 73 | if self._left is None: |
| 74 | return None |
| 75 | |
| 76 | # Create a breadcrumb to remember the current node when we move down |
| 77 | breadcrumb = { |
| 78 | "parent_value": self._value, |
| 79 | "left_sibling": None, # No left sibling when moving to left child |
| 80 | "right_sibling": self._right, |
| 81 | "direction": "left" |
| 82 | } |
| 83 | |
| 84 | return Zipper(self._left["value"], self._left["left"], self._left["right"], |
| 85 | self._path + [breadcrumb]) |
| 86 | |
| 16 | 87 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 88 | """ |
| 89 | Set the left subtree of the focus node, returning a new zipper. |
| 90 | |
| 91 | Args: |
| 92 | tree: A dictionary representation of a binary tree (or None) |
| 93 | |
| 94 | Returns: |
| 95 | A new Zipper instance with the updated left subtree |
| 96 | """ |
| 97 | return Zipper(self._value, tree, self._right, self._path) |
| 98 | |
| 19 | 99 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 100 | """ |
| 101 | Move the focus to the right child of the current node. |
| 102 | |
| 103 | Returns: |
| 104 | A new Zipper focused on the right child, or None if no right child exists |
| 105 | |
| 106 | Edge Case: No right child - Returns None |
| 107 | """ |
| 108 | # Edge Case: No right child |
| 109 | if self._right is None: |
| 110 | return None |
| 111 | |
| 112 | # Create a breadcrumb to remember the current node when we move down |
| 113 | breadcrumb = { |
| 114 | "parent_value": self._value, |
| 115 | "left_sibling": self._left, |
| 116 | "right_sibling": None, # No right sibling when moving to right child |
| 117 | "direction": "right" |
| 118 | } |
| 119 | |
| 120 | return Zipper(self._right["value"], self._right["left"], self._right["right"], |
| 121 | self._path + [breadcrumb]) |
| 122 | |
| 22 | 123 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 124 | """ |
| 125 | Set the right subtree of the focus node, returning a new zipper. |
| 126 | |
| 127 | Args: |
| 128 | tree: A dictionary representation of a binary tree (or None) |
| 129 | |
| 130 | Returns: |
| 131 | A new Zipper instance with the updated right subtree |
| 132 | """ |
| 133 | return Zipper(self._value, self._left, tree, self._path) |
| 134 | |
| 25 | 135 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 136 | """ |
| 137 | Move the focus to the parent of the current node. |
| 138 | |
| 139 | Returns: |
| 140 | A new Zipper focused on the parent node, or None if already at root |
| 141 | |
| 142 | Edge Case: Already at root - Returns None |
| 143 | """ |
| 144 | # Edge Case: Already at root (no path) |
| 145 | if not self._path: |
| 146 | return None |
| 147 | |
| 148 | # Get the last breadcrumb (parent information) |
| 149 | parent_info = self._path[-1] |
| 150 | |
| 151 | # Reconstruct the parent node with current node as the appropriate child |
| 152 | if parent_info["direction"] == "left": |
| 153 | # Current node was the left child of its parent |
| 154 | current_node = { |
| 155 | "value": self._value, |
| 156 | "left": self._left, |
| 157 | "right": self._right |
| 158 | } |
| 159 | return Zipper( |
| 160 | parent_info["parent_value"], |
| 161 | current_node, # Current node becomes left child |
| 162 | parent_info["right_sibling"], # Right sibling becomes right child |
| 163 | self._path[:-1] # Remove last breadcrumb from path |
| 164 | ) |
| 165 | else: # direction == "right" |
| 166 | # Current node was the right child of its parent |
| 167 | current_node = { |
| 168 | "value": self._value, |
| 169 | "left": self._left, |
| 170 | "right": self._right |
| 171 | } |
| 172 | return Zipper( |
| 173 | parent_info["parent_value"], |
| 174 | parent_info["left_sibling"], # Left sibling becomes left child |
| 175 | current_node, # Current node becomes right child |
| 176 | self._path[:-1] # Remove last breadcrumb from path |
| 177 | ) |
| 178 | |
| 28 | 179 | def to_tree(self) -> dict: |
| 29 | | pass |
| 180 | """ |
| 181 | Get the complete binary tree from the zipper. |
| 182 | |
| 183 | Returns: |
| 184 | A dictionary representation of the complete binary tree |
| 185 | """ |
| 186 | # Start with the current node |
| 187 | tree = { |
| 188 | "value": self._value, |
| 189 | "left": self._left, |
| 190 | "right": self._right |
| 191 | } |
| 192 | |
| 193 | # Traverse up the path to reconstruct the full tree |
| 194 | for breadcrumb in reversed(self._path): |
| 195 | if breadcrumb["direction"] == "left": |
| 196 | # Current tree was the left child |
| 197 | tree = { |
| 198 | "value": breadcrumb["parent_value"], |
| 199 | "left": tree, |
| 200 | "right": breadcrumb["right_sibling"] |
| 201 | } |
| 202 | else: # direction == "right" |
| 203 | # Current tree was the right child |
| 204 | tree = { |
| 205 | "value": breadcrumb["parent_value"], |
| 206 | "left": breadcrumb["left_sibling"], |
| 207 | "right": tree |
| 208 | } |
| 209 | |
| 210 | return tree |
| 211 | |
| 212 | # Handled Edge Cases: Empty tree in from_tree, No left child in left, No right child in right, Already at root in up |