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, with context
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: Empty tree
32 if tree is None:
33 return None
34 return Zipper(tree, [])
35
736 def value(self) -> int:
8 pass
9
37 """
38 Get the value of the focus node.
39
40 Returns:
41 int: The value of the current focus node
42 """
43 # Get the current node based on path
44 node = self.tree
45 for direction, parent, sibling in self.path:
46 if direction == 'left':
47 node = node['left']
48 else: # direction == 'right'
49 node = node['right']
50 return node['value']
51
1052 def set_value(self, value: int) -> "Zipper":
11 pass
12
53 """
54 Set the value of the focus node.
55
56 Args:
57 value (int): The new value for the focus node
58
59 Returns:
60 Zipper: A new zipper with the updated value
61 """
62 # Edge Case: Invalid value type (though not explicitly required by problem)
63 if not isinstance(value, int):
64 raise TypeError("Value must be an integer")
65
66 # Create a deep copy of the tree
67 new_tree = self._deep_copy_tree(self.tree)
68
69 # Navigate to the node and update its value
70 node = new_tree
71 for direction, parent, sibling in self.path:
72 if direction == 'left':
73 node = node['left']
74 else: # direction == 'right'
75 node = node['right']
76 node['value'] = value
77
78 return Zipper(new_tree, self.path[:])
79
1380 def left(self) -> "Zipper | None":
14 pass
15
81 """
82 Move the focus to the left child of the current node.
83
84 Returns:
85 Zipper | None: A new zipper focused on the left child, or None if no left child
86 """
87 # Get the current node
88 node = self.tree
89 for direction, parent, sibling in self.path:
90 if direction == 'left':
91 node = node['left']
92 else: # direction == 'right'
93 node = node['right']
94
95 # Edge Case: No left child
96 if node['left'] is None:
97 return None
98
99 # Create a new path entry for moving left
100 new_path = self.path + [('left', node, node['right'])]
101 return Zipper(self.tree, new_path)
102
16103 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
104 """
105 Set the left child of the focus node.
106
107 Args:
108 tree (dict | None): The new left subtree
109
110 Returns:
111 Zipper: A new zipper with the updated left child
112 """
113 # Create a deep copy of the tree
114 new_tree = self._deep_copy_tree(self.tree)
115
116 # Navigate to the node and update its left child
117 node = new_tree
118 for direction, parent, sibling in self.path:
119 if direction == 'left':
120 node = node['left']
121 else: # direction == 'right'
122 node = node['right']
123 node['left'] = tree
124
125 return Zipper(new_tree, self.path[:])
126
19127 def right(self) -> "Zipper | None":
20 pass
21
128 """
129 Move the focus to the right child of the current node.
130
131 Returns:
132 Zipper | None: A new zipper focused on the right child, or None if no right child
133 """
134 # Get the current node
135 node = self.tree
136 for direction, parent, sibling in self.path:
137 if direction == 'left':
138 node = node['left']
139 else: # direction == 'right'
140 node = node['right']
141
142 # Edge Case: No right child
143 if node['right'] is None:
144 return None
145
146 # Create a new path entry for moving right
147 new_path = self.path + [('right', node, node['left'])]
148 return Zipper(self.tree, new_path)
149
22150 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
151 """
152 Set the right child of the focus node.
153
154 Args:
155 tree (dict | None): The new right subtree
156
157 Returns:
158 Zipper: A new zipper with the updated right child
159 """
160 # Create a deep copy of the tree
161 new_tree = self._deep_copy_tree(self.tree)
162
163 # Navigate to the node and update its right child
164 node = new_tree
165 for direction, parent, sibling in self.path:
166 if direction == 'left':
167 node = node['left']
168 else: # direction == 'right'
169 node = node['right']
170 node['right'] = tree
171
172 return Zipper(new_tree, self.path[:])
173
25174 def up(self) -> "Zipper | None":
26 pass
27
175 """
176 Move the focus to the parent of the current node.
177
178 Returns:
179 Zipper | None: A new zipper focused on the parent, or None if at root
180 """
181 # Edge Case: Already at root (no parent)
182 if not self.path:
183 return None
184
185 # Create a new path without the last element
186 new_path = self.path[:-1]
187 return Zipper(self.tree, new_path)
188
28189 def to_tree(self) -> dict:
29 pass
190 """
191 Get the complete tree from the zipper.
192
193 Returns:
194 dict: The complete binary tree
195 """
196 return self.tree
197
198 def _deep_copy_tree(self, tree):
199 """
200 Create a deep copy of a binary tree.
201
202 Args:
203 tree (dict): The tree to copy
204
205 Returns:
206 dict: A deep copy of the tree
207 """
208 if tree is None:
209 return None
210
211 new_tree = {
212 'value': tree['value'],
213 'left': self._deep_copy_tree(tree['left']),
214 'right': self._deep_copy_tree(tree['right'])
215 }
216 return new_tree
217
218 # Handled Edge Cases: Empty tree in from_tree, None left child in left, None right child in right,
219 # No parent in up, Invalid value type in set_value
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.