| 1 | 1 | class Zipper: |
| 2 | | # Tree is a dict with keys "value" (int), "left" (dict or None), "right" (dict or None) |
| 2 | """ |
| 3 | A zipper for navigating and manipulating a binary tree. |
| 4 | |
| 5 | A zipper contains a data structure and a pointer (focus) into that structure. |
| 6 | It supports functional navigation and modification operations. |
| 7 | |
| 8 | Attributes: |
| 9 | _focus (dict): The current node in focus with keys 'value', 'left', 'right' |
| 10 | _path (list): A list of tuples representing the path from root to focus |
| 11 | Each tuple contains (parent_node, direction) where direction is 'left' or 'right' |
| 12 | """ |
| 13 | |
| 14 | def __init__(self, focus: dict, path: list): |
| 15 | """ |
| 16 | Initialize a Zipper. |
| 17 | |
| 18 | Args: |
| 19 | focus (dict): The node currently in focus |
| 20 | path (list): The path from root to focus |
| 21 | """ |
| 22 | self._focus = focus |
| 23 | self._path = path |
| 24 | |
| 3 | 25 | @staticmethod |
| 4 | 26 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 27 | """ |
| 28 | Create a zipper from a binary tree. |
| 29 | |
| 30 | Args: |
| 31 | tree (dict): A binary tree represented as a dict with keys 'value', 'left', 'right' |
| 32 | |
| 33 | Returns: |
| 34 | Zipper: A new zipper with focus on the root node |
| 35 | |
| 36 | # Handled Edge Cases: Empty tree (None) |
| 37 | """ |
| 38 | # Edge Case: Empty tree |
| 39 | if tree is None: |
| 40 | return None |
| 41 | return Zipper(tree, []) |
| 42 | |
| 7 | 43 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 44 | """ |
| 45 | Get the value of the focus node. |
| 46 | |
| 47 | Returns: |
| 48 | int: The value of the focus node |
| 49 | |
| 50 | # Handled Edge Cases: None zipper |
| 51 | """ |
| 52 | # Edge Case: None zipper |
| 53 | if self is None: |
| 54 | return None |
| 55 | return self._focus["value"] |
| 56 | |
| 10 | 57 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 58 | """ |
| 59 | Set the value of the focus node. |
| 60 | |
| 61 | Args: |
| 62 | value (int): The new value for the focus node |
| 63 | |
| 64 | Returns: |
| 65 | Zipper: A new zipper with updated focus node value |
| 66 | |
| 67 | # Handled Edge Cases: None zipper |
| 68 | """ |
| 69 | # Edge Case: None zipper |
| 70 | if self is None: |
| 71 | return None |
| 72 | new_focus = { |
| 73 | "value": value, |
| 74 | "left": self._focus["left"], |
| 75 | "right": self._focus["right"] |
| 76 | } |
| 77 | return Zipper(new_focus, self._path) |
| 78 | |
| 13 | 79 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 80 | """ |
| 81 | Move the focus to the left child of the current node. |
| 82 | |
| 83 | Returns: |
| 84 | Zipper | None: A new zipper with focus on the left child, or None if no left child |
| 85 | |
| 86 | # Handled Edge Cases: None zipper, no left child |
| 87 | """ |
| 88 | # Edge Case: None zipper |
| 89 | if self is None: |
| 90 | return None |
| 91 | # Edge Case: No left child |
| 92 | if self._focus["left"] is None: |
| 93 | return None |
| 94 | new_path = self._path + [(self._focus, "left")] |
| 95 | return Zipper(self._focus["left"], new_path) |
| 96 | |
| 16 | 97 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 98 | """ |
| 99 | Set the left child of the focus node. |
| 100 | |
| 101 | Args: |
| 102 | tree (dict | None): The new left subtree or None |
| 103 | |
| 104 | Returns: |
| 105 | Zipper: A new zipper with updated left child |
| 106 | |
| 107 | # Handled Edge Cases: None zipper |
| 108 | """ |
| 109 | # Edge Case: None zipper |
| 110 | if self is None: |
| 111 | return None |
| 112 | new_focus = { |
| 113 | "value": self._focus["value"], |
| 114 | "left": tree, |
| 115 | "right": self._focus["right"] |
| 116 | } |
| 117 | return Zipper(new_focus, self._path) |
| 118 | |
| 19 | 119 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 120 | """ |
| 121 | Move the focus to the right child of the current node. |
| 122 | |
| 123 | Returns: |
| 124 | Zipper | None: A new zipper with focus on the right child, or None if no right child |
| 125 | |
| 126 | # Handled Edge Cases: None zipper, no right child |
| 127 | """ |
| 128 | # Edge Case: None zipper |
| 129 | if self is None: |
| 130 | return None |
| 131 | # Edge Case: No right child |
| 132 | if self._focus["right"] is None: |
| 133 | return None |
| 134 | new_path = self._path + [(self._focus, "right")] |
| 135 | return Zipper(self._focus["right"], new_path) |
| 136 | |
| 22 | 137 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 138 | """ |
| 139 | Set the right child of the focus node. |
| 140 | |
| 141 | Args: |
| 142 | tree (dict | None): The new right subtree or None |
| 143 | |
| 144 | Returns: |
| 145 | Zipper: A new zipper with updated right child |
| 146 | |
| 147 | # Handled Edge Cases: None zipper |
| 148 | """ |
| 149 | # Edge Case: None zipper |
| 150 | if self is None: |
| 151 | return None |
| 152 | new_focus = { |
| 153 | "value": self._focus["value"], |
| 154 | "left": self._focus["left"], |
| 155 | "right": tree |
| 156 | } |
| 157 | return Zipper(new_focus, self._path) |
| 158 | |
| 25 | 159 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 160 | """ |
| 161 | Move the focus to the parent node. |
| 162 | |
| 163 | Returns: |
| 164 | Zipper | None: A new zipper with focus on the parent, or None if at root |
| 165 | |
| 166 | # Handled Edge Cases: None zipper, already at root |
| 167 | """ |
| 168 | # Edge Case: None zipper |
| 169 | if self is None: |
| 170 | return None |
| 171 | # Edge Case: Already at root (no path) |
| 172 | if not self._path: |
| 173 | return None |
| 174 | parent, direction = self._path[-1] |
| 175 | return Zipper(parent, self._path[:-1]) |
| 176 | |
| 28 | 177 | def to_tree(self) -> dict: |
| 29 | | pass |
| 178 | """ |
| 179 | Reconstruct the entire tree from the zipper. |
| 180 | |
| 181 | Returns: |
| 182 | dict: The complete tree with the current modifications |
| 183 | |
| 184 | # Handled Edge Cases: None zipper |
| 185 | """ |
| 186 | # Edge Case: None zipper |
| 187 | if self is None: |
| 188 | return None |
| 189 | # Reconstruct the tree by moving up to the root |
| 190 | current = self |
| 191 | while current._path: |
| 192 | current = current.up() |
| 193 | return current._focus |