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, allowing
6 navigation and modification operations.
7 """
8
9 def __init__(self, tree, breadcrumbs=None):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree: The tree node that is currently in focus
15 breadcrumbs: A list of breadcrumb dictionaries tracking the path to the focus
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 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 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 or missing left child
81 if self.tree is None or self.tree["left"] is None:
82 return None
83
84 new_breadcrumbs = self.breadcrumbs + [{
85 "parent": self.tree,
86 "side": "left",
87 "sibling": self.tree["right"]
88 }]
89
90 return Zipper(self.tree["left"], new_breadcrumbs)
91
1692 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
93 """
94 Set the left child of the focus node.
95
96
97 Args:
98 tree: The new left subtree (or None to remove the left child)
99
18100
101 Returns:
102 A new Zipper with the updated left child
103 """
104 # Edge Case: Handle None tree
105 if self.tree is None:
106 return self
107 new_tree = {
108 "value": self.tree["value"],
109 "left": tree,
110 "right": self.tree["right"]
111 }
112 return Zipper(new_tree, self.breadcrumbs)
113
19114 def right(self) -> "Zipper | None":
20 pass
115 """
116 Move the focus to the right child of the current node.
117
21118
119 Returns:
120 A new Zipper focused on the right child, or None if no right child exists
121 """
122 # Edge Case: Handle None tree or missing right child
123 if self.tree is None or self.tree["right"] is None:
124 return None
125
126 new_breadcrumbs = self.breadcrumbs + [{
127 "parent": self.tree,
128 "side": "right",
129 "sibling": self.tree["left"]
130 }]
131
132 return Zipper(self.tree["right"], new_breadcrumbs)
133
22134 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
135 """
136 Set the right child of the focus node.
137
24138
139 Args:
140 tree: The new right subtree (or None to remove the right child)
141
142
143 Returns:
144 A new Zipper with the updated right child
145 """
146 # Edge Case: Handle None tree
147 if self.tree is None:
148 return self
149 new_tree = {
150 "value": self.tree["value"],
151 "left": self.tree["left"],
152 "right": tree
153 }
154 return Zipper(new_tree, self.breadcrumbs)
155
25156 def up(self) -> "Zipper | None":
26 pass
157 """
158 Move the focus to the parent of the current node.
159
27160
161 Returns:
162 A new Zipper focused on the parent, or None if already at the root
163 """
164 # Edge Case: Handle when already at root (no breadcrumbs)
165 if not self.breadcrumbs:
166 return None
167
168 # Get the last breadcrumb (most recent ancestor)
169 parent_info = self.breadcrumbs[-1]
170 parent = parent_info["parent"].copy()
171
172 # Update the parent's child reference to match the current tree
173 if parent_info["side"] == "left":
174 parent["left"] = self.tree
175 parent["right"] = parent_info["sibling"]
176 else: # side == "right"
177 parent["right"] = self.tree
178 parent["left"] = parent_info["sibling"]
179
180 # Create new zipper with parent as focus and remaining breadcrumbs
181 return Zipper(parent, self.breadcrumbs[:-1])
182
28183 def to_tree(self) -> dict:
29 pass
184 """
185 Get the complete tree from the zipper.
186
187
188 Returns:
189 The complete binary tree
190 """
191 # Navigate back to the root to get the complete tree
192 current = self
193 while current.breadcrumbs:
194 current = current.up()
195 return current.tree
196
197 # Handled Edge Cases: None tree input, missing children, 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.