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