sn62

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 with context
16 """
17 self.tree = tree
18 self.path = path
19
20 # Calculate current node based on path
21 self.current = tree
22 for breadcrumb in self.path:
23 direction = breadcrumb['direction']
24 if direction == 'left':
25 self.current = self.current['left']
26 else: # direction == 'right'
27 self.current = self.current['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 (dict): A binary tree with keys "value", "left", "right"
36
37 Returns:
38 Zipper: A new zipper focused on the root of the tree
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 current focus node
51 """
52 # Edge Case: Handle None current node
53 if self.current is None:
54 return None
55 return self.current['value']
56
1057 def set_value(self, value: int) -> "Zipper":
11 pass
12
58 """
59 Set the value of the focus node, returning a new zipper.
60
61 Args:
62 value (int): The new value for the focus node
63
64 Returns:
65 Zipper: A new zipper with updated value
66 """
67 # Edge Case: Handle None current node
68 if self.current is None:
69 return self
70
71 # Create a deep copy of the tree
72 new_tree = self._deep_copy_tree(self.tree)
73
74 # Navigate to the focus node in the new tree and update its value
75 current_in_new = new_tree
76 for breadcrumb in self.path:
77 direction = breadcrumb['direction']
78 if direction == 'left':
79 current_in_new = current_in_new['left']
80 else: # direction == 'right'
81 current_in_new = current_in_new['right']
82
83 current_in_new['value'] = value
84
85 return Zipper(new_tree, self.path[:])
86
1387 def left(self) -> "Zipper | None":
14 pass
15
88 """
89 Move the focus to the left child of the current node.
90
91 Returns:
92 Zipper | None: A new zipper focused on the left child, or None if no left child
93 """
94 # Edge Case: Handle None current node
95 if self.current is None or self.current['left'] is None:
96 return None
97
98 # Create a new breadcrumb for the current node
99 breadcrumb = {
100 'parent': self.current,
101 'direction': 'left',
102 'sibling': self.current['right']
103 }
104
105 # Return a new zipper with updated path
106 return Zipper(self.tree, self.path + [breadcrumb])
107
16108 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
109 """
110 Set the left child of the focus node, returning a new zipper.
111
112 Args:
113 tree (dict | None): The new left subtree
114
115 Returns:
116 Zipper: A new zipper with updated left child
117 """
118 # Edge Case: Handle None current node
119 if self.current is None:
120 return self
121
122 # Create a deep copy of the tree
123 new_tree = self._deep_copy_tree(self.tree)
124
125 # Navigate to the focus node in the new tree and update its left child
126 current_in_new = new_tree
127 for breadcrumb in self.path:
128 direction = breadcrumb['direction']
129 if direction == 'left':
130 current_in_new = current_in_new['left']
131 else: # direction == 'right'
132 current_in_new = current_in_new['right']
133
134 current_in_new['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
144 """
145 # Edge Case: Handle None current node
146 if self.current is None or self.current['right'] is None:
147 return None
148
149 # Create a new breadcrumb for the current node
150 breadcrumb = {
151 'parent': self.current,
152 'direction': 'right',
153 'sibling': self.current['left']
154 }
155
156 # Return a new zipper with updated path
157 return Zipper(self.tree, self.path + [breadcrumb])
158
22159 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
160 """
161 Set the right child of the focus node, returning a new zipper.
162
163 Args:
164 tree (dict | None): The new right subtree
165
166 Returns:
167 Zipper: A new zipper with updated right child
168 """
169 # Edge Case: Handle None current node
170 if self.current is None:
171 return self
172
173 # Create a deep copy of the tree
174 new_tree = self._deep_copy_tree(self.tree)
175
176 # Navigate to the focus node in the new tree and update its right child
177 current_in_new = new_tree
178 for breadcrumb in self.path:
179 direction = breadcrumb['direction']
180 if direction == 'left':
181 current_in_new = current_in_new['left']
182 else: # direction == 'right'
183 current_in_new = current_in_new['right']
184
185 current_in_new['right'] = tree
186
187 return Zipper(new_tree, self.path[:])
188
25189 def up(self) -> "Zipper | None":
26 pass
27
190 """
191 Move the focus to the parent of the current node.
192
193 Returns:
194 Zipper | None: A new zipper focused on the parent, or None if at root
195 """
196 # Edge Case: Handle already at root (no path)
197 if not self.path:
198 return None
199
200 # Return a new zipper with the last breadcrumb removed
201 return Zipper(self.tree, self.path[:-1])
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 return self.tree
211
212 def _deep_copy_tree(self, tree):
213 """
214 Create a deep copy of a binary tree.
215
216 Args:
217 tree (dict): The tree to copy
218
219 Returns:
220 dict: A deep copy of the tree
221 """
222 if tree is None:
223 return None
224
225 return {
226 'value': tree['value'],
227 'left': self._deep_copy_tree(tree['left']),
228 'right': self._deep_copy_tree(tree['right'])
229 }
230
231 # Handled Edge Cases: None tree input, None current 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.