rockstar

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 A zipper contains a data structure and a pointer (focus) into that structure.
6 It supports moving through the tree and modifying it in a functional way.
7 """
8
9 def __init__(self, tree, breadcrumbs=None):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree: The current subtree (focus) as a dict with keys 'value', 'left', 'right'
15 breadcrumbs: A list of breadcrumb dicts tracking the path to the focus
16 """
17 self.tree = tree
18 self.breadcrumbs = breadcrumbs or []
19
320 @staticmethod
421 def from_tree(tree: dict) -> "Zipper":
5 pass
22 """
23 Create a zipper from a binary tree.
24
25 Args:
26 tree: A binary tree as a dict with keys 'value', 'left', 'right'
27
628
29 Returns:
30 A new Zipper instance focused on the root of the tree
31 """
32 # Edge Case: Handle None tree input
33 if tree is None:
34 return None
35 return Zipper(tree)
36
737 def value(self) -> int:
8 pass
38 """
39 Get the value of the focus node.
40
941
42 Returns:
43 The value of the current focus node
44 """
45 # Edge Case: Handle None tree
46 if self.tree is None:
47 return None
48 return self.tree["value"]
49
1050 def set_value(self, value: int) -> "Zipper":
11 pass
51 """
52 Set the value of the focus node, returning a new zipper.
53
54 Args:
55 value: The new value for the focus node
56
1257
58 Returns:
59 A new Zipper with the updated value
60 """
61 # Edge Case: Handle None tree
62 if self.tree is None:
63 return self
64 new_tree = {
65 "value": value,
66 "left": self.tree["left"],
67 "right": self.tree["right"]
68 }
69 return Zipper(new_tree, self.breadcrumbs)
70
1371 def left(self) -> "Zipper | None":
14 pass
72 """
73 Move the focus to the left child of the current node.
74
1575
76 Returns:
77 A new Zipper focused on the left child, or None if no left child exists
78 """
79 # Edge Case: Handle None tree
80 if self.tree is None or self.tree["left"] is None:
81 return None
82
83 # Create a breadcrumb to remember how to reconstruct the parent
84 breadcrumb = {
85 "parent": {
86 "value": self.tree["value"],
87 "right": self.tree["right"]
88 },
89 "is_left": True
90 }
91
92 return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb])
93
1694 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
95 """
96 Set the left child of the focus node, returning a new zipper.
97
98 Args:
99 tree: The new left subtree (or None)
100
18101
102 Returns:
103 A new Zipper with the updated left child
104 """
105 # Edge Case: Handle None tree
106 if self.tree is None:
107 return self
108 new_tree = {
109 "value": self.tree["value"],
110 "left": tree,
111 "right": self.tree["right"]
112 }
113 return Zipper(new_tree, self.breadcrumbs)
114
19115 def right(self) -> "Zipper | None":
20 pass
116 """
117 Move the focus to the right child of the current node.
118
21119
120 Returns:
121 A new Zipper focused on the right child, or None if no right child exists
122 """
123 # Edge Case: Handle None tree
124 if self.tree is None or self.tree["right"] is None:
125 return None
126
127 # Create a breadcrumb to remember how to reconstruct the parent
128 breadcrumb = {
129 "parent": {
130 "value": self.tree["value"],
131 "left": self.tree["left"]
132 },
133 "is_left": False
134 }
135
136 return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb])
137
22138 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
139 """
140 Set the right child of the focus node, returning a new zipper.
141
142 Args:
143 tree: The new right subtree (or None)
144
24145
146 Returns:
147 A new Zipper with the updated right child
148 """
149 # Edge Case: Handle None tree
150 if self.tree is None:
151 return self
152 new_tree = {
153 "value": self.tree["value"],
154 "left": self.tree["left"],
155 "right": tree
156 }
157 return Zipper(new_tree, self.breadcrumbs)
158
25159 def up(self) -> "Zipper | None":
26 pass
160 """
161 Move the focus to the parent of the current node.
162
27163
164 Returns:
165 A new Zipper focused on the parent, or None if already at the root
166 """
167 # Edge Case: Handle when already at root (no breadcrumbs)
168 if not self.breadcrumbs:
169 return None
170
171 # Get the last breadcrumb
172 last_breadcrumb = self.breadcrumbs[-1]
173
174 # Reconstruct the parent tree
175 parent = last_breadcrumb["parent"]
176
177 if last_breadcrumb["is_left"]:
178 # We were in the left subtree
179 reconstructed_tree = {
180 "value": parent["value"],
181 "left": self.tree,
182 "right": parent["right"]
183 }
184 else:
185 # We were in the right subtree
186 reconstructed_tree = {
187 "value": parent["value"],
188 "left": parent["left"],
189 "right": self.tree
190 }
191
192 return Zipper(reconstructed_tree, self.breadcrumbs[:-1])
193
28194 def to_tree(self) -> dict:
29 pass
195 """
196 Get the complete binary tree from the zipper.
197
198
199 Returns:
200 The complete binary tree as a dict
201 """
202 # Navigate back to the root to get the complete tree
203 current = self
204 while current.breadcrumbs:
205 current = current.up()
206 return current.tree
207
208 # Handled Edge Cases: None tree input, None left/right child, Already at root
209 # Handled Edge Cases: None tree, None left/right child
210 # Handled Edge Cases: None tree, None left/right child
211 # Handled Edge Cases: None tree
212 # Handled Edge Cases: None tree
213 # Handled Edge Cases: None tree, None left/right child
214 # Handled Edge Cases: None tree
215 # Handled Edge Cases: Already at root (no breadcrumbs)
216 # Handled Edge Cases: None tree input
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.