| 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 by returning an empty Zipper |
| 3 | 8 | @staticmethod |
| 4 | 9 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 10 | return Zipper(tree, []) |
| 11 | |
| 12 | # Edge Case: Return the value of the current focus node |
| 7 | 13 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 14 | return self.tree["value"] |
| 15 | |
| 16 | # Edge Case: Create a new Zipper with updated value while preserving path |
| 10 | 17 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 18 | new_tree = {"value": value, "left": self.tree["left"], "right": self.tree["right"]} |
| 19 | # Reconstruct the tree with the new value |
| 20 | return Zipper(self._reconstruct_tree(new_tree), self.path) |
| 21 | |
| 22 | # Edge Case: Navigate to left child, return None if no left child exists |
| 13 | 23 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 24 | if self.tree["left"] is None: |
| 25 | return None |
| 26 | # Create a breadcrumb to remember the current node when going left |
| 27 | breadcrumb = { |
| 28 | "parent": self.tree, |
| 29 | "side": "left", |
| 30 | "sibling": self.tree["right"] |
| 31 | } |
| 32 | return Zipper(self.tree["left"], self.path + [breadcrumb]) |
| 33 | |
| 34 | # Edge Case: Set left child and return new Zipper with updated tree |
| 16 | 35 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 36 | new_tree = {"value": self.tree["value"], "left": tree, "right": self.tree["right"]} |
| 37 | return Zipper(self._reconstruct_tree(new_tree), self.path) |
| 38 | |
| 39 | # Edge Case: Navigate to right child, return None if no right child exists |
| 19 | 40 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 41 | if self.tree["right"] is None: |
| 42 | return None |
| 43 | # Create a breadcrumb to remember the current node when going right |
| 44 | breadcrumb = { |
| 45 | "parent": self.tree, |
| 46 | "side": "right", |
| 47 | "sibling": self.tree["left"] |
| 48 | } |
| 49 | return Zipper(self.tree["right"], self.path + [breadcrumb]) |
| 50 | |
| 51 | # Edge Case: Set right child and return new Zipper with updated tree |
| 22 | 52 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 53 | new_tree = {"value": self.tree["value"], "left": self.tree["left"], "right": tree} |
| 54 | return Zipper(self._reconstruct_tree(new_tree), self.path) |
| 55 | |
| 56 | # Edge Case: Navigate up to parent, return None if already at root |
| 25 | 57 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 58 | if not self.path: |
| 59 | return None |
| 60 | # Get the last breadcrumb |
| 61 | breadcrumb = self.path[-1] |
| 62 | # Reconstruct the parent node with the current tree as the appropriate child |
| 63 | parent_tree = breadcrumb["parent"].copy() |
| 64 | if breadcrumb["side"] == "left": |
| 65 | parent_tree["left"] = self.tree |
| 66 | parent_tree["right"] = breadcrumb["sibling"] |
| 67 | else: # breadcrumb["side"] == "right" |
| 68 | parent_tree["right"] = self.tree |
| 69 | parent_tree["left"] = breadcrumb["sibling"] |
| 70 | # Return a new Zipper focused on the parent |
| 71 | return Zipper(parent_tree, self.path[:-1]) |
| 72 | |
| 73 | # Edge Case: Reconstruct the full tree from the current focus and path |
| 28 | 74 | def to_tree(self) -> dict: |
| 29 | | pass |
| 75 | # Start with the current tree |
| 76 | result = {"value": self.tree["value"], "left": self.tree["left"], "right": self.tree["right"]} |
| 77 | # Apply all breadcrumbs in reverse order to rebuild the full tree |
| 78 | for breadcrumb in reversed(self.path): |
| 79 | parent_tree = breadcrumb["parent"].copy() |
| 80 | if breadcrumb["side"] == "left": |
| 81 | parent_tree["left"] = result |
| 82 | parent_tree["right"] = breadcrumb["sibling"] |
| 83 | else: # breadcrumb["side"] == "right" |
| 84 | parent_tree["right"] = result |
| 85 | parent_tree["left"] = breadcrumb["sibling"] |
| 86 | result = parent_tree |
| 87 | return result |
| 88 | |
| 89 | # Helper method to reconstruct tree with changes applied |
| 90 | def _reconstruct_tree(self, new_subtree): |
| 91 | # If at root, return the new subtree |
| 92 | if not self.path: |
| 93 | return new_subtree |
| 94 | |
| 95 | # Start with the new subtree |
| 96 | result = new_subtree |
| 97 | |
| 98 | # Apply all breadcrumbs in reverse order |
| 99 | for breadcrumb in reversed(self.path): |
| 100 | parent_tree = breadcrumb["parent"].copy() |
| 101 | if breadcrumb["side"] == "left": |
| 102 | parent_tree["left"] = result |
| 103 | parent_tree["right"] = breadcrumb["sibling"] |
| 104 | else: # breadcrumb["side"] == "right" |
| 105 | parent_tree["right"] = result |
| 106 | parent_tree["left"] = breadcrumb["sibling"] |
| 107 | result = parent_tree |
| 108 | |
| 109 | return result |
| 110 | |
| 111 | # Handled Edge Cases: Empty tree, navigating to non-existent children, updating values and children, navigating up from root, reconstructing full tree |