sn62

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 root tree structure
15 path (list): Path from root to current focus node, containing breadcrumb information
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", and "right"
27
28 Returns:
29 Zipper: A new zipper focused on the root of the tree
30 """
31 # Edge Case: Empty tree
32 if tree is None:
33 return None
34 return Zipper(tree, [])
35
36 def _get_focus_node(self):
37 """
38 Get the current focus node based on the path.
39
40 Returns:
41 dict: The current focus node
42 """
43 node = self.tree
44 for breadcrumb in self.path:
45 direction = breadcrumb['direction']
46 if direction == 'left':
47 node = node['left']
48 else: # direction == 'right'
49 node = node['right']
50 return node
51
752 def value(self) -> int:
8 pass
9
53 """
54 Get the value of the focus node.
55
56 Returns:
57 int: The value of the focus node
58 """
59 return self._get_focus_node()['value']
60
1061 def set_value(self, value: int) -> "Zipper":
11 pass
12
62 """
63 Set the value of the focus node, returning a new zipper.
64
65 Args:
66 value (int): The new value for the focus node
67
68 Returns:
69 Zipper: A new zipper with the updated value
70 """
71 # Edge Case: Empty tree
72 if not self.path and self.tree is None:
73 return Zipper({'value': value, 'left': None, 'right': None}, [])
74
75 new_tree = self._copy_tree(self.tree)
76 node = new_tree
77
78 for breadcrumb in self.path:
79 direction = breadcrumb['direction']
80 if direction == 'left':
81 node = node['left']
82 else: # direction == 'right'
83 node = node['right']
84
85 node['value'] = value
86 return Zipper(new_tree, self.path[:])
87
1388 def left(self) -> "Zipper | None":
14 pass
15
89 """
90 Move the focus to the left child of the current node.
91
92 Returns:
93 Zipper | None: A new zipper focused on the left child, or None if no left child exists
94 """
95 focus_node = self._get_focus_node()
96 # Edge Case: No left child
97 if focus_node['left'] is None:
98 return None
99
100 new_path = self.path[:] + [{'direction': 'left', 'parent': focus_node}]
101 return Zipper(self.tree, new_path)
102
16103 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
104 """
105 Set the left child of the focus node, returning a new zipper.
106
107 Args:
108 tree (dict | None): The new left subtree
109
110 Returns:
111 Zipper: A new zipper with the updated left child
112 """
113 new_tree = self._copy_tree(self.tree)
114 node = new_tree
115
116 # Edge Case: Setting left on root of empty tree
117 if not self.path and self.tree is None:
118 return Zipper({'value': 0, 'left': tree, 'right': None}, [])
119
120 for breadcrumb in self.path:
121 direction = breadcrumb['direction']
122 if direction == 'left':
123 node = node['left']
124 else: # direction == 'right'
125 node = node['right']
126
127 node['left'] = tree
128 return Zipper(new_tree, self.path[:])
129
19130 def right(self) -> "Zipper | None":
20 pass
21
131 """
132 Move the focus to the right child of the current node.
133
134 Returns:
135 Zipper | None: A new zipper focused on the right child, or None if no right child exists
136 """
137 focus_node = self._get_focus_node()
138 # Edge Case: No right child
139 if focus_node['right'] is None:
140 return None
141
142 new_path = self.path[:] + [{'direction': 'right', 'parent': focus_node}]
143 return Zipper(self.tree, new_path)
144
22145 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
146 """
147 Set the right child of the focus node, returning a new zipper.
148
149 Args:
150 tree (dict | None): The new right subtree
151
152 Returns:
153 Zipper: A new zipper with the updated right child
154 """
155 new_tree = self._copy_tree(self.tree)
156 node = new_tree
157
158 # Edge Case: Setting right on root of empty tree
159 if not self.path and self.tree is None:
160 return Zipper({'value': 0, 'left': None, 'right': tree}, [])
161
162 for breadcrumb in self.path:
163 direction = breadcrumb['direction']
164 if direction == 'left':
165 node = node['left']
166 else: # direction == 'right'
167 node = node['right']
168
169 node['right'] = tree
170 return Zipper(new_tree, self.path[:])
171
25172 def up(self) -> "Zipper | None":
26 pass
27
173 """
174 Move the focus to the parent of the current node.
175
176 Returns:
177 Zipper | None: A new zipper focused on the parent, or None if already at root
178 """
179 # Edge Case: Already at root
180 if not self.path:
181 return None
182
183 return Zipper(self.tree, self.path[:-1])
184
28185 def to_tree(self) -> dict:
29 pass
186 """
187 Get the complete tree from the zipper.
188
189 Returns:
190 dict: The complete binary tree
191 """
192 return self.tree
193
194 def _copy_tree(self, tree):
195 """
196 Create a deep copy of a tree.
197
198 Args:
199 tree (dict): The tree to copy
200
201 Returns:
202 dict: A deep copy of the tree
203 """
204 if tree is None:
205 return None
206 return {
207 'value': tree['value'],
208 'left': self._copy_tree(tree['left']),
209 'right': self._copy_tree(tree['right'])
210 }
211
212 # Handled Edge Cases: Empty tree in from_tree, no left child in left, no right child in right, already at root in up, setting children on empty 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.