\besttop

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 the tree structure.
7 """
8
9 def __init__(self, tree, breadcrumbs=None):
10 """
11 Initialize a Zipper with a tree and breadcrumbs.
12
13 Args:
14 tree (dict): The current subtree with keys 'value', 'left', 'right'
15 breadcrumbs (list): 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 (dict): A binary tree with keys 'value', 'left', 'right'
27
628
29 Returns:
30 Zipper: A new zipper 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 int: The value of the current 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 (int): The new value for the focus node
57
58
59 Returns:
60 Zipper: 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 Zipper | None: A new zipper focused on the left child, or None if no left child
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 new_breadcrumbs = [breadcrumb] + self.breadcrumbs
94 return Zipper(self.tree["left"], new_breadcrumbs)
95
1696 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
97 """
98 Replace the left subtree of the focus node.
99
100
101 Args:
102 tree (dict | None): The new left subtree
103
18104
105 Returns:
106 Zipper: A new zipper with the updated left subtree
107 """
108 # Edge Case: Handle None tree
109 if self.tree is None:
110 return self
111 new_tree = {
112 "value": self.tree["value"],
113 "left": tree,
114 "right": self.tree["right"]
115 }
116 return Zipper(new_tree, self.breadcrumbs)
117
19118 def right(self) -> "Zipper | None":
20 pass
119 """
120 Move the focus to the right child of the current node.
121
21122
123 Returns:
124 Zipper | None: A new zipper focused on the right child, or None if no right child
125 """
126 # Edge Case: Handle None tree
127 if self.tree is None or self.tree["right"] is None:
128 return None
129
130 # Create breadcrumb to remember current node when going back up
131 breadcrumb = {
132 "parent": {
133 "value": self.tree["value"],
134 "left": self.tree["left"]
135 },
136 "side": "right"
137 }
138
139 new_breadcrumbs = [breadcrumb] + self.breadcrumbs
140 return Zipper(self.tree["right"], new_breadcrumbs)
141
22142 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
143 """
144 Replace the right subtree of the focus node.
145
24146
147 Args:
148 tree (dict | None): The new right subtree
149
150
151 Returns:
152 Zipper: A new zipper with the updated right subtree
153 """
154 # Edge Case: Handle None tree
155 if self.tree is None:
156 return self
157 new_tree = {
158 "value": self.tree["value"],
159 "left": self.tree["left"],
160 "right": tree
161 }
162 return Zipper(new_tree, self.breadcrumbs)
163
25164 def up(self) -> "Zipper | None":
26 pass
165 """
166 Move the focus to the parent of the current node.
167
27168
169 Returns:
170 Zipper | None: A new zipper focused on the parent, or None if at root
171 """
172 # Edge Case: Handle when already at root (no breadcrumbs)
173 if not self.breadcrumbs:
174 return None
175
176 breadcrumb = self.breadcrumbs[0]
177 parent = breadcrumb["parent"]
178 side = breadcrumb["side"]
179
180 # Reconstruct the parent node with current tree in the correct position
181 if side == "left":
182 new_tree = {
183 "value": parent["value"],
184 "left": self.tree,
185 "right": parent["right"]
186 }
187 else: # side == "right"
188 new_tree = {
189 "value": parent["value"],
190 "left": parent["left"],
191 "right": self.tree
192 }
193
194 return Zipper(new_tree, self.breadcrumbs[1:])
195
28196 def to_tree(self) -> dict:
29 pass
197 """
198 Get the entire tree from the root.
199
200
201 Returns:
202 dict: The complete binary tree
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 tree input, None left/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.