| 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 by returning a zipper with focus None |
| 3 | 7 | @staticmethod |
| 4 | 8 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 9 | return Zipper(tree, []) |
| 10 | |
| 11 | # Edge Case: Handle when focus is None (e.g., deleted root) |
| 7 | 12 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 13 | if self.focus is None: |
| 14 | raise ValueError("Cannot get value of None focus") |
| 15 | return self.focus["value"] |
| 16 | |
| 17 | # Edge Case: Handle when focus is None (e.g., deleted root) |
| 10 | 18 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 19 | if self.focus is None: |
| 20 | raise ValueError("Cannot set value of None focus") |
| 21 | new_focus = { |
| 22 | "value": value, |
| 23 | "left": self.focus["left"], |
| 24 | "right": self.focus["right"] |
| 25 | } |
| 26 | return Zipper(new_focus, self.path) |
| 27 | |
| 28 | # Edge Case: Handle when focus is None (e.g., deleted root) |
| 29 | # Edge Case: Handle when there's no left child |
| 13 | 30 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 31 | if self.focus is None or self.focus["left"] is None: |
| 32 | return None |
| 33 | new_path = self.path + [{ |
| 34 | "parent": self.focus, |
| 35 | "side": "left", |
| 36 | "sibling": self.focus["right"] |
| 37 | }] |
| 38 | return Zipper(self.focus["left"], new_path) |
| 39 | |
| 40 | # Edge Case: Handle when focus is None (e.g., deleted root) |
| 16 | 41 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 42 | if self.focus is None: |
| 43 | raise ValueError("Cannot set left of None focus") |
| 44 | new_focus = { |
| 45 | "value": self.focus["value"], |
| 46 | "left": tree, |
| 47 | "right": self.focus["right"] |
| 48 | } |
| 49 | return Zipper(new_focus, self.path) |
| 50 | |
| 51 | # Edge Case: Handle when focus is None (e.g., deleted root) |
| 52 | # Edge Case: Handle when there's no right child |
| 19 | 53 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 54 | if self.focus is None or self.focus["right"] is None: |
| 55 | return None |
| 56 | new_path = self.path + [{ |
| 57 | "parent": self.focus, |
| 58 | "side": "right", |
| 59 | "sibling": self.focus["left"] |
| 60 | }] |
| 61 | return Zipper(self.focus["right"], new_path) |
| 62 | |
| 63 | # Edge Case: Handle when focus is None (e.g., deleted root) |
| 22 | 64 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 65 | if self.focus is None: |
| 66 | raise ValueError("Cannot set right of None focus") |
| 67 | new_focus = { |
| 68 | "value": self.focus["value"], |
| 69 | "left": self.focus["left"], |
| 70 | "right": tree |
| 71 | } |
| 72 | return Zipper(new_focus, self.path) |
| 73 | |
| 74 | # Edge Case: Handle when already at root (path is empty) |
| 75 | # Edge Case: Handle when focus is None (e.g., deleted root) |
| 25 | 76 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 77 | if self.focus is None or len(self.path) == 0: |
| 78 | return None |
| 79 | |
| 80 | parent_info = self.path[-1] |
| 81 | parent = parent_info["parent"].copy() |
| 82 | |
| 83 | if parent_info["side"] == "left": |
| 84 | parent["left"] = self.focus |
| 85 | parent["right"] = parent_info["sibling"] |
| 86 | else: # right |
| 87 | parent["right"] = self.focus |
| 88 | parent["left"] = parent_info["sibling"] |
| 89 | |
| 90 | return Zipper(parent, self.path[:-1]) |
| 91 | |
| 92 | # Edge Case: Handle when focus is None (e.g., deleted root) |
| 28 | 93 | def to_tree(self) -> dict: |
| 29 | | pass |
| 94 | if self.focus is None: |
| 95 | return None |
| 96 | |
| 97 | current = self.focus.copy() |
| 98 | for info in reversed(self.path): |
| 99 | parent = info["parent"].copy() |
| 100 | if info["side"] == "left": |
| 101 | parent["left"] = current |
| 102 | parent["right"] = info["sibling"] |
| 103 | else: # right |
| 104 | parent["right"] = current |
| 105 | parent["left"] = info["sibling"] |
| 106 | current = parent |
| 107 | return current |
| 108 | |
| 109 | # Handled Edge Cases: Empty tree, None focus, no left/right child, already at root |