| 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, focus, path): |
| 3 | self.focus = focus |
| 4 | self.path = path |
| 5 | |
| 6 | # Edge Case: Handle empty tree input by checking if tree is None |
| 3 | 7 | @staticmethod |
| 4 | 8 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 9 | if tree is None: |
| 10 | return None |
| 11 | return Zipper(tree, []) |
| 12 | |
| 13 | # Edge Case: Return value of focus node when focus exists |
| 7 | 14 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 15 | if self.focus is None: |
| 16 | return None |
| 17 | return self.focus["value"] |
| 18 | |
| 19 | # Edge Case: Create new zipper with updated focus value while preserving path |
| 10 | 20 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 21 | if self.focus is None: |
| 22 | return self |
| 23 | new_focus = { |
| 24 | "value": value, |
| 25 | "left": self.focus["left"], |
| 26 | "right": self.focus["right"] |
| 27 | } |
| 28 | return Zipper(new_focus, self.path) |
| 29 | |
| 30 | # Edge Case: Handle navigation to left child when it exists, otherwise return None |
| 13 | 31 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 32 | if self.focus is None or self.focus["left"] is None: |
| 33 | return None |
| 34 | new_path = self.path + [{ |
| 35 | "parent": self.focus, |
| 36 | "side": "left" |
| 37 | }] |
| 38 | return Zipper(self.focus["left"], new_path) |
| 39 | |
| 40 | # Edge Case: Create new zipper with updated left subtree while preserving current focus and path |
| 16 | 41 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 42 | new_focus = { |
| 43 | "value": self.focus["value"], |
| 44 | "left": tree, |
| 45 | "right": self.focus["right"] |
| 46 | } |
| 47 | return Zipper(new_focus, self.path) |
| 48 | |
| 49 | # Edge Case: Handle navigation to right child when it exists, otherwise return None |
| 19 | 50 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 51 | if self.focus is None or self.focus["right"] is None: |
| 52 | return None |
| 53 | new_path = self.path + [{ |
| 54 | "parent": self.focus, |
| 55 | "side": "right" |
| 56 | }] |
| 57 | return Zipper(self.focus["right"], new_path) |
| 58 | |
| 59 | # Edge Case: Create new zipper with updated right subtree while preserving current focus and path |
| 22 | 60 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 61 | new_focus = { |
| 62 | "value": self.focus["value"], |
| 63 | "left": self.focus["left"], |
| 64 | "right": tree |
| 65 | } |
| 66 | return Zipper(new_focus, self.path) |
| 67 | |
| 68 | # Edge Case: Handle navigation to parent when path exists, otherwise return None |
| 25 | 69 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 70 | if not self.path: |
| 71 | return None |
| 72 | parent_info = self.path[-1] |
| 73 | parent = parent_info["parent"].copy() |
| 74 | side = parent_info["side"] |
| 75 | |
| 76 | if side == "left": |
| 77 | parent["left"] = self.focus |
| 78 | else: |
| 79 | parent["right"] = self.focus |
| 80 | |
| 81 | return Zipper(parent, self.path[:-1]) |
| 82 | |
| 83 | # Edge Case: Reconstruct complete tree from focus and path, handling root case when path is empty |
| 28 | 84 | def to_tree(self) -> dict: |
| 29 | | pass |
| 85 | current = self.focus |
| 86 | |
| 87 | # Rebuild the tree from the path upward |
| 88 | for step in reversed(self.path): |
| 89 | parent = step["parent"].copy() |
| 90 | side = step["side"] |
| 91 | |
| 92 | if side == "left": |
| 93 | parent["left"] = current |
| 94 | else: |
| 95 | parent["right"] = current |
| 96 | |
| 97 | current = parent |
| 98 | |
| 99 | return current |
| 100 | |
| 101 | # Handled Edge Cases: Empty tree input, None focus handling, Navigation to non-existent children, Path reconstruction, Root node handling |