bohdan

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 movement and modification operations while preserving the tree structure.
7 """
8
9 def __init__(self, tree, path=None):
10 """
11 Initialize a Zipper with a tree and a path to the focus node.
12
13 Args:
14 tree (dict): The complete tree structure
15 path (list): A list of (parent_node, direction) tuples representing the path to focus
16 """
17 self.tree = tree
18 # Edge Case: Initialize path as empty list if None is provided
19 self.path = path if path is not None else []
20
21 # Calculate the current focus based on the path
22 self.focus = self.tree
23 for parent, direction in self.path:
24 if direction == 'left':
25 self.focus = parent['left']
26 else: # direction == 'right'
27 self.focus = parent['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 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 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 and return 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 the updated value
66 """
67 # Edge Case: Handle None focus
68 if self.focus 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 position in the new tree
75 new_focus = new_tree
76 for parent, direction in self.path:
77 if direction == 'left':
78 new_focus = new_focus['left']
79 else: # direction == 'right'
80 new_focus = new_focus['right']
81
82 # Update the value
83 new_focus['value'] = value
84
85 return Zipper(new_tree, list(self.path))
86
1387 def left(self) -> "Zipper | None":
14 pass
15
88 """
89 Move the focus to the left child and return a new zipper.
90
91 Returns:
92 Zipper | None: A new zipper focused on the left child, or None if no left child exists
93 """
94 # Edge Case: Handle None focus
95 if self.focus is None:
96 return None
97
98 # Edge Case: Handle missing left child
99 if self.focus['left'] is None:
100 return None
101
102 # Create a new path that includes the move to the left child
103 new_path = list(self.path)
104 new_path.append((self.focus, 'left'))
105
106 return Zipper(self.tree, new_path)
107
16108 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
109 """
110 Set the left child of the focus node and return a new zipper.
111
112 Args:
113 tree (dict | None): The new left subtree
114
115 Returns:
116 Zipper: A new zipper with the updated left child
117 """
118 # Edge Case: Handle None focus
119 if self.focus 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 position in the new tree
126 new_focus = new_tree
127 for parent, direction in self.path:
128 if direction == 'left':
129 new_focus = new_focus['left']
130 else: # direction == 'right'
131 new_focus = new_focus['right']
132
133 # Update the left child
134 new_focus['left'] = tree
135
136 return Zipper(new_tree, list(self.path))
137
19138 def right(self) -> "Zipper | None":
20 pass
21
139 """
140 Move the focus to the right child and return a new zipper.
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 focus
146 if self.focus is None:
147 return None
148
149 # Edge Case: Handle missing right child
150 if self.focus['right'] is None:
151 return None
152
153 # Create a new path that includes the move to the right child
154 new_path = list(self.path)
155 new_path.append((self.focus, 'right'))
156
157 return Zipper(self.tree, new_path)
158
22159 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
160 """
161 Set the right child of the focus node and return a new zipper.
162
163 Args:
164 tree (dict | None): The new right subtree
165
166 Returns:
167 Zipper: A new zipper with the updated right child
168 """
169 # Edge Case: Handle None focus
170 if self.focus 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 position in the new tree
177 new_focus = new_tree
178 for parent, direction in self.path:
179 if direction == 'left':
180 new_focus = new_focus['left']
181 else: # direction == 'right'
182 new_focus = new_focus['right']
183
184 # Update the right child
185 new_focus['right'] = tree
186
187 return Zipper(new_tree, list(self.path))
188
25189 def up(self) -> "Zipper | None":
26 pass
27
190 """
191 Move the focus to the parent node and return a new zipper.
192
193 Returns:
194 Zipper | None: A new zipper focused on the parent, or None if already at root
195 """
196 # Edge Case: Handle None focus
197 if self.focus is None:
198 return None
199
200 # Edge Case: Handle already at root (no parent)
201 if not self.path:
202 return None
203
204 # Create a new path without the last element (move up one level)
205 new_path = self.path[:-1]
206
207 return Zipper(self.tree, new_path)
208
28209 def to_tree(self) -> dict:
29 pass
210 """
211 Get the complete tree from the zipper.
212
213 Returns:
214 dict: The complete binary tree
215 """
216 return self.tree
217
218 def _deep_copy_tree(self, tree):
219 """
220 Create a deep copy of a binary tree.
221
222 Args:
223 tree (dict): The tree to copy
224
225 Returns:
226 dict: A deep copy of the tree
227 """
228 if tree is None:
229 return None
230
231 return {
232 'value': tree['value'],
233 'left': self._deep_copy_tree(tree['left']),
234 'right': self._deep_copy_tree(tree['right'])
235 }
236
237 # Handled Edge Cases: None tree input, None focus, 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.