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 navigating and manipulating a binary tree.
4
5 The zipper maintains a focus on a specific node in the tree and allows
6 traversal 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 full tree structure
15 path (list): Path from root to current focus node, each element is a tuple (parent_node, direction)
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 with focus on the root node.
24
25 Args:
26 tree (dict): A binary tree represented as nested dictionaries with keys 'value', 'left', 'right'
27
28 Returns:
29 Zipper: A new zipper instance 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 parent, direction 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 Raises:
59 Exception: If the focus node is None
60 """
61 # Edge Case: Handle when focus node is None
62 focus = self._get_focus_node()
63 if focus is None:
64 raise Exception("Focus node is None")
65 return focus['value']
66
1067 def set_value(self, value: int) -> "Zipper":
11 pass
12
68 """
69 Set the value of the focus node, returning 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 updated focus node value
76 """
77 # Edge Case: Handle when focus node is None
78 if not self._path and self._tree is None:
79 new_tree = {'value': value, 'left': None, 'right': None}
80 return Zipper(new_tree, [])
81
82 # Create a deep copy of the tree
83 new_tree = self._deep_copy_tree(self._tree)
84
85 # Navigate to the focus node in the copied tree and update its value
86 node = new_tree
87 for parent, direction in self._path:
88 if direction == 'left':
89 node = node['left']
90 else: # direction == 'right'
91 node = node['right']
92 node['value'] = value
93
94 return Zipper(new_tree, self._path[:])
95
1396 def left(self) -> "Zipper | None":
14 pass
15
97 """
98 Move the focus to the left child of the current focus node.
99
100 Returns:
101 Zipper | None: A new zipper focused on the left child, or None if no left child exists
102 """
103 focus = self._get_focus_node()
104 # Edge Case: Handle when focus node is None
105 if focus is None:
106 return None
107 # Edge Case: Handle when left child is None
108 if focus['left'] is None:
109 return None
110
111 new_path = self._path + [(focus, 'left')]
112 return Zipper(self._tree, new_path)
113
16114 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
115 """
116 Set the left child of the focus node, returning a new zipper.
117
118 Args:
119 tree (dict | None): The new left subtree
120
121 Returns:
122 Zipper: A new zipper with updated left child
123 """
124 # Edge Case: Handle when focus node is None
125 if not self._path and self._tree is None:
126 # If we're at the root and it's None, create a new tree
127 new_tree = {'value': 0, 'left': tree, 'right': None} # Default value of 0
128 return Zipper(new_tree, [])
129
130 # Create a deep copy of the tree
131 new_tree = self._deep_copy_tree(self._tree)
132
133 # Navigate to the focus node in the copied tree and update its left child
134 node = new_tree
135 for parent, direction in self._path:
136 if direction == 'left':
137 node = node['left']
138 else: # direction == 'right'
139 node = node['right']
140 node['left'] = tree
141
142 return Zipper(new_tree, self._path[:])
143
19144 def right(self) -> "Zipper | None":
20 pass
21
145 """
146 Move the focus to the right child of the current focus node.
147
148 Returns:
149 Zipper | None: A new zipper focused on the right child, or None if no right child exists
150 """
151 focus = self._get_focus_node()
152 # Edge Case: Handle when focus node is None
153 if focus is None:
154 return None
155 # Edge Case: Handle when right child is None
156 if focus['right'] is None:
157 return None
158
159 new_path = self._path + [(focus, 'right')]
160 return Zipper(self._tree, new_path)
161
22162 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
163 """
164 Set the right child of the focus node, returning a new zipper.
165
166 Args:
167 tree (dict | None): The new right subtree
168
169 Returns:
170 Zipper: A new zipper with updated right child
171 """
172 # Edge Case: Handle when focus node is None
173 if not self._path and self._tree is None:
174 # If we're at the root and it's None, create a new tree
175 new_tree = {'value': 0, 'left': None, 'right': tree} # Default value of 0
176 return Zipper(new_tree, [])
177
178 # Create a deep copy of the tree
179 new_tree = self._deep_copy_tree(self._tree)
180
181 # Navigate to the focus node in the copied tree and update its right child
182 node = new_tree
183 for parent, direction in self._path:
184 if direction == 'left':
185 node = node['left']
186 else: # direction == 'right'
187 node = node['right']
188 node['right'] = tree
189
190 return Zipper(new_tree, self._path[:])
191
25192 def up(self) -> "Zipper | None":
26 pass
27
193 """
194 Move the focus to the parent of the current focus node.
195
196 Returns:
197 Zipper | None: A new zipper focused on the parent, or None if already at root
198 """
199 # Edge Case: Handle when already at root
200 if not self._path:
201 return None
202
203 new_path = self._path[:-1]
204 return Zipper(self._tree, new_path)
205
28206 def to_tree(self) -> dict:
29 pass
207 """
208 Get the complete tree from the zipper.
209
210 Returns:
211 dict: The complete binary tree
212 """
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
228 return {
229 'value': tree['value'],
230 'left': self._deep_copy_tree(tree['left']),
231 'right': self._deep_copy_tree(tree['right'])
232 }
233 # Handled Edge Cases: None tree input, focus node is None, left child is None, right child is None, 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.