Konoha

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 root tree structure
15 path (list): Path from root to current focus node, containing breadcrumb information
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 current focus node
42 """
43 node = self.tree
44 for direction, parent, sibling in self.path:
45 if direction == 'left':
46 node = parent['left']
47 else: # direction == 'right'
48 node = parent['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 return self._get_focus_node()['value']
59
1060 def set_value(self, value: int) -> "Zipper":
11 pass
12
61 """
62 Set the value of the focus node.
63
64 Args:
65 value (int): The new value for the focus node
66
67 Returns:
68 Zipper: A new zipper with the updated value
69 """
70 # Edge Case: Handle None value
71 if value is None:
72 raise ValueError("Value cannot be None")
73
74 new_tree = self._copy_tree(self.tree)
75 new_path = list(self.path)
76
77 # Navigate to the focus node in the copied tree
78 node = new_tree
79 for i, (direction, parent, sibling) in enumerate(new_path):
80 if direction == 'left':
81 node = node['left']
82 # Update the parent reference in the path
83 new_path[i] = (direction, self._get_parent(new_tree, new_path[:i]), sibling)
84 else: # direction == 'right'
85 node = node['right']
86 # Update the parent reference in the path
87 new_path[i] = (direction, self._get_parent(new_tree, new_path[:i]), sibling)
88
89 # Update the value
90 node['value'] = value
91
92 return Zipper(new_tree, new_path)
93
1394 def left(self) -> "Zipper | None":
14 pass
15
95 """
96 Move the focus to the left child of the current node.
97
98 Returns:
99 Zipper | None: A new zipper focused on the left child, or None if no left child exists
100 """
101 focus = self._get_focus_node()
102 # Edge Case: Handle missing left child
103 if focus['left'] is None:
104 return None
105
106 new_path = list(self.path)
107 new_path.append(('left', focus, focus['right']))
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 subtree of the focus node.
113
114 Args:
115 tree (dict | None): The new left subtree
116
117 Returns:
118 Zipper: A new zipper with the updated left subtree
119 """
120 new_tree = self._copy_tree(self.tree)
121 new_path = list(self.path)
122
123 # Navigate to the focus node in the copied tree
124 node = new_tree
125 for i, (direction, parent, sibling) in enumerate(new_path):
126 if direction == 'left':
127 node = node['left']
128 # Update the parent reference in the path
129 new_path[i] = (direction, self._get_parent(new_tree, new_path[:i]), sibling)
130 else: # direction == 'right'
131 node = node['right']
132 # Update the parent reference in the path
133 new_path[i] = (direction, self._get_parent(new_tree, new_path[:i]), sibling)
134
135 # Update the left subtree
136 node['left'] = tree
137
138 return Zipper(new_tree, new_path)
139
19140 def right(self) -> "Zipper | None":
20 pass
21
141 """
142 Move the focus to the right child of the current node.
143
144 Returns:
145 Zipper | None: A new zipper focused on the right child, or None if no right child exists
146 """
147 focus = self._get_focus_node()
148 # Edge Case: Handle missing right child
149 if focus['right'] is None:
150 return None
151
152 new_path = list(self.path)
153 new_path.append(('right', focus, focus['left']))
154 return Zipper(self.tree, new_path)
155
22156 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
157 """
158 Set the right subtree of the focus node.
159
160 Args:
161 tree (dict | None): The new right subtree
162
163 Returns:
164 Zipper: A new zipper with the updated right subtree
165 """
166 new_tree = self._copy_tree(self.tree)
167 new_path = list(self.path)
168
169 # Navigate to the focus node in the copied tree
170 node = new_tree
171 for i, (direction, parent, sibling) in enumerate(new_path):
172 if direction == 'left':
173 node = node['left']
174 # Update the parent reference in the path
175 new_path[i] = (direction, self._get_parent(new_tree, new_path[:i]), sibling)
176 else: # direction == 'right'
177 node = node['right']
178 # Update the parent reference in the path
179 new_path[i] = (direction, self._get_parent(new_tree, new_path[:i]), sibling)
180
181 # Update the right subtree
182 node['right'] = tree
183
184 return Zipper(new_tree, new_path)
185
25186 def up(self) -> "Zipper | None":
26 pass
27
187 """
188 Move the focus to the parent of the current node.
189
190 Returns:
191 Zipper | None: A new zipper focused on the parent, or None if already at root
192 """
193 # Edge Case: Handle attempt to move up from root
194 if not self.path:
195 return None
196
197 new_path = list(self.path)
198 new_path.pop()
199 return Zipper(self.tree, new_path)
200
28201 def to_tree(self) -> dict:
29 pass
202 """
203 Get the complete tree from the zipper.
204
205 Returns:
206 dict: The complete binary tree
207 """
208 return self.tree
209
210 def _copy_tree(self, tree):
211 """
212 Create a deep copy of a tree.
213
214 Args:
215 tree (dict): The tree to copy
216
217 Returns:
218 dict: A deep copy of the tree
219 """
220 if tree is None:
221 return None
222 return {
223 'value': tree['value'],
224 'left': self._copy_tree(tree['left']),
225 'right': self._copy_tree(tree['right'])
226 }
227
228 def _get_parent(self, tree, path):
229 """
230 Get the parent node based on a path.
231
232 Args:
233 tree (dict): The root tree
234 path (list): Path to the parent
235
236 Returns:
237 dict: The parent node
238 """
239 node = tree
240 for direction, parent, sibling in path:
241 if direction == 'left':
242 node = node['left']
243 else: # direction == 'right'
244 node = node['right']
245 return node
246
247# Handled Edge Cases: None tree input, missing left child, missing right child, attempt to move up from root, None value input
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.