rockstar

Finished
11class 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's structure.
7 """
8
9 def __init__(self, tree, path=None):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree (dict): The binary tree represented as a dict with keys 'value', 'left', 'right'.
15 path (list): A list of tuples representing the path from root to focus node.
16 Each tuple contains (parent_node, direction) where direction is 'left' or 'right'.
17 """
18 self.tree = tree
19 self.path = path or []
20
321 @staticmethod
422 def from_tree(tree: dict) -> "Zipper":
5 pass
6
23 """
24 Create a zipper from a binary tree.
25
26 Args:
27 tree (dict): The binary tree to create a zipper for.
28
29 Returns:
30 Zipper: A new zipper with focus on the root node.
31
32 Edge Case: Empty tree (None) is handled by creating an empty path.
33 """
34 # Edge Case: Handle None tree
35 if tree is None:
36 return Zipper({}, [])
37 return Zipper(tree, [])
38
39 def _get_focus_node(self):
40 """
41 Get the current focus node by following the path from root.
42
43 Returns:
44 dict: The focus node, or None if the path leads to a None node.
45 """
46 node = self.tree
47 for parent, direction in self.path:
48 if node is None:
49 return None
50 node = node.get(direction)
51 return node
52
753 def value(self) -> int:
8 pass
9
54 """
55 Get the value of the focus node.
56
57 Returns:
58 int: The value of the focus node.
59
60 Edge Case: If focus node is None, this would raise an error in a real implementation,
61 but according to problem constraints, we assume valid operations.
62 """
63 node = self._get_focus_node()
64 return node["value"]
65
1066 def set_value(self, value: int) -> "Zipper":
11 pass
12
67 """
68 Set the value of the focus node.
69
70 Args:
71 value (int): The new value for the focus node.
72
73 Returns:
74 Zipper: A new zipper with the updated value.
75
76 Edge Case: If focus node is None, this would not work correctly, but we assume valid operations.
77 """
78 # Create a deep copy of the tree
79 new_tree = self._deep_copy_tree(self.tree)
80
81 # Navigate to the focus node in the copied tree
82 node = new_tree
83 for parent, direction in self.path:
84 node = node[direction]
85
86 # Update the value
87 node["value"] = value
88
89 return Zipper(new_tree, list(self.path))
90
1391 def left(self) -> "Zipper | None":
14 pass
15
92 """
93 Move the focus to the left child of the current focus node.
94
95 Returns:
96 Zipper | None: A new zipper with focus on the left child, or None if no left child exists.
97
98 Edge Case: If focus node is None or has no left child, return None.
99 """
100 node = self._get_focus_node()
101 # Edge Case: Focus node is None or has no left child
102 if node is None or node.get("left") is None:
103 return None
104
105 new_path = list(self.path)
106 new_path.append((node, "left"))
107 return Zipper(self.tree, new_path)
108
16109 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
110 """
111 Set the left child of the focus node.
112
113 Args:
114 tree (dict | None): The new left subtree.
115
116 Returns:
117 Zipper: A new zipper with the updated left child.
118
119 Edge Case: If focus node is None, this would not work correctly, but we assume valid operations.
120 """
121 # Create a deep copy of the tree
122 new_tree = self._deep_copy_tree(self.tree)
123
124 # Navigate to the focus node in the copied tree
125 node = new_tree
126 for parent, direction in self.path:
127 node = node[direction]
128
129 # Update the left child
130 node["left"] = tree
131
132 return Zipper(new_tree, list(self.path))
133
19134 def right(self) -> "Zipper | None":
20 pass
21
135 """
136 Move the focus to the right child of the current focus node.
137
138 Returns:
139 Zipper | None: A new zipper with focus on the right child, or None if no right child exists.
140
141 Edge Case: If focus node is None or has no right child, return None.
142 """
143 node = self._get_focus_node()
144 # Edge Case: Focus node is None or has no right child
145 if node is None or node.get("right") is None:
146 return None
147
148 new_path = list(self.path)
149 new_path.append((node, "right"))
150 return Zipper(self.tree, new_path)
151
22152 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
153 """
154 Set the right child of the focus node.
155
156 Args:
157 tree (dict | None): The new right subtree.
158
159 Returns:
160 Zipper: A new zipper with the updated right child.
161
162 Edge Case: If focus node is None, this would not work correctly, but we assume valid operations.
163 """
164 # Create a deep copy of the tree
165 new_tree = self._deep_copy_tree(self.tree)
166
167 # Navigate to the focus node in the copied tree
168 node = new_tree
169 for parent, direction in self.path:
170 node = node[direction]
171
172 # Update the right child
173 node["right"] = tree
174
175 return Zipper(new_tree, list(self.path))
176
25177 def up(self) -> "Zipper | None":
26 pass
27
178 """
179 Move the focus to the parent of the current focus node.
180
181 Returns:
182 Zipper | None: A new zipper with focus on the parent, or None if focus is already at root.
183
184 Edge Case: If focus is at root (no parent), return None.
185 """
186 # Edge Case: Already at root (no parent)
187 if not self.path:
188 return None
189
190 new_path = list(self.path)
191 new_path.pop()
192 return Zipper(self.tree, new_path)
193
28194 def to_tree(self) -> dict:
29 pass
195 """
196 Get the complete tree from the zipper.
197
198 Returns:
199 dict: The complete binary tree.
200 """
201 return self.tree
202
203 def _deep_copy_tree(self, tree):
204 """
205 Create a deep copy of a binary tree.
206
207 Args:
208 tree (dict): The tree to copy.
209
210 Returns:
211 dict: A deep copy of the tree.
212 """
213 if tree is None:
214 return None
215
216 copied = {
217 "value": tree["value"],
218 "left": self._deep_copy_tree(tree.get("left")),
219 "right": self._deep_copy_tree(tree.get("right"))
220 }
221 return copied
222
223 # Handled Edge Cases: Empty tree, None focus node, No left child, No right child, Already at root
Test NameStatus
test_data_is_retained
Pass
test_dead_end
Pass
test_different_paths_to_same_zipper
Pass
test_left_right_and_up
Pass
test_left_right_and_value
Pass
test_set_left_with_leaf
Pass
test_set_right_with_null
Pass
test_set_right_with_subtree
Pass
test_set_value
Pass
test_set_value_after_traversing_up
Pass
test_set_value_on_deep_focus
Pass
test_test_ability_to_descend_multiple_levels_and_return
Pass
test_traversing_up_from_top
Pass
test_tree_from_deep_focus
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.