xas

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, path):
10 """
11 Initialize a Zipper instance.
12
13 Args:
14 tree (dict): The current subtree focused on
15 path (list): Path of breadcrumbs from root to current node
16 """
17 self.tree = tree
18 self.path = path
19
320 @staticmethod
421 def from_tree(tree: dict) -> "Zipper":
5 pass
6
22 """
23 Create a zipper from a binary tree.
24
25 Args:
26 tree (dict): A binary tree with keys "value", "left", "right"
27
28 Returns:
29 Zipper: A new zipper focused on the root of the tree
30 """
31 # Edge Case: Handle None tree input
32 if tree is None:
33 return None
34 return Zipper(tree, [])
35
736 def value(self) -> int:
8 pass
9
37 """
38 Get the value of the focus node.
39
40 Returns:
41 int: The value of the current node
42 """
43 # Edge Case: Handle None tree
44 if self.tree is None:
45 return None
46 return self.tree["value"]
47
1048 def set_value(self, value: int) -> "Zipper":
11 pass
12
49 """
50 Set the value of the focus node.
51
52 Args:
53 value (int): The new value for the focus node
54
55 Returns:
56 Zipper: A new zipper with the updated value
57 """
58 # Edge Case: Handle None tree
59 if self.tree is None:
60 return self
61 new_tree = {
62 "value": value,
63 "left": self.tree["left"],
64 "right": self.tree["right"]
65 }
66 return Zipper(new_tree, self.path)
67
1368 def left(self) -> "Zipper | None":
14 pass
15
69 """
70 Move the focus to the left child of the current node.
71
72 Returns:
73 Zipper | None: A new zipper focused on the left child, or None if no left child
74 """
75 # Edge Case: Handle None tree
76 if self.tree is None or self.tree["left"] is None:
77 return None
78
79 # Create breadcrumb to store current context
80 breadcrumb = {
81 "parent": {
82 "value": self.tree["value"],
83 "right": self.tree["right"]
84 },
85 "side": "left"
86 }
87
88 new_path = self.path + [breadcrumb]
89 return Zipper(self.tree["left"], new_path)
90
1691 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
92 """
93 Set the left child of the focus node.
94
95 Args:
96 tree (dict | None): The new left subtree
97
98 Returns:
99 Zipper: A new zipper with the updated left child
100 """
101 # Edge Case: Handle None tree
102 if self.tree is None:
103 return self
104 new_tree = {
105 "value": self.tree["value"],
106 "left": tree,
107 "right": self.tree["right"]
108 }
109 return Zipper(new_tree, self.path)
110
19111 def right(self) -> "Zipper | None":
20 pass
21
112 """
113 Move the focus to the right child of the current node.
114
115 Returns:
116 Zipper | None: A new zipper focused on the right child, or None if no right child
117 """
118 # Edge Case: Handle None tree or no right child
119 if self.tree is None or self.tree["right"] is None:
120 return None
121
122 # Create breadcrumb to store current context
123 breadcrumb = {
124 "parent": {
125 "value": self.tree["value"],
126 "left": self.tree["left"]
127 },
128 "side": "right"
129 }
130
131 new_path = self.path + [breadcrumb]
132 return Zipper(self.tree["right"], new_path)
133
22134 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
135 """
136 Set the right child of the focus node.
137
138 Args:
139 tree (dict | None): The new right subtree
140
141 Returns:
142 Zipper: A new zipper with the updated right child
143 """
144 # Edge Case: Handle None tree
145 if self.tree is None:
146 return self
147 new_tree = {
148 "value": self.tree["value"],
149 "left": self.tree["left"],
150 "right": tree
151 }
152 return Zipper(new_tree, self.path)
153
25154 def up(self) -> "Zipper | None":
26 pass
27
155 """
156 Move the focus to the parent of the current node.
157
158 Returns:
159 Zipper | None: A new zipper focused on the parent, or None if at root
160 """
161 # Edge Case: Handle None path (at root)
162 if not self.path:
163 return None
164
165 # Get the last breadcrumb
166 breadcrumb = self.path[-1]
167 parent = breadcrumb["parent"]
168 side = breadcrumb["side"]
169
170 # Reconstruct parent tree with current subtree in correct position
171 if side == "left":
172 new_tree = {
173 "value": parent["value"],
174 "left": self.tree,
175 "right": parent["right"]
176 }
177 else: # side == "right"
178 new_tree = {
179 "value": parent["value"],
180 "left": parent["left"],
181 "right": self.tree
182 }
183
184 return Zipper(new_tree, self.path[:-1])
185
28186 def to_tree(self) -> dict:
29 pass
187 """
188 Get the full tree from the zipper.
189
190 Returns:
191 dict: The complete binary tree
192 """
193 # Start with current tree
194 result = self.tree
195
196 # Reconstruct the tree by moving up the path
197 for breadcrumb in reversed(self.path):
198 parent = breadcrumb["parent"]
199 side = breadcrumb["side"]
200
201 if side == "left":
202 result = {
203 "value": parent["value"],
204 "left": result,
205 "right": parent["right"]
206 }
207 else: # side == "right"
208 result = {
209 "value": parent["value"],
210 "left": parent["left"],
211 "right": result
212 }
213
214 return result
215 # Handled Edge Cases: None tree input, None tree in operations, no left/right child, at root node
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.