goodagent

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 and allows
6 navigation and modification operations while preserving immutability.
7 """
8
9 def __init__(self, tree, path):
10 """
11 Initialize a Zipper instance.
12
13 Args:
14 tree (dict): The tree structure
15 path (list): Path from root to current focus node
16 """
17 self.tree = tree
18 self.path = path
19
320 @staticmethod
421 def from_tree(tree: dict) -> "Zipper":
5 pass
6
22 """
23 Create a zipper from a binary tree.
24
25 Args:
26 tree (dict): A binary tree with keys "value", "left", and "right"
27
28 Returns:
29 Zipper: A new zipper focused on the root of the tree
30 """
31 # Edge Case: Empty tree
32 if tree is None:
33 return None
34 return Zipper(tree, [])
35
36 def _get_focus_node(self):
37 """
38 Get the current focus node based on the path.
39
40 Returns:
41 dict: The focus node
42 """
43 node = self.tree
44 for direction, parent in self.path:
45 if direction == 'left':
46 node = parent['left']
47 else: # direction == 'right'
48 node = parent['right']
49 return node
50
751 def value(self) -> int:
8 pass
9
52 """
53 Get the value of the focus node.
54
55 Returns:
56 int: The value of the focus node
57 """
58 return self._get_focus_node()['value']
59
1060 def set_value(self, value: int) -> "Zipper":
11 pass
12
61 """
62 Set the value of the focus node, returning a new zipper.
63
64 Args:
65 value (int): The new value
66
67 Returns:
68 Zipper: A new zipper with the updated value
69 """
70 # Create a deep copy of the tree
71 new_tree = self._deep_copy_tree(self.tree)
72
73 # Navigate to the focus node in the copied tree
74 node = new_tree
75 for direction, parent in self.path:
76 if direction == 'left':
77 node = node['left']
78 else: # direction == 'right'
79 node = node['right']
80
81 # Update the value
82 node['value'] = value
83
84 return Zipper(new_tree, self.path[:])
85
1386 def left(self) -> "Zipper | None":
14 pass
15
87 """
88 Move the focus to the left child of the current node.
89
90 Returns:
91 Zipper | None: A new zipper focused on the left child, or None if no left child
92 """
93 focus_node = self._get_focus_node()
94 # Edge Case: No left child
95 if focus_node['left'] is None:
96 return None
97
98 # Create a new path entry for moving left
99 new_path = self.path + [('left', focus_node)]
100 return Zipper(self.tree, new_path)
101
16102 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
103 """
104 Set the left child of the focus node, returning a new zipper.
105
106 Args:
107 tree (dict | None): The new left subtree
108
109 Returns:
110 Zipper: A new zipper with the updated left child
111 """
112 # Create a deep copy of the tree
113 new_tree = self._deep_copy_tree(self.tree)
114
115 # Navigate to the focus node in the copied tree
116 node = new_tree
117 for direction, parent in self.path:
118 if direction == 'left':
119 node = node['left']
120 else: # direction == 'right'
121 node = node['right']
122
123 # Update the left child
124 node['left'] = tree
125
126 return Zipper(new_tree, self.path[:])
127
19128 def right(self) -> "Zipper | None":
20 pass
21
129 """
130 Move the focus to the right child of the current node.
131
132 Returns:
133 Zipper | None: A new zipper focused on the right child, or None if no right child
134 """
135 focus_node = self._get_focus_node()
136 # Edge Case: No right child
137 if focus_node['right'] is None:
138 return None
139
140 # Create a new path entry for moving right
141 new_path = self.path + [('right', focus_node)]
142 return Zipper(self.tree, new_path)
143
22144 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
145 """
146 Set the right child of the focus node, returning a new zipper.
147
148 Args:
149 tree (dict | None): The new right subtree
150
151 Returns:
152 Zipper: A new zipper with the updated right child
153 """
154 # Create a deep copy of the tree
155 new_tree = self._deep_copy_tree(self.tree)
156
157 # Navigate to the focus node in the copied tree
158 node = new_tree
159 for direction, parent in self.path:
160 if direction == 'left':
161 node = node['left']
162 else: # direction == 'right'
163 node = node['right']
164
165 # Update the right child
166 node['right'] = tree
167
168 return Zipper(new_tree, self.path[:])
169
25170 def up(self) -> "Zipper | None":
26 pass
27
171 """
172 Move the focus to the parent of the current node.
173
174 Returns:
175 Zipper | None: A new zipper focused on the parent, or None if at root
176 """
177 # Edge Case: Already at root (no parent)
178 if not self.path:
179 return None
180
181 # Remove the last path entry to move up
182 new_path = self.path[:-1]
183 return Zipper(self.tree, new_path)
184
28185 def to_tree(self) -> dict:
29 pass
186 """
187 Get the complete tree from the zipper.
188
189 Returns:
190 dict: The complete tree
191 """
192 return self.tree
193
194 def _deep_copy_tree(self, tree):
195 """
196 Create a deep copy of a binary tree.
197
198 Args:
199 tree (dict): The tree to copy
200
201 Returns:
202 dict: A deep copy of the tree
203 """
204 if tree is None:
205 return None
206
207 copied_tree = {
208 'value': tree['value'],
209 'left': self._deep_copy_tree(tree['left']),
210 'right': self._deep_copy_tree(tree['right'])
211 }
212 return copied_tree
213
214# Handled Edge Cases: Empty tree in from_tree, no left child in left, no right child in right, already at root in up
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.