| 1 | 1 | class Zipper: |
| 2 | 2 | # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None) |
| 3 | def __init__(self, tree, path=None): |
| 4 | # Edge Case: Initialize with None tree |
| 5 | if tree is None: |
| 6 | raise ValueError("Tree cannot be None") |
| 7 | self.tree = tree |
| 8 | # Path is a list of tuples (parent_node, is_left_child) |
| 9 | self.path = path or [] |
| 10 | # Focus is the current node we're pointing to |
| 11 | self.focus = tree |
| 12 | # If we have a path, we need to navigate to the correct focus |
| 13 | if self.path: |
| 14 | for parent, is_left in self.path: |
| 15 | if is_left: |
| 16 | self.focus = parent["left"] |
| 17 | else: |
| 18 | self.focus = parent["right"] |
| 19 | |
| 3 | 20 | @staticmethod |
| 4 | 21 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 22 | # Edge Case: Handle None tree input |
| 23 | if tree is None: |
| 24 | raise ValueError("Tree cannot be None") |
| 25 | return Zipper(tree) |
| 6 | 26 | |
| 7 | 27 | def value(self) -> int: |
| 8 | | pass |
| 28 | # Edge Case: Handle None focus |
| 29 | if self.focus is None: |
| 30 | return None |
| 31 | return self.focus["value"] |
| 9 | 32 | |
| 10 | 33 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 34 | # Edge Case: Handle None focus |
| 35 | if self.focus is None: |
| 36 | return self |
| 37 | # Create a new tree with the updated value |
| 38 | new_tree = self._copy_tree(self.tree) |
| 39 | # Navigate to the focus node in the new tree |
| 40 | focus = new_tree |
| 41 | for parent, is_left in self.path: |
| 42 | if is_left: |
| 43 | focus = focus["left"] |
| 44 | else: |
| 45 | focus = focus["right"] |
| 46 | # Update the value |
| 47 | focus["value"] = value |
| 48 | return Zipper(new_tree, self.path) |
| 12 | 49 | |
| 13 | 50 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 51 | # Edge Case: Handle None focus |
| 52 | if self.focus is None or self.focus["left"] is None: |
| 53 | return None |
| 54 | new_path = self.path + [(self.focus, True)] |
| 55 | return Zipper(self.tree, new_path) |
| 15 | 56 | |
| 16 | 57 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 58 | # Edge Case: Handle None focus |
| 59 | if self.focus is None: |
| 60 | return self |
| 61 | # Create a new tree with the updated left child |
| 62 | new_tree = self._copy_tree(self.tree) |
| 63 | # Navigate to the focus node in the new tree |
| 64 | focus = new_tree |
| 65 | for parent, is_left in self.path: |
| 66 | if is_left: |
| 67 | focus = focus["left"] |
| 68 | else: |
| 69 | focus = focus["right"] |
| 70 | # Update the left child |
| 71 | focus["left"] = tree |
| 72 | return Zipper(new_tree, self.path) |
| 18 | 73 | |
| 19 | 74 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 75 | # Edge Case: Handle None focus |
| 76 | if self.focus is None or self.focus["right"] is None: |
| 77 | return None |
| 78 | new_path = self.path + [(self.focus, False)] |
| 79 | return Zipper(self.tree, new_path) |
| 21 | 80 | |
| 22 | 81 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 82 | # Edge Case: Handle None focus |
| 83 | if self.focus is None: |
| 84 | return self |
| 85 | # Create a new tree with the updated right child |
| 86 | new_tree = self._copy_tree(self.tree) |
| 87 | # Navigate to the focus node in the new tree |
| 88 | focus = new_tree |
| 89 | for parent, is_left in self.path: |
| 90 | if is_left: |
| 91 | focus = focus["left"] |
| 92 | else: |
| 93 | focus = focus["right"] |
| 94 | # Update the right child |
| 95 | focus["right"] = tree |
| 96 | return Zipper(new_tree, self.path) |
| 24 | 97 | |
| 25 | 98 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 99 | # Edge Case: Handle when already at root (no path) |
| 100 | if not self.path: |
| 101 | return None |
| 102 | new_path = self.path[:-1] |
| 103 | return Zipper(self.tree, new_path) |
| 27 | 104 | |
| 28 | 105 | def to_tree(self) -> dict: |
| 29 | | pass |
| 106 | return self.tree |
| 107 | |
| 108 | def _copy_tree(self, tree): |
| 109 | """Helper method to create a deep copy of a tree""" |
| 110 | if tree is None: |
| 111 | return None |
| 112 | return { |
| 113 | "value": tree["value"], |
| 114 | "left": self._copy_tree(tree["left"]), |
| 115 | "right": self._copy_tree(tree["right"]) |
| 116 | } |
| 117 | # Handled Edge Cases: Initialize with None tree, Handle None tree input, Handle None focus, Handle when already at root (no path) |