| 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 and allows |
| 6 | traversal and modification operations. |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, tree, breadcrumbs=None): |
| 10 | """ |
| 11 | Initialize a Zipper. |
| 12 | |
| 13 | Args: |
| 14 | tree: The current subtree (focus) |
| 15 | breadcrumbs: List of parent contexts for navigation back up the tree |
| 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", and "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 | 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 value input |
| 56 | if value is None: |
| 57 | raise ValueError("Value cannot be None") |
| 58 | |
| 59 | new_tree = { |
| 60 | "value": value, |
| 61 | "left": self.tree["left"], |
| 62 | "right": self.tree["right"] |
| 63 | } |
| 64 | return Zipper(new_tree, self.breadcrumbs) |
| 65 | |
| 13 | 66 | def left(self) -> "Zipper | None": |
| 14 | | pass |
| 15 | | |
| 67 | """ |
| 68 | Move the focus to the left child of the current node. |
| 69 | |
| 70 | Returns: |
| 71 | A new Zipper focused on the left child, or None if no left child exists |
| 72 | """ |
| 73 | # Edge Case: Handle missing left child |
| 74 | if self.tree["left"] is None: |
| 75 | return None |
| 76 | |
| 77 | # Create breadcrumb to remember how to reconstruct the parent |
| 78 | breadcrumb = { |
| 79 | "parent": { |
| 80 | "value": self.tree["value"], |
| 81 | "right": self.tree["right"] |
| 82 | }, |
| 83 | "is_left": True |
| 84 | } |
| 85 | |
| 86 | return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb]) |
| 87 | |
| 16 | 88 | def set_left(self, tree: dict | None) -> "Zipper": |
| 17 | | pass |
| 18 | | |
| 89 | """ |
| 90 | Set the left child of the focus node. |
| 91 | |
| 92 | Args: |
| 93 | tree: The new left subtree (or None to remove the left child) |
| 94 | |
| 95 | Returns: |
| 96 | A new Zipper with the updated left child |
| 97 | """ |
| 98 | new_tree = { |
| 99 | "value": self.tree["value"], |
| 100 | "left": tree, |
| 101 | "right": self.tree["right"] |
| 102 | } |
| 103 | return Zipper(new_tree, self.breadcrumbs) |
| 104 | |
| 19 | 105 | def right(self) -> "Zipper | None": |
| 20 | | pass |
| 21 | | |
| 106 | """ |
| 107 | Move the focus to the right child of the current node. |
| 108 | |
| 109 | Returns: |
| 110 | A new Zipper focused on the right child, or None if no right child exists |
| 111 | """ |
| 112 | # Edge Case: Handle missing right child |
| 113 | if self.tree["right"] is None: |
| 114 | return None |
| 115 | |
| 116 | # Create breadcrumb to remember how to reconstruct the parent |
| 117 | breadcrumb = { |
| 118 | "parent": { |
| 119 | "value": self.tree["value"], |
| 120 | "left": self.tree["left"] |
| 121 | }, |
| 122 | "is_left": False |
| 123 | } |
| 124 | |
| 125 | return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb]) |
| 126 | |
| 22 | 127 | def set_right(self, tree: dict | None) -> "Zipper": |
| 23 | | pass |
| 24 | | |
| 128 | """ |
| 129 | Set the right child of the focus node. |
| 130 | |
| 131 | Args: |
| 132 | tree: The new right subtree (or None to remove the right child) |
| 133 | |
| 134 | Returns: |
| 135 | A new Zipper with the updated right child |
| 136 | """ |
| 137 | new_tree = { |
| 138 | "value": self.tree["value"], |
| 139 | "left": self.tree["left"], |
| 140 | "right": tree |
| 141 | } |
| 142 | return Zipper(new_tree, self.breadcrumbs) |
| 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 focused on the parent, or None if already at the root |
| 150 | """ |
| 151 | # Edge Case: Handle attempt to move up from root |
| 152 | if not self.breadcrumbs: |
| 153 | return None |
| 154 | |
| 155 | # Get the last breadcrumb |
| 156 | last_breadcrumb = self.breadcrumbs[-1] |
| 157 | parent = last_breadcrumb["parent"] |
| 158 | is_left = last_breadcrumb["is_left"] |
| 159 | |
| 160 | # Reconstruct the parent tree with the current subtree in the correct position |
| 161 | if is_left: |
| 162 | new_tree = { |
| 163 | "value": parent["value"], |
| 164 | "left": self.tree, |
| 165 | "right": parent["right"] |
| 166 | } |
| 167 | else: |
| 168 | new_tree = { |
| 169 | "value": parent["value"], |
| 170 | "left": parent["left"], |
| 171 | "right": self.tree |
| 172 | } |
| 173 | |
| 174 | return Zipper(new_tree, self.breadcrumbs[:-1]) |
| 175 | |
| 28 | 176 | def to_tree(self) -> dict: |
| 29 | | pass |
| 177 | """ |
| 178 | Get the complete binary tree from the zipper. |
| 179 | |
| 180 | Returns: |
| 181 | The complete binary tree as a dict |
| 182 | """ |
| 183 | # Navigate back to the root to get the complete tree |
| 184 | current = self |
| 185 | while current.breadcrumbs: |
| 186 | current = current.up() |
| 187 | return current.tree |
| 188 | |
| 189 | # Handled Edge Cases: None tree input, None value input, missing left child, missing right child, |
| 190 | # attempt to move up from root |