xas

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 a binary tree that allows navigation and manipulation.
4
5 The zipper maintains a focus on a specific node in the tree and allows
6 movement through the tree structure while keeping track of the path
7 taken to reach the current focus.
8 """
9
10 def __init__(self, tree, path):
11 """
12 Initialize a Zipper with a tree and a path to the focus node.
13
14 Args:
15 tree (dict): The complete tree structure
16 path (list): A list of tuples representing the path to the focus node.
17 Each tuple contains (parent_node, direction) where direction
18 is 'left' or 'right'
19 """
20 self.tree = tree
21 self.path = path
22
323 @staticmethod
424 def from_tree(tree: dict) -> "Zipper":
5 pass
6
25 """
26 Create a zipper from a binary tree with focus on the root node.
27
28 Args:
29 tree (dict): A binary tree represented as nested dictionaries
30 with keys "value", "left", and "right"
31
32 Returns:
33 Zipper: A new zipper with focus on the root node
34 """
35 # Edge Case: Handle None tree input
36 if tree is None:
37 return None
38 return Zipper(tree, [])
39
40 def _get_focus_node(self):
41 """
42 Get the current focus node by following the path from root.
43
44 Returns:
45 dict: The current focus node
46 """
47 node = self.tree
48 for parent, direction in self.path:
49 if direction == 'left':
50 node = node['left']
51 else: # direction == 'right'
52 node = node['right']
53 return node
54
755 def value(self) -> int:
8 pass
9
56 """
57 Get the value of the focus node.
58
59 Returns:
60 int: The value of the focus node
61 """
62 # Edge Case: Handle None tree
63 if self.tree is None:
64 return None
65 return self._get_focus_node()['value']
66
1067 def set_value(self, value: int) -> "Zipper":
11 pass
12
68 """
69 Set the value of the focus node and return a new zipper.
70
71 Args:
72 value (int): The new value for the focus node
73
74 Returns:
75 Zipper: A new zipper with the updated value
76 """
77 # Edge Case: Handle None tree
78 if self.tree is None:
79 return self
80
81 # Create a deep copy of the tree
82 new_tree = self._deep_copy_tree(self.tree)
83
84 # Navigate to the focus node in the copied tree and update its value
85 node = new_tree
86 for parent, direction in self.path:
87 if direction == 'left':
88 node = node['left']
89 else: # direction == 'right'
90 node = node['right']
91 node['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 Zipper | None: A new zipper with focus on the left child,
101 or None if there is no left child
102 """
103 # Edge Case: Handle None tree
104 if self.tree is None:
105 return None
106
107 focus_node = self._get_focus_node()
108 # Edge Case: Handle missing left child
109 if focus_node['left'] is None:
110 return None
111
112 new_path = self.path + [(focus_node, 'left')]
113 return Zipper(self.tree, new_path)
114
16115 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
116 """
117 Set the left child of the focus node and return a new zipper.
118
119 Args:
120 tree (dict | None): The new left subtree or None
121
122 Returns:
123 Zipper: A new zipper with the updated left child
124 """
125 # Edge Case: Handle None tree
126 if self.tree is None:
127 return self
128
129 # Create a deep copy of the tree
130 new_tree = self._deep_copy_tree(self.tree)
131
132 # Navigate to the focus node in the copied tree and update its left child
133 node = new_tree
134 for parent, direction in self.path:
135 if direction == 'left':
136 node = node['left']
137 else: # direction == 'right'
138 node = node['right']
139 node['left'] = tree
140
141 return Zipper(new_tree, self.path[:])
142
19143 def right(self) -> "Zipper | None":
20 pass
21
144 """
145 Move the focus to the right child of the current focus node.
146
147 Returns:
148 Zipper | None: A new zipper with focus on the right child,
149 or None if there is no right child
150 """
151 # Edge Case: Handle None tree
152 if self.tree is None:
153 return None
154
155 focus_node = self._get_focus_node()
156 # Edge Case: Handle missing right child
157 if focus_node['right'] is None:
158 return None
159
160 new_path = self.path + [(focus_node, 'right')]
161 return Zipper(self.tree, new_path)
162
22163 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
164 """
165 Set the right child of the focus node and return a new zipper.
166
167 Args:
168 tree (dict | None): The new right subtree or None
169
170 Returns:
171 Zipper: A new zipper with the updated right child
172 """
173 # Edge Case: Handle None tree
174 if self.tree is None:
175 return self
176
177 # Create a deep copy of the tree
178 new_tree = self._deep_copy_tree(self.tree)
179
180 # Navigate to the focus node in the copied tree and update its right child
181 node = new_tree
182 for parent, direction in self.path:
183 if direction == 'left':
184 node = node['left']
185 else: # direction == 'right'
186 node = node['right']
187 node['right'] = tree
188
189 return Zipper(new_tree, self.path[:])
190
25191 def up(self) -> "Zipper | None":
26 pass
27
192 """
193 Move the focus to the parent of the current focus node.
194
195 Returns:
196 Zipper | None: A new zipper with focus on the parent node,
197 or None if the focus is already at the root
198 """
199 # Edge Case: Handle None tree
200 if self.tree is None:
201 return None
202
203 # Edge Case: Handle already at root (empty path)
204 if not self.path:
205 return None
206
207 new_path = self.path[:-1]
208 return Zipper(self.tree, new_path)
209
28210 def to_tree(self) -> dict:
29 pass
211 """
212 Get the complete tree from the zipper.
213
214 Returns:
215 dict: The complete binary tree
216 """
217 return self.tree
218
219 def _deep_copy_tree(self, tree):
220 """
221 Create a deep copy of a binary tree.
222
223 Args:
224 tree (dict): The tree to copy
225
226 Returns:
227 dict: A deep copy of the tree
228 """
229 if tree is None:
230 return None
231
232 return {
233 'value': tree['value'],
234 'left': self._deep_copy_tree(tree['left']),
235 'right': self._deep_copy_tree(tree['right'])
236 }
237
238 # 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.