| 1 | 1 | class Zipper: |
| 2 | def __init__(self, tree, path): |
| 3 | self.tree = tree |
| 4 | self.path = path |
| 5 | |
| 2 | 6 | # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None) |
| 3 | 7 | @staticmethod |
| 4 | 8 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 9 | # Edge Case: Empty tree |
| 10 | if tree is None: |
| 11 | return None |
| 12 | return Zipper(tree, []) |
| 6 | 13 | |
| 7 | 14 | def value(self) -> int: |
| 8 | | pass |
| 15 | # Edge Case: Called on None zipper |
| 16 | if self.tree is None: |
| 17 | return None |
| 18 | return self.tree["value"] |
| 9 | 19 | |
| 10 | 20 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 21 | # Edge Case: Called on None zipper |
| 22 | if self.tree is None: |
| 23 | return None |
| 24 | # Create a new tree with updated value |
| 25 | new_tree = {"value": value, "left": self.tree["left"], "right": self.tree["right"]} |
| 26 | # Reconstruct the tree from the path |
| 27 | return Zipper._reconstruct_from_path(new_tree, self.path) |
| 12 | 28 | |
| 13 | 29 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 30 | # Edge Case: Called on None zipper |
| 31 | if self.tree is None: |
| 32 | return None |
| 33 | # Edge Case: No left child |
| 34 | if self.tree["left"] is None: |
| 35 | return None |
| 36 | # Create a new path breadcrumb |
| 37 | new_path = self.path + [{ |
| 38 | "parent": self.tree, |
| 39 | "side": "left", |
| 40 | "sibling": self.tree["right"] |
| 41 | }] |
| 42 | return Zipper(self.tree["left"], new_path) |
| 15 | 43 | |
| 16 | 44 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 45 | # Edge Case: Called on None zipper |
| 46 | if self.tree is None: |
| 47 | return None |
| 48 | # Create a new tree with updated left child |
| 49 | new_tree = {"value": self.tree["value"], "left": tree, "right": self.tree["right"]} |
| 50 | # Reconstruct the tree from the path |
| 51 | return Zipper._reconstruct_from_path(new_tree, self.path) |
| 18 | 52 | |
| 19 | 53 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 54 | # Edge Case: Called on None zipper |
| 55 | if self.tree is None: |
| 56 | return None |
| 57 | # Edge Case: No right child |
| 58 | if self.tree["right"] is None: |
| 59 | return None |
| 60 | # Create a new path breadcrumb |
| 61 | new_path = self.path + [{ |
| 62 | "parent": self.tree, |
| 63 | "side": "right", |
| 64 | "sibling": self.tree["left"] |
| 65 | }] |
| 66 | return Zipper(self.tree["right"], new_path) |
| 21 | 67 | |
| 22 | 68 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 69 | # Edge Case: Called on None zipper |
| 70 | if self.tree is None: |
| 71 | return None |
| 72 | # Create a new tree with updated right child |
| 73 | new_tree = {"value": self.tree["value"], "left": self.tree["left"], "right": tree} |
| 74 | # Reconstruct the tree from the path |
| 75 | return Zipper._reconstruct_from_path(new_tree, self.path) |
| 24 | 76 | |
| 25 | 77 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 78 | # Edge Case: Called on None zipper |
| 79 | if self.tree is None: |
| 80 | return None |
| 81 | # Edge Case: Already at root (no path) |
| 82 | if not self.path: |
| 83 | return None |
| 84 | # Get the last breadcrumb |
| 85 | parent_info = self.path[-1] |
| 86 | parent = parent_info["parent"] |
| 87 | side = parent_info["side"] |
| 88 | sibling = parent_info["sibling"] |
| 89 | |
| 90 | # Create the parent tree with current tree as the appropriate child |
| 91 | if side == "left": |
| 92 | new_parent = {"value": parent["value"], "left": self.tree, "right": sibling} |
| 93 | else: # side == "right" |
| 94 | new_parent = {"value": parent["value"], "left": sibling, "right": self.tree} |
| 95 | |
| 96 | # Return a new zipper with the parent as the focus and truncated path |
| 97 | return Zipper(new_parent, self.path[:-1]) |
| 27 | 98 | |
| 28 | 99 | def to_tree(self) -> dict: |
| 29 | | pass |
| 100 | # Edge Case: Called on None zipper |
| 101 | if self.tree is None: |
| 102 | return None |
| 103 | # Navigate up to the root to get the complete tree |
| 104 | current = self |
| 105 | while current.path: |
| 106 | current = current.up() |
| 107 | return current.tree |
| 108 | |
| 109 | @staticmethod |
| 110 | def _reconstruct_from_path(tree, path): |
| 111 | """Helper method to reconstruct a tree from the path.""" |
| 112 | # Edge Case: Empty path (at root) |
| 113 | if not path: |
| 114 | return Zipper(tree, []) |
| 115 | |
| 116 | # Start from the current tree and move up, updating parents |
| 117 | current_tree = tree |
| 118 | # Work backwards through the path |
| 119 | for breadcrumb in reversed(path): |
| 120 | parent = breadcrumb["parent"] |
| 121 | side = breadcrumb["side"] |
| 122 | sibling = breadcrumb["sibling"] |
| 123 | |
| 124 | # Create updated parent with current_tree in the correct position |
| 125 | if side == "left": |
| 126 | updated_parent = {"value": parent["value"], "left": current_tree, "right": sibling} |
| 127 | else: # side == "right" |
| 128 | updated_parent = {"value": parent["value"], "left": sibling, "right": current_tree} |
| 129 | |
| 130 | current_tree = updated_parent |
| 131 | |
| 132 | # Return a new zipper at the root |
| 133 | return Zipper(current_tree, []) |
| 134 | # Handled Edge Cases: Empty tree in from_tree; Called on None zipper in value, set_value, left, set_left, right, set_right, up, to_tree; No left child in left; No right child in right; Already at root in up; Empty path in _reconstruct_from_path |