| 1 | 1 | class Zipper: |
| 2 | | # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None) |
| 2 | def __init__(self, tree, path): |
| 3 | # Initialize a Zipper with a tree and a path |
| 4 | self.tree = tree |
| 5 | self.path = path |
| 6 | |
| 7 | # Edge Case: Handle empty tree input |
| 3 | 8 | @staticmethod |
| 4 | 9 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 10 | # Create a zipper from a tree, starting at the root |
| 11 | return Zipper(tree, []) |
| 12 | |
| 13 | # Edge Case: Handle when focus is None (should not happen with valid zippers) |
| 7 | 14 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 15 | # Get the value of the focus node |
| 16 | focus = self._get_focus_node() |
| 17 | if focus is None: |
| 18 | raise ValueError("Focus node is None") |
| 19 | return focus["value"] |
| 20 | |
| 21 | # Edge Case: Handle when focus is None (should not happen with valid zippers) |
| 10 | 22 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 23 | # Set the value of the focus node and return a new zipper |
| 24 | new_tree = self._copy_tree() |
| 25 | focus_path = self._get_focus_path(new_tree) |
| 26 | if focus_path is None: |
| 27 | raise ValueError("Focus node is None") |
| 28 | focus_path["value"] = value |
| 29 | return Zipper(new_tree, self.path) |
| 30 | |
| 31 | # Edge Case: Handle when there's no left child |
| 13 | 32 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 33 | # Move focus to the left child |
| 34 | focus = self._get_focus_node() |
| 35 | if focus is None or focus["left"] is None: |
| 36 | return None |
| 37 | new_path = self.path + [(focus, "left", focus["right"])] |
| 38 | return Zipper(self.tree, new_path) |
| 39 | |
| 40 | # Edge Case: Handle when focus is None (should not happen with valid zippers) |
| 16 | 41 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 42 | # Set the left child of the focus node and return a new zipper |
| 43 | new_tree = self._copy_tree() |
| 44 | focus_path = self._get_focus_path(new_tree) |
| 45 | if focus_path is None: |
| 46 | raise ValueError("Focus node is None") |
| 47 | focus_path["left"] = tree |
| 48 | return Zipper(new_tree, self.path) |
| 49 | |
| 50 | # Edge Case: Handle when there's no right child |
| 19 | 51 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 52 | # Move focus to the right child |
| 53 | focus = self._get_focus_node() |
| 54 | if focus is None or focus["right"] is None: |
| 55 | return None |
| 56 | new_path = self.path + [(focus, "right", focus["left"])] |
| 57 | return Zipper(self.tree, new_path) |
| 58 | |
| 59 | # Edge Case: Handle when focus is None (should not happen with valid zippers) |
| 22 | 60 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 61 | # Set the right child of the focus node and return a new zipper |
| 62 | new_tree = self._copy_tree() |
| 63 | focus_path = self._get_focus_path(new_tree) |
| 64 | if focus_path is None: |
| 65 | raise ValueError("Focus node is None") |
| 66 | focus_path["right"] = tree |
| 67 | return Zipper(new_tree, self.path) |
| 68 | |
| 69 | # Edge Case: Handle when already at the root (no parent) |
| 25 | 70 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 71 | # Move focus to the parent node |
| 72 | if not self.path: |
| 73 | return None |
| 74 | return Zipper(self.tree, self.path[:-1]) |
| 75 | |
| 76 | # Edge Case: Handle when tree is None |
| 28 | 77 | def to_tree(self) -> dict: |
| 29 | | pass |
| 78 | # Return the full tree from the zipper |
| 79 | return self.tree |
| 80 | |
| 81 | # Helper method to get the focus node based on the path |
| 82 | def _get_focus_node(self): |
| 83 | # Navigate to the focus node based on the path |
| 84 | node = self.tree |
| 85 | for parent, direction, _ in self.path: |
| 86 | if node is None: |
| 87 | return None |
| 88 | if direction == "left": |
| 89 | node = parent["left"] |
| 90 | else: # direction == "right" |
| 91 | node = parent["right"] |
| 92 | return node |
| 93 | |
| 94 | # Helper method to copy the entire tree |
| 95 | def _copy_tree(self): |
| 96 | # Create a deep copy of the tree |
| 97 | import copy |
| 98 | return copy.deepcopy(self.tree) |
| 99 | |
| 100 | # Helper method to get the path to the focus node in a copied tree |
| 101 | def _get_focus_path(self, tree_copy): |
| 102 | # Navigate to the focus node in the copied tree |
| 103 | node = tree_copy |
| 104 | for parent, direction, _ in self.path: |
| 105 | if direction == "left": |
| 106 | node = node["left"] |
| 107 | else: # direction == "right" |
| 108 | node = node["right"] |
| 109 | return node |
| 110 | |
| 111 | # Handled Edge Cases: Empty tree input, focus is None, no left child, no right child, already at root, tree is None |