| 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 moving through the tree and modifying it in a functional way. |
| 7 | |
| 8 | Attributes: |
| 9 | _focus: The current node that is focused. |
| 10 | _path: 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: The node that is currently focused. |
| 20 | path: The path from root to focus as a list of (parent_node, direction) tuples. |
| 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: A binary tree represented as a dict with keys "value", "left", and "right". |
| 32 | left and right are either dicts or None. |
| 33 | |
| 34 | Returns: |
| 35 | A new Zipper with focus on the root node. |
| 36 | |
| 37 | # Handled Edge Cases: None (tree is assumed to be valid) |
| 38 | """ |
| 39 | # Edge Case: Empty tree (represented as None) |
| 40 | if tree is None: |
| 41 | return None |
| 42 | return Zipper(tree, []) |
| 43 | |
| 7 | 44 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 45 | """ |
| 46 | Get the value of the focus node. |
| 47 | |
| 48 | Returns: |
| 49 | The value of the focus node. |
| 50 | |
| 51 | # Handled Edge Cases: None (assumes valid zipper) |
| 52 | """ |
| 53 | return self._focus["value"] |
| 54 | |
| 10 | 55 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 56 | """ |
| 57 | Set the value of the focus node. |
| 58 | |
| 59 | Args: |
| 60 | value: The new value for the focus node. |
| 61 | |
| 62 | Returns: |
| 63 | A new Zipper with the updated focus node. |
| 64 | |
| 65 | # Handled Edge Cases: None (assumes valid zipper) |
| 66 | """ |
| 67 | new_focus = { |
| 68 | "value": value, |
| 69 | "left": self._focus["left"], |
| 70 | "right": self._focus["right"] |
| 71 | } |
| 72 | return Zipper(new_focus, self._path) |
| 73 | |
| 13 | 74 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 75 | """ |
| 76 | Move the focus to the left child of the current node. |
| 77 | |
| 78 | Returns: |
| 79 | A new Zipper with focus on the left child, or None if no left child exists. |
| 80 | |
| 81 | # Handled Edge Cases: Left child is None |
| 82 | """ |
| 83 | # Edge Case: No left child |
| 84 | if self._focus["left"] is None: |
| 85 | return None |
| 86 | |
| 87 | new_path = self._path + [(self._focus, "left")] |
| 88 | return Zipper(self._focus["left"], new_path) |
| 89 | |
| 16 | 90 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 91 | """ |
| 92 | Set the left child of the focus node. |
| 93 | |
| 94 | Args: |
| 95 | tree: The new left subtree (or None to remove the left child). |
| 96 | |
| 97 | Returns: |
| 98 | A new Zipper with the updated focus node. |
| 99 | |
| 100 | # Handled Edge Cases: None (assumes valid zipper) |
| 101 | """ |
| 102 | new_focus = { |
| 103 | "value": self._focus["value"], |
| 104 | "left": tree, |
| 105 | "right": self._focus["right"] |
| 106 | } |
| 107 | return Zipper(new_focus, self._path) |
| 108 | |
| 19 | 109 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 110 | """ |
| 111 | Move the focus to the right child of the current node. |
| 112 | |
| 113 | Returns: |
| 114 | A new Zipper with focus on the right child, or None if no right child exists. |
| 115 | |
| 116 | # Handled Edge Cases: Right child is None |
| 117 | """ |
| 118 | # Edge Case: No right child |
| 119 | if self._focus["right"] is None: |
| 120 | return None |
| 121 | |
| 122 | new_path = self._path + [(self._focus, "right")] |
| 123 | return Zipper(self._focus["right"], new_path) |
| 124 | |
| 22 | 125 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 126 | """ |
| 127 | Set the right child of the focus node. |
| 128 | |
| 129 | Args: |
| 130 | tree: The new right subtree (or None to remove the right child). |
| 131 | |
| 132 | Returns: |
| 133 | A new Zipper with the updated focus node. |
| 134 | |
| 135 | # Handled Edge Cases: None (assumes valid zipper) |
| 136 | """ |
| 137 | new_focus = { |
| 138 | "value": self._focus["value"], |
| 139 | "left": self._focus["left"], |
| 140 | "right": tree |
| 141 | } |
| 142 | return Zipper(new_focus, self._path) |
| 143 | |
| 25 | 144 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 145 | """ |
| 146 | Move the focus to the parent of the current node. |
| 147 | |
| 148 | Returns: |
| 149 | A new Zipper with focus on the parent node, or None if already at root. |
| 150 | |
| 151 | # Handled Edge Cases: Already at root (no parent) |
| 152 | """ |
| 153 | # Edge Case: Already at root (no parent) |
| 154 | if not self._path: |
| 155 | return None |
| 156 | |
| 157 | parent, direction = self._path[-1] |
| 158 | new_focus = dict(parent) # Create a copy |
| 159 | |
| 160 | # Update the parent's reference to the current node |
| 161 | if direction == "left": |
| 162 | new_focus["left"] = self._focus |
| 163 | else: # direction == "right" |
| 164 | new_focus["right"] = self._focus |
| 165 | |
| 166 | return Zipper(new_focus, self._path[:-1]) |
| 167 | |
| 28 | 168 | def to_tree(self) -> dict: |
| 29 | | pass |
| 169 | """ |
| 170 | Get the complete tree from the zipper. |
| 171 | |
| 172 | Returns: |
| 173 | The complete binary tree as a dict. |
| 174 | |
| 175 | # Handled Edge Cases: None (assumes valid zipper) |
| 176 | """ |
| 177 | # Navigate to the root to get the complete tree |
| 178 | current_zipper = self |
| 179 | while current_zipper._path: |
| 180 | current_zipper = current_zipper.up() |
| 181 | return current_zipper._focus |