| 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 | |
| 9 | def __init__(self, tree, breadcrumbs=None): |
| 10 | """ |
| 11 | Initialize a Zipper. |
| 12 | |
| 13 | Args: |
| 14 | tree: The current subtree (focus) as a dict with keys 'value', 'left', 'right' |
| 15 | breadcrumbs: A list of tuples representing the path back to the root |
| 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 input |
| 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 | # Edge Case: Handle None tree |
| 44 | if self.tree is None: |
| 45 | return None |
| 46 | return self.tree['value'] |
| 47 | |
| 10 | 48 | def set_value(self, value: int) -> "Zipper": |
| 11 | | pass |
| 12 | | |
| 49 | """ |
| 50 | Set the value of the focus node. |
| 51 | |
| 52 | Args: |
| 53 | value: The new value for the focus node |
| 54 | |
| 55 | Returns: |
| 56 | A new Zipper with the updated value |
| 57 | """ |
| 58 | # Edge Case: Handle None tree |
| 59 | if self.tree is None: |
| 60 | return self |
| 61 | new_tree = { |
| 62 | 'value': value, |
| 63 | 'left': self.tree['left'], |
| 64 | 'right': self.tree['right'] |
| 65 | } |
| 66 | return Zipper(new_tree, self.breadcrumbs) |
| 67 | |
| 13 | 68 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 69 | """ |
| 70 | Move the focus to the left child of the current node. |
| 71 | |
| 72 | Returns: |
| 73 | A new Zipper focused on the left child, or None if no left child exists |
| 74 | """ |
| 75 | # Edge Case: Handle None tree |
| 76 | if self.tree is None or self.tree['left'] is None: |
| 77 | return None |
| 78 | |
| 79 | # Create breadcrumb to remember how to reconstruct the parent |
| 80 | breadcrumb = { |
| 81 | 'parent': { |
| 82 | 'value': self.tree['value'], |
| 83 | 'right': self.tree['right'] |
| 84 | }, |
| 85 | 'is_left': True |
| 86 | } |
| 87 | |
| 88 | return Zipper(self.tree['left'], self.breadcrumbs + [breadcrumb]) |
| 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 left child |
| 99 | """ |
| 100 | # Edge Case: Handle None tree |
| 101 | if self.tree is None: |
| 102 | return self |
| 103 | new_tree = { |
| 104 | 'value': self.tree['value'], |
| 105 | 'left': tree, |
| 106 | 'right': self.tree['right'] |
| 107 | } |
| 108 | return Zipper(new_tree, self.breadcrumbs) |
| 109 | |
| 19 | 110 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 111 | """ |
| 112 | Move the focus to the right child of the current node. |
| 113 | |
| 114 | Returns: |
| 115 | A new Zipper focused on the right child, or None if no right child exists |
| 116 | """ |
| 117 | # Edge Case: Handle None tree |
| 118 | if self.tree is None or self.tree['right'] is None: |
| 119 | return None |
| 120 | |
| 121 | # Create breadcrumb to remember how to reconstruct the parent |
| 122 | breadcrumb = { |
| 123 | 'parent': { |
| 124 | 'value': self.tree['value'], |
| 125 | 'left': self.tree['left'] |
| 126 | }, |
| 127 | 'is_left': False |
| 128 | } |
| 129 | |
| 130 | return Zipper(self.tree['right'], self.breadcrumbs + [breadcrumb]) |
| 131 | |
| 22 | 132 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 133 | """ |
| 134 | Set the right child of the focus node. |
| 135 | |
| 136 | Args: |
| 137 | tree: The new right subtree (or None to remove the right child) |
| 138 | |
| 139 | Returns: |
| 140 | A new Zipper with the updated right child |
| 141 | """ |
| 142 | # Edge Case: Handle None tree |
| 143 | if self.tree is None: |
| 144 | return self |
| 145 | new_tree = { |
| 146 | 'value': self.tree['value'], |
| 147 | 'left': self.tree['left'], |
| 148 | 'right': tree |
| 149 | } |
| 150 | return Zipper(new_tree, self.breadcrumbs) |
| 151 | |
| 25 | 152 | def up(self) -> "Zipper | None": |
| 26 | | pass |
| 27 | | |
| 153 | """ |
| 154 | Move the focus to the parent of the current node. |
| 155 | |
| 156 | Returns: |
| 157 | A new Zipper focused on the parent, or None if already at the root |
| 158 | """ |
| 159 | # Edge Case: Handle when already at root (no breadcrumbs) |
| 160 | if not self.breadcrumbs: |
| 161 | return None |
| 162 | |
| 163 | # Get the last breadcrumb |
| 164 | last_breadcrumb = self.breadcrumbs[-1] |
| 165 | parent_info = last_breadcrumb['parent'] |
| 166 | is_left = last_breadcrumb['is_left'] |
| 167 | |
| 168 | # Reconstruct the parent node |
| 169 | if is_left: |
| 170 | new_tree = { |
| 171 | 'value': parent_info['value'], |
| 172 | 'left': self.tree, |
| 173 | 'right': parent_info['right'] |
| 174 | } |
| 175 | else: |
| 176 | new_tree = { |
| 177 | 'value': parent_info['value'], |
| 178 | 'left': parent_info['left'], |
| 179 | 'right': self.tree |
| 180 | } |
| 181 | |
| 182 | return Zipper(new_tree, self.breadcrumbs[:-1]) |
| 183 | |
| 28 | 184 | def to_tree(self) -> dict: |
| 29 | | pass |
| 185 | """ |
| 186 | Get the complete tree from the zipper. |
| 187 | |
| 188 | Returns: |
| 189 | The full binary tree as a dict |
| 190 | """ |
| 191 | # Navigate back to the root to get the complete tree |
| 192 | current = self |
| 193 | while True: |
| 194 | parent = current.up() |
| 195 | if parent is None: |
| 196 | break |
| 197 | current = parent |
| 198 | return current.tree |
| 199 | |
| 200 | # Handled Edge Cases: None tree input, None tree in operations, moving when no child exists, moving up when at root |