fortitude

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 for
6 efficient navigation and modification operations.
7 """
8
9 def __init__(self, tree: dict, path: list = None):
10 """
11 Initialize a Zipper instance.
12
13 Args:
14 tree: The binary tree represented as a dict with keys 'value', 'left', and 'right'.
15 path: A list of tuples representing the path from root to focus node.
16 Each tuple contains (parent_tree, direction) where direction is 'left' or 'right'.
17 """
18 self.tree = tree
19 self.path = path or []
20
21 # Navigate to the focus node based on the path
22 self.focus = self.tree
23 for parent, direction in self.path:
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, with focus on the root node.
33
34 Args:
35 tree: The binary tree represented as a dict with keys 'value', 'left', and 'right'.
36
37 Returns:
38 A new Zipper instance focused on the root of the tree.
39
40 Edge Case: Empty tree (None) - Returns a Zipper with an empty tree.
41 """
42 # Edge Case: Empty tree
43 if tree is None:
44 return Zipper({"value": None, "left": None, "right": None}, [])
45
46 return Zipper(tree, [])
47
748 def value(self) -> int:
8 pass
9
49 """
50 Get the value of the focus node.
51
52 Returns:
53 The value of the focus node.
54
55 Edge Case: Focus node is None - Returns None.
56 """
57 # Edge Case: Focus node is None
58 if self.focus is None:
59 return None
60
61 return self.focus['value']
62
1063 def set_value(self, value: int) -> "Zipper":
11 pass
12
64 """
65 Set the value of the focus node, returning a new zipper.
66
67 Args:
68 value: The new value for the focus node.
69
70 Returns:
71 A new Zipper instance with the updated value.
72
73 Edge Case: Focus node is None - Returns a new Zipper with the same state.
74 """
75 # Edge Case: Focus node is None
76 if self.focus is None:
77 return Zipper(self.tree, self.path)
78
79 # Create a deep copy of the tree
80 new_tree = self._deep_copy_tree(self.tree)
81
82 # Navigate to the focus node in the new tree
83 new_focus = new_tree
84 for parent, direction in self.path:
85 if direction == 'left':
86 new_focus = new_focus['left']
87 else: # direction == 'right'
88 new_focus = new_focus['right']
89
90 # Update the value
91 new_focus['value'] = value
92
93 return Zipper(new_tree, self.path)
94
1395 def left(self) -> "Zipper | None":
14 pass
15
96 """
97 Move the focus to the left child of the current focus node.
98
99 Returns:
100 A new Zipper focused on the left child, or None if there is no left child.
101
102 Edge Case: Focus node is None - Returns None.
103 Edge Case: No left child - Returns None.
104 """
105 # Edge Case: Focus node is None
106 if self.focus is None:
107 return None
108
109 # Edge Case: No left child
110 if self.focus['left'] is None:
111 return None
112
113 new_path = self.path + [(self.focus, 'left')]
114 return Zipper(self.tree, new_path)
115
16116 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
117 """
118 Set the left child of the focus node, returning a new zipper.
119
120 Args:
121 tree: The new left subtree (or None to remove the left child).
122
123 Returns:
124 A new Zipper instance with the updated left child.
125
126 Edge Case: Focus node is None - Returns a new Zipper with the same state.
127 """
128 # Edge Case: Focus node is None
129 if self.focus is None:
130 return Zipper(self.tree, self.path)
131
132 # Create a deep copy of the tree
133 new_tree = self._deep_copy_tree(self.tree)
134
135 # Navigate to the focus node in the new tree
136 new_focus = new_tree
137 for parent, direction in self.path:
138 if direction == 'left':
139 new_focus = new_focus['left']
140 else: # direction == 'right'
141 new_focus = new_focus['right']
142
143 # Update the left child
144 new_focus['left'] = tree
145
146 return Zipper(new_tree, self.path)
147
19148 def right(self) -> "Zipper | None":
20 pass
21
149 """
150 Move the focus to the right child of the current focus node.
151
152 Returns:
153 A new Zipper focused on the right child, or None if there is no right child.
154
155 Edge Case: Focus node is None - Returns None.
156 Edge Case: No right child - Returns None.
157 """
158 # Edge Case: Focus node is None
159 if self.focus is None:
160 return None
161
162 # Edge Case: No right child
163 if self.focus['right'] is None:
164 return None
165
166 new_path = self.path + [(self.focus, 'right')]
167 return Zipper(self.tree, new_path)
168
22169 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
170 """
171 Set the right child of the focus node, returning a new zipper.
172
173 Args:
174 tree: The new right subtree (or None to remove the right child).
175
176 Returns:
177 A new Zipper instance with the updated right child.
178
179 Edge Case: Focus node is None - Returns a new Zipper with the same state.
180 """
181 # Edge Case: Focus node is None
182 if self.focus is None:
183 return Zipper(self.tree, self.path)
184
185 # Create a deep copy of the tree
186 new_tree = self._deep_copy_tree(self.tree)
187
188 # Navigate to the focus node in the new tree
189 new_focus = new_tree
190 for parent, direction in self.path:
191 if direction == 'left':
192 new_focus = new_focus['left']
193 else: # direction == 'right'
194 new_focus = new_focus['right']
195
196 # Update the right child
197 new_focus['right'] = tree
198
199 return Zipper(new_tree, self.path)
200
25201 def up(self) -> "Zipper | None":
26 pass
27
202 """
203 Move the focus to the parent of the current focus node.
204
205 Returns:
206 A new Zipper focused on the parent node, or None if the focus is already at the root.
207
208 Edge Case: Focus is at root - Returns None.
209 """
210 # Edge Case: Focus is at root
211 if not self.path:
212 return None
213
214 new_path = self.path[:-1]
215 return Zipper(self.tree, new_path)
216
28217 def to_tree(self) -> dict:
29 pass
218 """
219 Get the complete binary tree from the zipper.
220
221 Returns:
222 The binary tree represented as a dict with keys 'value', 'left', and 'right'.
223 """
224 return self.tree
225
226 def _deep_copy_tree(self, tree: dict) -> dict:
227 """
228 Create a deep copy of a binary tree.
229
230 Args:
231 tree: The binary tree to copy.
232
233 Returns:
234 A deep copy of the binary tree.
235 """
236 if tree is None:
237 return None
238
239 return {
240 'value': tree['value'],
241 'left': self._deep_copy_tree(tree['left']),
242 'right': self._deep_copy_tree(tree['right'])
243 }
244
245 # Handled Edge Cases: Empty tree, Focus node is None, No left child, No right child, Focus is 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.