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 and allows
6 navigation and modification operations while preserving immutability.
7 """
8
9 def __init__(self, tree, breadcrumbs=None):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree: The current subtree (focus node)
15 breadcrumbs: List of parent context information
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 represented as a dict with keys "value", "left", and "right"
27
628
29 Returns:
30 A new Zipper instance focused on the root of the tree
31 """
32 # Edge Case: Handle None input tree
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 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.
53
1254
55 Args:
56 value: The new value for the focus node
57
58
59 Returns:
60 A new Zipper with the updated value
61 """
62 # Edge Case: Handle None tree
63 if self.tree is None:
64 return self
65 new_tree = {
66 "value": value,
67 "left": self.tree["left"],
68 "right": self.tree["right"]
69 }
70 return Zipper(new_tree, self.breadcrumbs)
71
1372 def left(self) -> "Zipper | None":
14 pass
73 """
74 Move the focus to the left child of the current node.
75
1576
77 Returns:
78 A new Zipper focused on the left child, or None if no left child exists
79 """
80 # Edge Case: Handle None tree
81 if self.tree is None or self.tree["left"] is None:
82 return None
83
84 # Create breadcrumb to remember current node when going back up
85 breadcrumb = {
86 "parent": {
87 "value": self.tree["value"],
88 "right": self.tree["right"]
89 },
90 "side": "left"
91 }
92
93 return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb])
94
1695 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
96 """
97 Set the left child of the focus node.
98
99
100 Args:
101 tree: The new left subtree (or None to remove the left child)
102
18103
104 Returns:
105 A new Zipper with the updated left child
106 """
107 # Edge Case: Handle None tree
108 if self.tree is None:
109 return self
110 new_tree = {
111 "value": self.tree["value"],
112 "left": tree,
113 "right": self.tree["right"]
114 }
115 return Zipper(new_tree, self.breadcrumbs)
116
19117 def right(self) -> "Zipper | None":
20 pass
118 """
119 Move the focus to the right child of the current node.
120
21121
122 Returns:
123 A new Zipper focused on the right child, or None if no right child exists
124 """
125 # Edge Case: Handle None tree
126 if self.tree is None or self.tree["right"] is None:
127 return None
128
129 # Create breadcrumb to remember current node when going back up
130 breadcrumb = {
131 "parent": {
132 "value": self.tree["value"],
133 "left": self.tree["left"]
134 },
135 "side": "right"
136 }
137
138 return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb])
139
22140 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
141 """
142 Set the right child of the focus node.
143
24144
145 Args:
146 tree: The new right subtree (or None to remove the right child)
147
148
149 Returns:
150 A new Zipper with the updated right child
151 """
152 # Edge Case: Handle None tree
153 if self.tree is None:
154 return self
155 new_tree = {
156 "value": self.tree["value"],
157 "left": self.tree["left"],
158 "right": tree
159 }
160 return Zipper(new_tree, self.breadcrumbs)
161
25162 def up(self) -> "Zipper | None":
26 pass
163 """
164 Move the focus to the parent of the current node.
165
27166
167 Returns:
168 A new Zipper focused on the parent node, or None if already at the root
169 """
170 # Edge Case: Handle None breadcrumbs (already at root)
171 if not self.breadcrumbs:
172 return None
173
174 # Get the last breadcrumb (most recent parent)
175 last_breadcrumb = self.breadcrumbs[-1]
176 parent = last_breadcrumb["parent"]
177 side = last_breadcrumb["side"]
178
179 # Reconstruct the parent tree with current subtree in the correct position
180 if side == "left":
181 new_tree = {
182 "value": parent["value"],
183 "left": self.tree,
184 "right": parent["right"]
185 }
186 else: # side == "right"
187 new_tree = {
188 "value": parent["value"],
189 "left": parent["left"],
190 "right": self.tree
191 }
192
193 # Return new zipper with parent as focus and remaining breadcrumbs
194 return Zipper(new_tree, self.breadcrumbs[:-1])
195
28196 def to_tree(self) -> dict:
29 pass
197 """
198 Get the complete binary tree from the root.
199
200
201 Returns:
202 The complete binary tree as a dict
203 """
204 # Navigate back to the root to get the complete tree
205 current = self
206 while True:
207 parent = current.up()
208 if parent is None:
209 break
210 current = parent
211 return current.tree
212
213 # Handled Edge Cases: None input tree, None tree in zipper, no left child, 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.