let it go

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 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
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: Handle None tree input
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 in the tree
42 """
43 node = self.tree
44 for direction, parent in self.path:
45 if direction == 'left':
46 node = node['left']
47 else: # direction == 'right'
48 node = node['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 # Edge Case: Handle None tree
59 if self.tree is None:
60 return None
61 return self._get_focus_node()['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 (int): The new value for the focus node
69
70 Returns:
71 Zipper: A new zipper with the updated value
72 """
73 # Edge Case: Handle None tree
74 if self.tree is None:
75 return self
76
77 # Create a deep copy of the tree
78 new_tree = self._deep_copy_tree(self.tree)
79
80 # Navigate to the focus node in the copied tree and update its value
81 node = new_tree
82 for direction, parent in self.path:
83 if direction == 'left':
84 node = node['left']
85 else: # direction == 'right'
86 node = node['right']
87 node['value'] = value
88
89 return Zipper(new_tree, self.path[:])
90
1391 def left(self) -> "Zipper | None":
14 pass
15
92 """
93 Move the focus to the left child of the current node.
94
95 Returns:
96 Zipper | None: A new zipper focused on the left child, or None if no left child exists
97 """
98 # Edge Case: Handle None tree
99 if self.tree is None:
100 return None
101
102 focus_node = self._get_focus_node()
103 # Edge Case: Handle missing left child
104 if focus_node['left'] is None:
105 return None
106
107 new_path = self.path + [('left', focus_node)]
108 return Zipper(self.tree, new_path)
109
16110 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
111 """
112 Set the left child of the focus node, returning a new zipper.
113
114 Args:
115 tree (dict | None): The new left subtree
116
117 Returns:
118 Zipper: A new zipper with the updated left child
119 """
120 # Edge Case: Handle None tree
121 if self.tree is None:
122 return self
123
124 # Create a deep copy of the tree
125 new_tree = self._deep_copy_tree(self.tree)
126
127 # Navigate to the focus node in the copied tree and update its left child
128 node = new_tree
129 for direction, parent in self.path:
130 if direction == 'left':
131 node = node['left']
132 else: # direction == 'right'
133 node = node['right']
134 node['left'] = tree
135
136 return Zipper(new_tree, self.path[:])
137
19138 def right(self) -> "Zipper | None":
20 pass
21
139 """
140 Move the focus to the right child of the current node.
141
142 Returns:
143 Zipper | None: A new zipper focused on the right child, or None if no right child exists
144 """
145 # Edge Case: Handle None tree
146 if self.tree is None:
147 return None
148
149 focus_node = self._get_focus_node()
150 # Edge Case: Handle missing right child
151 if focus_node['right'] is None:
152 return None
153
154 new_path = self.path + [('right', focus_node)]
155 return Zipper(self.tree, new_path)
156
22157 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
158 """
159 Set the right child of the focus node, returning a new zipper.
160
161 Args:
162 tree (dict | None): The new right subtree
163
164 Returns:
165 Zipper: A new zipper with the updated right child
166 """
167 # Edge Case: Handle None tree
168 if self.tree is None:
169 return self
170
171 # Create a deep copy of the tree
172 new_tree = self._deep_copy_tree(self.tree)
173
174 # Navigate to the focus node in the copied tree and update its right child
175 node = new_tree
176 for direction, parent in self.path:
177 if direction == 'left':
178 node = node['left']
179 else: # direction == 'right'
180 node = node['right']
181 node['right'] = tree
182
183 return Zipper(new_tree, self.path[:])
184
25185 def up(self) -> "Zipper | None":
26 pass
27
186 """
187 Move the focus to the parent of the current node.
188
189 Returns:
190 Zipper | None: A new zipper focused on the parent, or None if already at root
191 """
192 # Edge Case: Handle None tree
193 if self.tree is None:
194 return None
195
196 # Edge Case: Handle already at root (no parent)
197 if not self.path:
198 return None
199
200 new_path = self.path[:-1]
201 return Zipper(self.tree, new_path)
202
28203 def to_tree(self) -> dict:
29 pass
204 """
205 Get the complete tree from the zipper.
206
207 Returns:
208 dict: The complete binary tree
209 """
210 # Edge Case: Handle None tree
211 if self.tree is None:
212 return None
213 return self.tree
214
215 def _deep_copy_tree(self, tree):
216 """
217 Create a deep copy of a binary tree.
218
219 Args:
220 tree (dict): The tree to copy
221
222 Returns:
223 dict: A deep copy of the tree
224 """
225 if tree is None:
226 return None
227 return {
228 'value': tree['value'],
229 'left': self._deep_copy_tree(tree['left']),
230 'right': self._deep_copy_tree(tree['right'])
231 }
232
233 # Handled Edge Cases: None tree input, missing left child, missing 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.