| 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 | The zipper maintains a focus on a specific node in the tree, allowing |
| 6 | navigation and modifications while preserving the tree structure. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, breadcrumbs=None): |
| 10 | """ |
| 11 | Initialize a Zipper. |
| 12 | |
| 13 | Args: |
| 14 | tree: The tree node at the current focus (dict with 'value', 'left', 'right') |
| 15 | breadcrumbs: List of breadcrumb dictionaries tracking the path to the focus |
| 16 | """ |
| 17 | self.tree = tree |
| 18 | self.breadcrumbs = breadcrumbs or [] |
| 19 | |
| 3 | 20 | @staticmethod |
| 4 | 21 | def from_tree(tree: dict) -> "Zipper": |
| 5 | | pass |
| 6 | | |
| 22 | """ |
| 23 | Create a zipper from a binary tree. |
| 24 | |
| 25 | Args: |
| 26 | tree: A binary tree represented as a dict with keys 'value', 'left', 'right' |
| 27 | |
| 28 | Returns: |
| 29 | A new Zipper instance focused on the root of the tree |
| 30 | """ |
| 31 | # Edge Case: Handle None tree |
| 32 | if tree is None: |
| 33 | return None |
| 34 | return Zipper(tree) |
| 35 | |
| 7 | 36 | def value(self) -> int: |
| 8 | | pass |
| 9 | | |
| 37 | """ |
| 38 | Get the value of the focus node. |
| 39 | |
| 40 | Returns: |
| 41 | The value of the current node |
| 42 | """ |
| 43 | return self.tree["value"] |
| 44 | |
| 10 | 45 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 46 | """ |
| 47 | Set the value of the focus node. |
| 48 | |
| 49 | Args: |
| 50 | value: The new value for the focus node |
| 51 | |
| 52 | Returns: |
| 53 | A new Zipper with the updated value |
| 54 | """ |
| 55 | # Edge Case: Handle None tree |
| 56 | if self.tree is None: |
| 57 | return self |
| 58 | new_tree = { |
| 59 | "value": value, |
| 60 | "left": self.tree["left"], |
| 61 | "right": self.tree["right"] |
| 62 | } |
| 63 | return Zipper(new_tree, self.breadcrumbs) |
| 64 | |
| 13 | 65 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 66 | """ |
| 67 | Move the focus to the left child of the current node. |
| 68 | |
| 69 | Returns: |
| 70 | A new Zipper focused on the left child, or None if no left child exists |
| 71 | """ |
| 72 | # Edge Case: Handle None tree or missing left child |
| 73 | if self.tree is None or self.tree["left"] is None: |
| 74 | return None |
| 75 | |
| 76 | new_breadcrumb = { |
| 77 | "parent": self.tree, |
| 78 | "side": "left", |
| 79 | "right_sibling": self.tree["right"] |
| 80 | } |
| 81 | |
| 82 | return Zipper(self.tree["left"], self.breadcrumbs + [new_breadcrumb]) |
| 83 | |
| 16 | 84 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 85 | """ |
| 86 | Set the left child of the focus node. |
| 87 | |
| 88 | Args: |
| 89 | tree: The new left subtree (or None to remove the left child) |
| 90 | |
| 91 | Returns: |
| 92 | A new Zipper with the updated left child |
| 93 | """ |
| 94 | # Edge Case: Handle None tree |
| 95 | if self.tree is None: |
| 96 | return self |
| 97 | new_tree = { |
| 98 | "value": self.tree["value"], |
| 99 | "left": tree, |
| 100 | "right": self.tree["right"] |
| 101 | } |
| 102 | return Zipper(new_tree, self.breadcrumbs) |
| 103 | |
| 19 | 104 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 105 | """ |
| 106 | Move the focus to the right child of the current node. |
| 107 | |
| 108 | Returns: |
| 109 | A new Zipper focused on the right child, or None if no right child exists |
| 110 | """ |
| 111 | # Edge Case: Handle None tree or missing right child |
| 112 | if self.tree is None or self.tree["right"] is None: |
| 113 | return None |
| 114 | |
| 115 | new_breadcrumb = { |
| 116 | "parent": self.tree, |
| 117 | "side": "right", |
| 118 | "left_sibling": self.tree["left"] |
| 119 | } |
| 120 | |
| 121 | return Zipper(self.tree["right"], self.breadcrumbs + [new_breadcrumb]) |
| 122 | |
| 22 | 123 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 124 | """ |
| 125 | Set the right child of the focus node. |
| 126 | |
| 127 | Args: |
| 128 | tree: The new right subtree (or None to remove the right child) |
| 129 | |
| 130 | Returns: |
| 131 | A new Zipper with the updated right child |
| 132 | """ |
| 133 | # Edge Case: Handle None tree |
| 134 | if self.tree is None: |
| 135 | return self |
| 136 | new_tree = { |
| 137 | "value": self.tree["value"], |
| 138 | "left": self.tree["left"], |
| 139 | "right": tree |
| 140 | } |
| 141 | return Zipper(new_tree, self.breadcrumbs) |
| 142 | |
| 25 | 143 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 144 | """ |
| 145 | Move the focus to the parent of the current node. |
| 146 | |
| 147 | Returns: |
| 148 | A new Zipper focused on the parent, or None if at the root |
| 149 | """ |
| 150 | # Edge Case: Handle None tree or no breadcrumbs (at root) |
| 151 | if self.tree is None or not self.breadcrumbs: |
| 152 | return None |
| 153 | |
| 154 | parent_breadcrumb = self.breadcrumbs[-1] |
| 155 | parent = parent_breadcrumb["parent"].copy() |
| 156 | |
| 157 | if parent_breadcrumb["side"] == "left": |
| 158 | parent["left"] = self.tree |
| 159 | parent["right"] = parent_breadcrumb["right_sibling"] |
| 160 | else: # side == "right" |
| 161 | parent["right"] = self.tree |
| 162 | parent["left"] = parent_breadcrumb["left_sibling"] |
| 163 | |
| 164 | return Zipper(parent, self.breadcrumbs[:-1]) |
| 165 | |
| 28 | 166 | def to_tree(self) -> dict: |
| 29 | | pass |
| 167 | """ |
| 168 | Get the complete tree from the zipper. |
| 169 | |
| 170 | Returns: |
| 171 | The full binary tree as a dict |
| 172 | """ |
| 173 | # Edge Case: Handle None tree |
| 174 | if self.tree is None: |
| 175 | return None |
| 176 | |
| 177 | # Navigate to the root to get the complete tree |
| 178 | current = self |
| 179 | while current.breadcrumbs: |
| 180 | current = current.up() |
| 181 | return current.tree |
| 182 | |
| 183 | # Handled Edge Cases: None tree, missing children, root node navigation, empty breadcrumbs |