perfect

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
6 navigation and modifications while preserving the tree's structure.
7 """
8
9 def __init__(self, tree: dict, path: list = None):
10 """
11 Initialize a Zipper instance.
12
13 Args:
14 tree: The binary tree represented as a dict with keys 'value', 'left', and 'right'.
15 path: A list of tuples representing the path from root to focus node.
16 Each tuple contains (parent_node, direction) where direction is 'left' or 'right'.
17 """
18 self.tree = tree
19 self.path = path or []
20
321 @staticmethod
422 def from_tree(tree: dict) -> "Zipper":
5 pass
6
23 """
24 Create a zipper from a binary tree.
25
26 Args:
27 tree: The binary tree represented as a dict with keys 'value', 'left', and 'right'.
28
29 Returns:
30 A new Zipper instance focused on the root of the tree.
31 """
32 # Edge Case: Handle None tree
33 if tree is None:
34 return None
35 return Zipper(tree)
36
37 def _get_focus_node(self) -> dict:
38 """
39 Get the current focus node based on the path.
40
41 Returns:
42 The focus node as a dict.
43 """
44 node = self.tree
45 for parent, direction in self.path:
46 node = node[direction]
47 return node
48
749 def value(self) -> int:
8 pass
9
50 """
51 Get the value of the focus node.
52
53 Returns:
54 The value of the focus node.
55 """
56 return self._get_focus_node()["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 empty path (root node)
69 if not self.path:
70 new_tree = {"value": value, "left": self.tree["left"], "right": self.tree["right"]}
71 return Zipper(new_tree, self.path)
72
73 # Create a deep copy of the tree
74 new_tree = self._deep_copy_tree(self.tree)
75
76 # Navigate to the focus node and update its value
77 node = new_tree
78 for parent, direction in self.path:
79 node = node[direction]
80 node["value"] = value
81
82 return Zipper(new_tree, self.path)
83
1384 def left(self) -> "Zipper | None":
14 pass
15
85 """
86 Move the focus to the left child of the current focus node.
87
88 Returns:
89 A new Zipper instance focused on the left child, or None if no left child exists.
90 """
91 focus_node = self._get_focus_node()
92 # Edge Case: Handle missing left child
93 if focus_node["left"] is None:
94 return None
95
96 new_path = self.path + [(focus_node, "left")]
97 return Zipper(self.tree, new_path)
98
1699 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
100 """
101 Set the left child of the focus node.
102
103 Args:
104 tree: The new left subtree (or None to remove the left child).
105
106 Returns:
107 A new Zipper instance with the updated left child.
108 """
109 # Edge Case: Handle empty path (root node)
110 if not self.path:
111 new_tree = {"value": self.tree["value"], "left": tree, "right": self.tree["right"]}
112 return Zipper(new_tree, self.path)
113
114 # Create a deep copy of the tree
115 new_tree = self._deep_copy_tree(self.tree)
116
117 # Navigate to the focus node and update its left child
118 node = new_tree
119 for parent, direction in self.path:
120 node = node[direction]
121 node["left"] = tree
122
123 return Zipper(new_tree, self.path)
124
19125 def right(self) -> "Zipper | None":
20 pass
21
126 """
127 Move the focus to the right child of the current focus node.
128
129 Returns:
130 A new Zipper instance focused on the right child, or None if no right child exists.
131 """
132 focus_node = self._get_focus_node()
133 # Edge Case: Handle missing right child
134 if focus_node["right"] is None:
135 return None
136
137 new_path = self.path + [(focus_node, "right")]
138 return Zipper(self.tree, new_path)
139
22140 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
141 """
142 Set the right child of the focus node.
143
144 Args:
145 tree: The new right subtree (or None to remove the right child).
146
147 Returns:
148 A new Zipper instance with the updated right child.
149 """
150 # Edge Case: Handle empty path (root node)
151 if not self.path:
152 new_tree = {"value": self.tree["value"], "left": self.tree["left"], "right": tree}
153 return Zipper(new_tree, self.path)
154
155 # Create a deep copy of the tree
156 new_tree = self._deep_copy_tree(self.tree)
157
158 # Navigate to the focus node and update its right child
159 node = new_tree
160 for parent, direction in self.path:
161 node = node[direction]
162 node["right"] = tree
163
164 return Zipper(new_tree, self.path)
165
25166 def up(self) -> "Zipper | None":
26 pass
27
167 """
168 Move the focus to the parent of the current focus node.
169
170 Returns:
171 A new Zipper instance focused on the parent, or None if the focus is already at the root.
172 """
173 # Edge Case: Handle already at root
174 if not self.path:
175 return None
176
177 new_path = self.path[:-1]
178 return Zipper(self.tree, new_path)
179
28180 def to_tree(self) -> dict:
29 pass
181 """
182 Get the complete binary tree from the zipper.
183
184 Returns:
185 The binary tree as a dict.
186 """
187 return self.tree
188
189 def _deep_copy_tree(self, tree: dict) -> dict:
190 """
191 Create a deep copy of a binary tree.
192
193 Args:
194 tree: The tree to copy.
195
196 Returns:
197 A deep copy of the tree.
198 """
199 if tree is None:
200 return None
201 return {
202 "value": tree["value"],
203 "left": self._deep_copy_tree(tree["left"]),
204 "right": self._deep_copy_tree(tree["right"])
205 }
206
207 # Handled Edge Cases: None tree in from_tree, missing children in left/right,
208 # empty path in set_value/set_left/set_right, already at root in up
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.