Alone

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 maintains a focus on a specific node in the tree, allowing for
6 functional navigation and modification. It stores the path from the root
7 to the current focus node, enabling movement in all directions.
8
9 Attributes:
10 _focus (dict): The current node in focus with keys 'value', 'left', 'right'
11 _path (list): A list of tuples representing the path from root to focus
12 Each tuple contains (parent_node, direction) where direction
13 is 'left' or 'right'
14 """
15
16 def __init__(self, focus: dict, path: list):
17 """
18 Initialize a Zipper with a focus node and path.
19
20 Args:
21 focus (dict): The node currently in focus
22 path (list): The path from root to focus
23 """
24 self._focus = focus
25 self._path = path
26
327 @staticmethod
428 def from_tree(tree: dict) -> "Zipper":
5 pass
6
29 """
30 Create a zipper from a binary tree, with focus on the root node.
31
32 Args:
33 tree (dict): A binary tree represented as a dict with keys 'value', 'left', 'right'
34
35 Returns:
36 Zipper: A new zipper with focus on the root of the tree
37
38 # Edge Case: Empty tree (None)
39 """
40 # Handled Edge Cases: Empty tree
41 if tree is None:
42 return None
43 return Zipper(tree, [])
44
745 def value(self) -> int:
8 pass
9
46 """
47 Get the value of the focus node.
48
49 Returns:
50 int: The value of the focus node
51
52 # Edge Case: Called on None zipper
53 """
54 # Handled Edge Cases: None zipper
55 if self is None:
56 return None
57 return self._focus["value"]
58
1059 def set_value(self, value: int) -> "Zipper":
11 pass
12
60 """
61 Set the value of the focus node, returning a new zipper.
62
63 Args:
64 value (int): The new value for the focus node
65
66 Returns:
67 Zipper: A new zipper with the updated focus node value
68
69 # Edge Case: Called on None zipper
70 """
71 # Handled Edge Cases: None zipper
72 if self is None:
73 return None
74 new_focus = {
75 "value": value,
76 "left": self._focus["left"],
77 "right": self._focus["right"]
78 }
79 return Zipper(new_focus, self._path)
80
1381 def left(self) -> "Zipper | None":
14 pass
15
82 """
83 Move the focus to the left child of the current node.
84
85 Returns:
86 Zipper | None: A new zipper with focus on the left child, or None if no left child
87
88 # Edge Case: Called on None zipper
89 # Edge Case: No left child
90 """
91 # Handled Edge Cases: None zipper, No left child
92 if self is None:
93 return None
94 if self._focus["left"] is None:
95 return None
96 new_path = self._path + [(self._focus, "left")]
97 return Zipper(self._focus["left"], new_path)
98
1699 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
100 """
101 Set the left child of the focus node, returning a new zipper.
102
103 Args:
104 tree (dict | None): The new left subtree
105
106 Returns:
107 Zipper: A new zipper with the updated left child
108
109 # Edge Case: Called on None zipper
110 """
111 # Handled Edge Cases: None zipper
112 if self is None:
113 return None
114 new_focus = {
115 "value": self._focus["value"],
116 "left": tree,
117 "right": self._focus["right"]
118 }
119 return Zipper(new_focus, self._path)
120
19121 def right(self) -> "Zipper | None":
20 pass
21
122 """
123 Move the focus to the right child of the current node.
124
125 Returns:
126 Zipper | None: A new zipper with focus on the right child, or None if no right child
127
128 # Edge Case: Called on None zipper
129 # Edge Case: No right child
130 """
131 # Handled Edge Cases: None zipper, No right child
132 if self is None:
133 return None
134 if self._focus["right"] is None:
135 return None
136 new_path = self._path + [(self._focus, "right")]
137 return Zipper(self._focus["right"], new_path)
138
22139 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
140 """
141 Set the right child of the focus node, returning a new zipper.
142
143 Args:
144 tree (dict | None): The new right subtree
145
146 Returns:
147 Zipper: A new zipper with the updated right child
148
149 # Edge Case: Called on None zipper
150 """
151 # Handled Edge Cases: None zipper
152 if self is None:
153 return None
154 new_focus = {
155 "value": self._focus["value"],
156 "left": self._focus["left"],
157 "right": tree
158 }
159 return Zipper(new_focus, self._path)
160
25161 def up(self) -> "Zipper | None":
26 pass
27
162 """
163 Move the focus to the parent of the current node.
164
165 Returns:
166 Zipper | None: A new zipper with focus on the parent, or None if at root
167
168 # Edge Case: Called on None zipper
169 # Edge Case: Already at root (no parent)
170 """
171 # Handled Edge Cases: None zipper, Already at root
172 if self is None or not self._path:
173 return None
174
175 parent, direction = self._path[-1]
176 new_focus = parent.copy()
177
178 # Update the parent's child reference to match the current focus
179 if direction == "left":
180 new_focus["left"] = self._focus
181 else: # direction == "right"
182 new_focus["right"] = self._focus
183
184 return Zipper(new_focus, self._path[:-1])
185
28186 def to_tree(self) -> dict:
29 pass
187 """
188 Reconstruct the entire tree from the current zipper state.
189
190 Returns:
191 dict: The complete binary tree
192
193 # Edge Case: Called on None zipper
194 """
195 # Handled Edge Cases: None zipper
196 if self is None:
197 return None
198
199 # Start with the current focus
200 tree = self._focus
201
202 # Work our way up the path, rebuilding the tree
203 for node, direction in reversed(self._path):
204 if direction == "left":
205 tree = {
206 "value": node["value"],
207 "left": tree,
208 "right": node["right"]
209 }
210 else: # direction == "right"
211 tree = {
212 "value": node["value"],
213 "left": node["left"],
214 "right": tree
215 }
216
217 return tree
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.