yahoo

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 traversal and modification operations.
7 """
8
9 def __init__(self, tree, breadcrumbs=None):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree: The current subtree (focus)
15 breadcrumbs: Path information to navigate back up the tree
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 current node
44 """
45 return self.tree["value"]
46
1047 def set_value(self, value: int) -> "Zipper":
11 pass
48 """
49 Set the value of the focus node.
50
1251
52 Args:
53 value: The new value for the focus node
54
55
56 Returns:
57 A new Zipper with the updated value
58 """
59 # Edge Case: Handle None value
60 if value is None:
61 raise ValueError("Value cannot be None")
62
63
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
75
76 Returns:
77 A new Zipper focused on the left child, or None if no left child exists
78 """
79 # Edge Case: Handle missing left child
80 if self.tree["left"] is None:
81 return None
82
83
84 # Create breadcrumb to remember how to get back to this node
85 breadcrumb = {
86 "parent": {
87 "value": self.tree["value"],
88 "right": self.tree["right"]
89 },
90 "is_left": True
91 }
92
1593
94 return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb])
95
1696 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
97 """
98 Set the left child of the focus node.
99
18100
101 Args:
102 tree: The new left subtree (or None to remove the left child)
103
104
105 Returns:
106 A new Zipper with the updated left child
107 """
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
119
120 Returns:
121 A new Zipper focused on the right child, or None if no right child exists
122 """
123 # Edge Case: Handle missing right child
124 if self.tree["right"] is None:
125 return None
126
127
128 # Create breadcrumb to remember how to get back to this node
129 breadcrumb = {
130 "parent": {
131 "value": self.tree["value"],
132 "left": self.tree["left"]
133 },
134 "is_left": False
135 }
136
21137
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 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
163
164 Returns:
165 A new Zipper focused on the parent node, or None if already at the root
166 """
167 # Edge Case: Handle attempt to move up from root
168 if not self.breadcrumbs:
169 return None
170
171
172 # Get the last breadcrumb (most recent parent information)
173 last_breadcrumb = self.breadcrumbs[-1]
174 rest_breadcrumbs = self.breadcrumbs[:-1]
175
176
177 parent_info = last_breadcrumb["parent"]
178
27179
180 if last_breadcrumb["is_left"]:
181 # We were at the left child, so reconstruct parent with current tree as left child
182 new_tree = {
183 "value": parent_info["value"],
184 "left": self.tree,
185 "right": parent_info["right"]
186 }
187 else:
188 # We were at the right child, so reconstruct parent with current tree as right child
189 new_tree = {
190 "value": parent_info["value"],
191 "left": parent_info["left"],
192 "right": self.tree
193 }
194
195
196 return Zipper(new_tree, rest_breadcrumbs)
197
28198 def to_tree(self) -> dict:
29 pass
199 """
200 Get the complete binary tree from the zipper.
201
202
203 Returns:
204 The complete binary tree as a dict
205 """
206 # Navigate back to the root to get the complete tree
207 current = self
208 while True:
209 parent = current.up()
210 if parent is None:
211 break
212 current = parent
213 return current.tree
214
215 # Handled Edge Cases: None input tree, missing left child, missing right child, attempt to move up from root, None value
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.