agent

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 A zipper maintains a focus on a specific node in the tree and allows
6 navigation and modification operations while preserving the tree structure.
7 """
8
9 def __init__(self, tree, path):
10 """
11 Initialize a Zipper instance.
12
13 Args:
14 tree (dict): The tree structure being navigated.
15 path (list): A list of breadcrumbs representing the path from root to focus.
16 """
17 self.tree = tree
18 self.path = path
19
20 # Calculate current focus based on path
21 self.focus = tree
22 for breadcrumb in self.path:
23 direction, parent, sibling = breadcrumb
24 if direction == 'left':
25 self.focus = self.focus['left']
26 else: # direction == 'right'
27 self.focus = self.focus['right']
28
329 @staticmethod
430 def from_tree(tree: dict) -> "Zipper":
5 pass
6
31 """
32 Create a zipper from a binary tree.
33
34 Args:
35 tree (dict): A binary tree with keys "value", "left", and "right".
36
37 Returns:
38 Zipper: A new zipper with focus on the root node.
39 """
40 # Edge Case: Handle None tree input
41 if tree is None:
42 return None
43 return Zipper(tree, [])
44
745 def value(self) -> int:
8 pass
9
46 """
47 Get the value of the focus node.
48
49 Returns:
50 int: The value of the focus node.
51 """
52 # Edge Case: Handle None focus
53 if self.focus is None:
54 return None
55 return self.focus['value']
56
1057 def set_value(self, value: int) -> "Zipper":
11 pass
12
58 """
59 Set the value of the focus node.
60
61 Args:
62 value (int): The new value for the focus node.
63
64 Returns:
65 Zipper: A new zipper with the updated value.
66 """
67 # Edge Case: Handle None focus
68 if self.focus is None:
69 return self
70
71 # Create a new tree with updated value
72 new_tree = self._reconstruct_tree()
73
74 # Navigate to the focus position in the new tree
75 current = new_tree
76 for breadcrumb in self.path:
77 direction, _, _ = breadcrumb
78 if direction == 'left':
79 current = current['left']
80 else: # direction == 'right'
81 current = current['right']
82
83 # Update the value
84 current['value'] = value
85
86 return Zipper(new_tree, self.path)
87
1388 def left(self) -> "Zipper | None":
14 pass
15
89 """
90 Move the focus to the left child of the current node.
91
92 Returns:
93 Zipper | None: A new zipper focused on the left child, or None if no left child exists.
94 """
95 # Edge Case: Handle None focus
96 if self.focus is None or self.focus['left'] is None:
97 return None
98
99 # Create breadcrumb for current node
100 breadcrumb = ('left', self.focus, self.focus['right'])
101
102 return Zipper(self.tree, self.path + [breadcrumb])
103
16104 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
105 """
106 Set the left child of the focus node.
107
108 Args:
109 tree (dict | None): The new left subtree.
110
111 Returns:
112 Zipper: A new zipper with the updated left child.
113 """
114 # Edge Case: Handle None focus
115 if self.focus is None:
116 return self
117
118 # Create a new tree with updated left child
119 new_tree = self._reconstruct_tree()
120
121 # Navigate to the focus position in the new tree
122 current = new_tree
123 for breadcrumb in self.path:
124 direction, _, _ = breadcrumb
125 if direction == 'left':
126 current = current['left']
127 else: # direction == 'right'
128 current = current['right']
129
130 # Update the left child
131 current['left'] = tree
132
133 return Zipper(new_tree, self.path)
134
19135 def right(self) -> "Zipper | None":
20 pass
21
136 """
137 Move the focus to the right child of the current node.
138
139 Returns:
140 Zipper | None: A new zipper focused on the right child, or None if no right child exists.
141 """
142 # Edge Case: Handle None focus
143 if self.focus is None or self.focus['right'] is None:
144 return None
145
146 # Create breadcrumb for current node
147 breadcrumb = ('right', self.focus, self.focus['left'])
148
149 return Zipper(self.tree, self.path + [breadcrumb])
150
22151 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
152 """
153 Set the right child of the focus node.
154
155 Args:
156 tree (dict | None): The new right subtree.
157
158 Returns:
159 Zipper: A new zipper with the updated right child.
160 """
161 # Edge Case: Handle None focus
162 if self.focus is None:
163 return self
164
165 # Create a new tree with updated right child
166 new_tree = self._reconstruct_tree()
167
168 # Navigate to the focus position in the new tree
169 current = new_tree
170 for breadcrumb in self.path:
171 direction, _, _ = breadcrumb
172 if direction == 'left':
173 current = current['left']
174 else: # direction == 'right'
175 current = current['right']
176
177 # Update the right child
178 current['right'] = tree
179
180 return Zipper(new_tree, self.path)
181
25182 def up(self) -> "Zipper | None":
26 pass
27
183 """
184 Move the focus to the parent of the current node.
185
186 Returns:
187 Zipper | None: A new zipper focused on the parent, or None if already at root.
188 """
189 # Edge Case: Handle when already at root (no path)
190 if not self.path:
191 return None
192
193 # Remove the last breadcrumb to move up
194 new_path = self.path[:-1]
195
196 return Zipper(self.tree, new_path)
197
28198 def to_tree(self) -> dict:
29 pass
199 """
200 Get the complete tree from the zipper.
201
202 Returns:
203 dict: The complete binary tree.
204 """
205 return self.tree
206
207 def _reconstruct_tree(self):
208 """
209 Reconstruct the tree with all modifications applied.
210
211 Returns:
212 dict: A new tree with all changes applied.
213 """
214 import copy
215 return copy.deepcopy(self.tree)
216
217 # Handled Edge Cases: None tree input, None focus, 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.