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