| 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 a binary tree that allows navigation and manipulation. |
| 4 | |
| 5 | The zipper maintains a focus on a specific node in the tree and allows |
| 6 | movement to parent, left child, and right child nodes. It also supports |
| 7 | modifying the value of the current node and replacing subtrees. |
| 8 | |
| 9 | Attributes: |
| 10 | _focus (dict): The current node in focus |
| 11 | _path (list): A list of tuples representing the path from root to focus |
| 12 | Each tuple contains (parent_node, side) where side is 'left' or 'right' |
| 13 | """ |
| 14 | |
| 15 | def __init__(self, focus: dict, path: list): |
| 16 | """ |
| 17 | Initialize a Zipper with a focus node and path. |
| 18 | |
| 19 | Args: |
| 20 | focus (dict): The node currently in focus |
| 21 | path (list): Path from root to focus as list of (parent_node, side) tuples |
| 22 | """ |
| 23 | self._focus = focus |
| 24 | self._path = path |
| 25 | |
| 3 | 26 | @staticmethod |
| 4 | 27 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 28 | """ |
| 29 | Create a zipper from a binary tree with focus on the root node. |
| 30 | |
| 31 | Args: |
| 32 | tree (dict): A binary tree represented as nested dictionaries |
| 33 | with keys "value", "left", and "right" |
| 34 | |
| 35 | Returns: |
| 36 | Zipper: A new zipper with focus on the root of the tree |
| 37 | |
| 38 | # Edge Case: Empty tree (None) |
| 39 | """ |
| 40 | if tree is None: |
| 41 | return None |
| 42 | return Zipper(tree, []) |
| 43 | |
| 7 | 44 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 45 | """ |
| 46 | Get the value of the focus node. |
| 47 | |
| 48 | Returns: |
| 49 | int: The value stored in the focus node |
| 50 | |
| 51 | # Edge Case: Called on None zipper |
| 52 | """ |
| 53 | if self is None: |
| 54 | return None |
| 55 | return self._focus["value"] |
| 56 | |
| 10 | 57 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 58 | """ |
| 59 | Set the value of the focus node, returning a new zipper. |
| 60 | |
| 61 | Args: |
| 62 | value (int): The new value for the focus node |
| 63 | |
| 64 | Returns: |
| 65 | Zipper: A new zipper with updated focus node value |
| 66 | |
| 67 | # Edge Case: Called on None zipper |
| 68 | """ |
| 69 | if self is None: |
| 70 | return None |
| 71 | new_focus = { |
| 72 | "value": value, |
| 73 | "left": self._focus["left"], |
| 74 | "right": self._focus["right"] |
| 75 | } |
| 76 | return Zipper(new_focus, self._path) |
| 77 | |
| 13 | 78 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 79 | """ |
| 80 | Move focus to the left child of the current node. |
| 81 | |
| 82 | Returns: |
| 83 | Zipper | None: A new zipper with focus on the left child, |
| 84 | or None if there is no left child |
| 85 | |
| 86 | # Edge Case: Called on None zipper |
| 87 | # Edge Case: No left child exists |
| 88 | """ |
| 89 | if self is None: |
| 90 | return None |
| 91 | if self._focus["left"] is None: |
| 92 | return None |
| 93 | new_path = self._path + [(self._focus, "left")] |
| 94 | return Zipper(self._focus["left"], new_path) |
| 95 | |
| 16 | 96 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 97 | """ |
| 98 | Replace the left subtree of the focus node. |
| 99 | |
| 100 | Args: |
| 101 | tree (dict | None): The new left subtree or None |
| 102 | |
| 103 | Returns: |
| 104 | Zipper: A new zipper with updated left subtree |
| 105 | |
| 106 | # Edge Case: Called on None zipper |
| 107 | """ |
| 108 | if self is None: |
| 109 | return None |
| 110 | new_focus = { |
| 111 | "value": self._focus["value"], |
| 112 | "left": tree, |
| 113 | "right": self._focus["right"] |
| 114 | } |
| 115 | return Zipper(new_focus, self._path) |
| 116 | |
| 19 | 117 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 118 | """ |
| 119 | Move focus to the right child of the current node. |
| 120 | |
| 121 | Returns: |
| 122 | Zipper | None: A new zipper with focus on the right child, |
| 123 | or None if there is no right child |
| 124 | |
| 125 | # Edge Case: Called on None zipper |
| 126 | # Edge Case: No right child exists |
| 127 | """ |
| 128 | if self is None: |
| 129 | return None |
| 130 | if self._focus["right"] is None: |
| 131 | return None |
| 132 | new_path = self._path + [(self._focus, "right")] |
| 133 | return Zipper(self._focus["right"], new_path) |
| 134 | |
| 22 | 135 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 136 | """ |
| 137 | Replace the right subtree of the focus node. |
| 138 | |
| 139 | Args: |
| 140 | tree (dict | None): The new right subtree or None |
| 141 | |
| 142 | Returns: |
| 143 | Zipper: A new zipper with updated right subtree |
| 144 | |
| 145 | # Edge Case: Called on None zipper |
| 146 | """ |
| 147 | if self is None: |
| 148 | return None |
| 149 | new_focus = { |
| 150 | "value": self._focus["value"], |
| 151 | "left": self._focus["left"], |
| 152 | "right": tree |
| 153 | } |
| 154 | return Zipper(new_focus, self._path) |
| 155 | |
| 25 | 156 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 157 | """ |
| 158 | Move focus to the parent of the current node. |
| 159 | |
| 160 | Returns: |
| 161 | Zipper | None: A new zipper with focus on the parent node, |
| 162 | or None if already at the root |
| 163 | |
| 164 | # Edge Case: Called on None zipper |
| 165 | # Edge Case: Already at root (no parent) |
| 166 | """ |
| 167 | if self is None: |
| 168 | return None |
| 169 | if not self._path: |
| 170 | return None |
| 171 | |
| 172 | parent, side = self._path[-1] |
| 173 | new_focus = dict(parent) # Create a copy |
| 174 | |
| 175 | # Update the appropriate child reference to match current focus |
| 176 | if side == "left": |
| 177 | new_focus["left"] = self._focus |
| 178 | else: # side == "right" |
| 179 | new_focus["right"] = self._focus |
| 180 | |
| 181 | return Zipper(new_focus, self._path[:-1]) |
| 182 | |
| 28 | 183 | def to_tree(self) -> dict: |
| 29 | | pass |
| 184 | """ |
| 185 | Reconstruct the entire tree from the current zipper state. |
| 186 | |
| 187 | Returns: |
| 188 | dict: The complete tree with all modifications applied |
| 189 | |
| 190 | # Edge Case: Called on None zipper |
| 191 | """ |
| 192 | if self is None: |
| 193 | return None |
| 194 | |
| 195 | # Start with current focus |
| 196 | tree = dict(self._focus) |
| 197 | |
| 198 | # Traverse up the path, rebuilding the tree |
| 199 | for parent, side in reversed(self._path): |
| 200 | new_parent = dict(parent) |
| 201 | if side == "left": |
| 202 | new_parent["left"] = tree |
| 203 | new_parent["right"] = parent["right"] |
| 204 | else: # side == "right" |
| 205 | new_parent["left"] = parent["left"] |
| 206 | new_parent["right"] = tree |
| 207 | tree = new_parent |
| 208 | |
| 209 | return tree |
| 210 | |
| 211 | # Handled Edge Cases: Empty tree in from_tree, None zipper in all methods, |
| 212 | # No left/right child in left/right methods, |
| 213 | # Already at root in up method |