JiaYou

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, allowing for
6 efficient navigation and modification operations.
7 """
8
9 def __init__(self, tree, path):
10 """
11 Initialize a Zipper instance.
12
13 Args:
14 tree (dict): The complete tree structure
15 path (list): Path from root to focus node, with context at each level
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 focus node
42 """
43 node = self.tree
44 for direction, parent, sibling in self.path:
45 node = node[direction]
46 return node
47
748 def value(self) -> int:
8 pass
9
49 """
50 Get the value of the focus node.
51
52 Returns:
53 int: The value of the focus node
54
55 Raises:
56 ValueError: If the focus node is None
57 """
58 focus = self._get_focus_node()
59 # Edge Case: Handle None focus node
60 if focus is None:
61 raise ValueError("Cannot get value of None node")
62 return focus["value"]
63
1064 def set_value(self, value: int) -> "Zipper":
11 pass
12
65 """
66 Set the value of the focus node.
67
68 Args:
69 value (int): The new value for the focus node
70
71 Returns:
72 Zipper: A new zipper with the updated value
73 """
74 # Edge Case: Handle None tree
75 if self.tree is None:
76 return Zipper(None, self.path)
77
78 # Create a deep copy of the tree
79 new_tree = self._deep_copy_tree(self.tree)
80
81 # Navigate to the focus node in the copy
82 node = new_tree
83 for direction, parent, sibling in self.path:
84 node = node[direction]
85
86 # Edge Case: Handle None focus node
87 if node is None:
88 return Zipper(new_tree, self.path)
89
90 node["value"] = value
91 return Zipper(new_tree, self.path)
92
1393 def left(self) -> "Zipper | None":
14 pass
15
94 """
95 Move the focus to the left child of the current node.
96
97 Returns:
98 Zipper | None: A new zipper focused on the left child, or None if no left child
99 """
100 focus = self._get_focus_node()
101 # Edge Case: Handle None focus node
102 if focus is None:
103 return None
104 # Edge Case: Handle focus node with no left child
105 if focus.get("left") is None:
106 return None
107
108 # Create a new path entry for moving to the left child
109 new_path = self.path + [("left", focus, focus.get("right"))]
110 return Zipper(self.tree, new_path)
111
16112 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
113 """
114 Set the left child of the focus node.
115
116 Args:
117 tree (dict | None): The new left subtree
118
119 Returns:
120 Zipper: A new zipper with the updated left child
121 """
122 # Edge Case: Handle None tree
123 if self.tree is None:
124 return Zipper(None, self.path)
125
126 # Create a deep copy of the tree
127 new_tree = self._deep_copy_tree(self.tree)
128
129 # Navigate to the focus node in the copy
130 node = new_tree
131 for direction, parent, sibling in self.path:
132 node = node[direction]
133
134 # Edge Case: Handle None focus node
135 if node is None:
136 return Zipper(new_tree, self.path)
137
138 node["left"] = tree
139 return Zipper(new_tree, self.path)
140
19141 def right(self) -> "Zipper | None":
20 pass
21
142 """
143 Move the focus to the right child of the current node.
144
145 Returns:
146 Zipper | None: A new zipper focused on the right child, or None if no right child
147 """
148 focus = self._get_focus_node()
149 # Edge Case: Handle None focus node
150 if focus is None:
151 return None
152 # Edge Case: Handle focus node with no right child
153 if focus.get("right") is None:
154 return None
155
156 # Create a new path entry for moving to the right child
157 new_path = self.path + [("right", focus, focus.get("left"))]
158 return Zipper(self.tree, new_path)
159
22160 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
161 """
162 Set the right child of the focus node.
163
164 Args:
165 tree (dict | None): The new right subtree
166
167 Returns:
168 Zipper: A new zipper with the updated right child
169 """
170 # Edge Case: Handle None tree
171 if self.tree is None:
172 return Zipper(None, self.path)
173
174 # Create a deep copy of the tree
175 new_tree = self._deep_copy_tree(self.tree)
176
177 # Navigate to the focus node in the copy
178 node = new_tree
179 for direction, parent, sibling in self.path:
180 node = node[direction]
181
182 # Edge Case: Handle None focus node
183 if node is None:
184 return Zipper(new_tree, self.path)
185
186 node["right"] = tree
187 return Zipper(new_tree, self.path)
188
25189 def up(self) -> "Zipper | None":
26 pass
27
190 """
191 Move the focus to the parent of the current node.
192
193 Returns:
194 Zipper | None: A new zipper focused on the parent, or None if at root
195 """
196 # Edge Case: Handle already at root (empty path)
197 if not self.path:
198 return None
199
200 # Remove the last entry from the path to move up
201 new_path = self.path[:-1]
202 return Zipper(self.tree, new_path)
203
28204 def to_tree(self) -> dict:
29 pass
205 """
206 Get the complete tree from the zipper.
207
208 Returns:
209 dict: The complete tree
210 """
211 return self.tree
212
213 def _deep_copy_tree(self, tree):
214 """
215 Create a deep copy of a tree.
216
217 Args:
218 tree (dict): The tree to copy
219
220 Returns:
221 dict: A deep copy of the tree
222 """
223 if tree is None:
224 return None
225
226 return {
227 "value": tree["value"],
228 "left": self._deep_copy_tree(tree.get("left")),
229 "right": self._deep_copy_tree(tree.get("right"))
230 }
231
232 # Handled Edge Cases: None tree input, None focus node, focus node with no left child, focus node with no 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.