corpus

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 def __init__(self, tree, path):
9 """
10 Initialize a Zipper.
11
12 Args:
13 tree: The complete tree structure
14 path: A list of tuples representing the path from root to focus node.
15 Each tuple contains (parent_node, side) where side is 'left' or 'right'
16 """
17 self.tree = tree
18 self.path = path
19
20 # Calculate the focus node based on the path
21 self.focus = tree
22 for parent, side in self.path:
23 if self.focus is None:
24 break
25 self.focus = self.focus.get(side)
26
327 @staticmethod
428 def from_tree(tree: dict) -> "Zipper":
5 pass
6
29 """
30 Create a zipper from a binary tree.
31
32 Args:
33 tree: A binary tree represented as a dict with keys "value", "left", and "right"
34
35 Returns:
36 A new Zipper instance with focus on the root node
37 """
38 # Edge Case: Handle None tree input
39 if tree is None:
40 return Zipper(None, [])
41 return Zipper(tree, [])
42
743 def value(self) -> int:
8 pass
9
44 """
45 Get the value of the focus node.
46
47 Returns:
48 The value of the focus node
49
50 Raises:
51 Exception: If the focus is None (empty tree)
52 """
53 # Edge Case: Handle when focus is None
54 if self.focus is None:
55 raise Exception("Cannot get value of None focus")
56 return self.focus["value"]
57
1058 def set_value(self, value: int) -> "Zipper":
11 pass
12
59 """
60 Set the value of the focus node.
61
62 Args:
63 value: The new value for the focus node
64
65 Returns:
66 A new Zipper instance with the updated value
67 """
68 # Edge Case: Handle when focus is None
69 if self.focus is None:
70 raise Exception("Cannot set value of None focus")
71
72 # Create a new tree with the updated value
73 new_tree = self._copy_tree(self.tree)
74
75 # Navigate to the focus node in the new tree
76 current = new_tree
77 for parent, side in self.path:
78 current = current[side]
79
80 # Update the value
81 current["value"] = value
82
83 return Zipper(new_tree, self.path)
84
1385 def left(self) -> "Zipper | None":
14 pass
15
86 """
87 Move the focus to the left child of the current focus node.
88
89 Returns:
90 A new Zipper instance focused on the left child, or None if no left child exists
91 """
92 # Edge Case: Handle when focus is None
93 if self.focus is None or self.focus.get("left") is None:
94 return None
95
96 # Create a new path with the current focus added
97 new_path = self.path + [(self.focus, "left")]
98 return Zipper(self.tree, new_path)
99
16100 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
101 """
102 Set the left child of the focus node.
103
104 Args:
105 tree: The new left subtree (or None to remove the left child)
106
107 Returns:
108 A new Zipper instance with the updated left child
109 """
110 # Edge Case: Handle when focus is None
111 if self.focus is None:
112 raise Exception("Cannot set left of None focus")
113
114 # Create a new tree with the updated left child
115 new_tree = self._copy_tree(self.tree)
116
117 # Navigate to the focus node in the new tree
118 current = new_tree
119 for parent, side in self.path:
120 current = current[side]
121
122 # Update the left child
123 current["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 focus node.
130
131 Returns:
132 A new Zipper instance focused on the right child, or None if no right child exists
133 """
134 # Edge Case: Handle when focus is None
135 if self.focus is None or self.focus.get("right") is None:
136 return None
137
138 # Create a new path with the current focus added
139 new_path = self.path + [(self.focus, "right")]
140 return Zipper(self.tree, new_path)
141
22142 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
143 """
144 Set the right child of the focus node.
145
146 Args:
147 tree: The new right subtree (or None to remove the right child)
148
149 Returns:
150 A new Zipper instance with the updated right child
151 """
152 # Edge Case: Handle when focus is None
153 if self.focus is None:
154 raise Exception("Cannot set right of None focus")
155
156 # Create a new tree with the updated right child
157 new_tree = self._copy_tree(self.tree)
158
159 # Navigate to the focus node in the new tree
160 current = new_tree
161 for parent, side in self.path:
162 current = current[side]
163
164 # Update the right child
165 current["right"] = tree
166
167 return Zipper(new_tree, self.path)
168
25169 def up(self) -> "Zipper | None":
26 pass
27
170 """
171 Move the focus to the parent of the current focus node.
172
173 Returns:
174 A new Zipper instance focused on the parent, or None if at the root
175 """
176 # Edge Case: Handle when at root (no parent)
177 if len(self.path) == 0:
178 return None
179
180 # Create a new path without the last element
181 new_path = self.path[:-1]
182 return Zipper(self.tree, new_path)
183
28184 def to_tree(self) -> dict:
29 pass
185 """
186 Get the complete tree from the zipper.
187
188 Returns:
189 The complete binary tree
190 """
191 return self.tree
192
193 def _copy_tree(self, tree):
194 """
195 Create a deep copy of a binary tree.
196
197 Args:
198 tree: The tree to copy
199
200 Returns:
201 A deep copy of the tree
202 """
203 if tree is None:
204 return None
205
206 return {
207 "value": tree["value"],
208 "left": self._copy_tree(tree.get("left")),
209 "right": self._copy_tree(tree.get("right"))
210 }
211
212 # Handled Edge Cases: None tree input, None focus, setting/getting value of None focus,
213 # moving left/right when no child exists, moving up when 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.