lexicon

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 for
6 functional navigation and modification operations.
7 """
8 def __init__(self, tree, path):
9 """
10 Initialize a Zipper.
11
12 Args:
13 tree: The full tree structure (dict with 'value', 'left', 'right' keys)
14 path: A list of breadcrumbs representing the path from root to focus
15 """
16 self.tree = tree
17 self.path = path
18
19 # Navigate to the focus node based on the path
20 self.focus = tree
21 for direction, parent in self.path:
22 if direction == 'left':
23 self.focus = parent['left']
24 else: # direction == 'right'
25 self.focus = parent['right']
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: A binary tree represented as a dict with 'value', 'left', 'right' keys
34
35 Returns:
36 A new Zipper instance focused on the root of the tree
37 """
38 # Edge Case: Handle None tree input
39 if tree is None:
40 return None
41 return Zipper(tree, [])
42
743 def value(self) -> int:
8 pass
9
44 """
45 Get the value of the focus node.
46
47 Returns:
48 The value of the focus node
49 """
50 # Edge Case: Handle None focus
51 if self.focus is None:
52 return None
53 return self.focus['value']
54
1055 def set_value(self, value: int) -> "Zipper":
11 pass
12
56 """
57 Set the value of the focus node, returning a new zipper.
58
59 Args:
60 value: The new value for the focus node
61
62 Returns:
63 A new Zipper with the updated value
64 """
65 # Edge Case: Handle None focus
66 if self.focus is None:
67 return self
68
69 # Create a new tree with the updated value
70 new_tree = self._copy_tree(self.tree)
71
72 # Navigate to the focus position in the new tree
73 current = new_tree
74 for direction, parent in self.path:
75 if direction == 'left':
76 current = current['left']
77 else: # direction == 'right'
78 current = current['right']
79
80 # Update the value
81 current['value'] = value
82
83 return Zipper(new_tree, self.path)
84
1385 def left(self) -> "Zipper | None":
14 pass
15
86 """
87 Move the focus to the left child of the current node.
88
89 Returns:
90 A new Zipper focused on the left child, or None if no left child exists
91 """
92 # Edge Case: Handle None focus
93 if self.focus is None or self.focus['left'] is None:
94 return None
95
96 # Add breadcrumb for current node
97 new_path = self.path + [('left', self.focus)]
98 return Zipper(self.tree, new_path)
99
16100 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
101 """
102 Set the left child of the focus node, returning a new zipper.
103
104 Args:
105 tree: The new left subtree (or None to remove the left child)
106
107 Returns:
108 A new Zipper with the updated left child
109 """
110 # Edge Case: Handle None focus
111 if self.focus is None:
112 return self
113
114 # Create a new tree with the updated left child
115 new_tree = self._copy_tree(self.tree)
116
117 # Navigate to the focus position in the new tree
118 current = new_tree
119 for direction, parent in self.path:
120 if direction == 'left':
121 current = current['left']
122 else: # direction == 'right'
123 current = current['right']
124
125 # Update the left child
126 current['left'] = tree
127
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 A new Zipper focused on the right child, or None if no right child exists
136 """
137 # Edge Case: Handle None focus
138 if self.focus is None or self.focus['right'] is None:
139 return None
140
141 # Add breadcrumb for current node
142 new_path = self.path + [('right', self.focus)]
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: The new right subtree (or None to remove the right child)
151
152 Returns:
153 A new Zipper with the updated right child
154 """
155 # Edge Case: Handle None focus
156 if self.focus is None:
157 return self
158
159 # Create a new tree with the updated right child
160 new_tree = self._copy_tree(self.tree)
161
162 # Navigate to the focus position in the new tree
163 current = new_tree
164 for direction, parent in self.path:
165 if direction == 'left':
166 current = current['left']
167 else: # direction == 'right'
168 current = current['right']
169
170 # Update the right child
171 current['right'] = tree
172
173 return Zipper(new_tree, self.path)
174
25175 def up(self) -> "Zipper | None":
26 pass
27
176 """
177 Move the focus to the parent of the current node.
178
179 Returns:
180 A new Zipper focused on the parent, or None if already at the root
181 """
182 # Edge Case: Handle None path (already at root)
183 if not self.path:
184 return None
185
186 # Remove the last breadcrumb to move up
187 new_path = self.path[:-1]
188 return Zipper(self.tree, new_path)
189
28190 def to_tree(self) -> dict:
29 pass
191 """
192 Get the full binary tree from the zipper.
193
194 Returns:
195 The complete binary tree as a dict
196 """
197 return self.tree
198
199 def _copy_tree(self, tree):
200 """
201 Create a deep copy of a binary tree.
202
203 Args:
204 tree: The tree to copy
205
206 Returns:
207 A deep copy of the tree
208 """
209 if tree is None:
210 return None
211 return {
212 'value': tree['value'],
213 'left': self._copy_tree(tree['left']),
214 'right': self._copy_tree(tree['right'])
215 }
216
217 # Handled Edge Cases: None tree input, None focus, None path (root), None children
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.