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 efficient 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
14 path: A list of tuples representing the path from root to focus.
15 Each tuple contains (parent_tree, side) where side is 'left' or 'right'
16 """
17 self.tree = tree
18 self.path = path
19
20 # Navigate to the focus node based on path
21 self.focus = self.tree
22 for parent, side in self.path:
23 if side == 'left':
24 self.focus = parent['left']
25 else: # side == 'right'
26 self.focus = parent['right']
27
328 @staticmethod
429 def from_tree(tree: dict) -> "Zipper":
5 pass
6
30 """
31 Create a zipper from a binary tree.
32
33 Args:
34 tree: A binary tree represented as a dict with keys "value", "left", and "right"
35
36 Returns:
37 A new Zipper instance focused on the root of the tree
38 """
39 # Edge Case: Handle None tree
40 if tree is None:
41 return None
42 return Zipper(tree, [])
43
744 def value(self) -> int:
8 pass
9
45 """
46 Get the value of the focus node.
47
48 Returns:
49 The value of the focus node
50 """
51 # Edge Case: Handle None focus
52 if self.focus is None:
53 return None
54 return self.focus['value']
55
1056 def set_value(self, value: int) -> "Zipper":
11 pass
12
57 """
58 Set the value of the focus node.
59
60 Args:
61 value: The new value for the focus node
62
63 Returns:
64 A new Zipper with the updated value
65 """
66 # Edge Case: Handle None focus
67 if self.focus is None:
68 return self
69
70 # Create a deep copy of the tree
71 new_tree = self._deep_copy_tree(self.tree)
72
73 # Navigate to the focus in the new tree and update the value
74 new_focus = new_tree
75 for parent, side in self.path:
76 if side == 'left':
77 new_focus = new_focus['left']
78 else: # side == 'right'
79 new_focus = new_focus['right']
80
81 new_focus['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 focus 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 # Create a new path that includes the move to the left child
97 new_path = self.path + [(self.focus, 'left')]
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.
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 deep copy of the tree
115 new_tree = self._deep_copy_tree(self.tree)
116
117 # Navigate to the focus in the new tree and update the left child
118 new_focus = new_tree
119 for parent, side in self.path:
120 if side == 'left':
121 new_focus = new_focus['left']
122 else: # side == 'right'
123 new_focus = new_focus['right']
124
125 new_focus['left'] = tree
126
127 return Zipper(new_tree, self.path)
128
19129 def right(self) -> "Zipper | None":
20 pass
21
130 """
131 Move the focus to the right child of the current focus node.
132
133 Returns:
134 A new Zipper focused on the right child, or None if no right child exists
135 """
136 # Edge Case: Handle None focus
137 if self.focus is None or self.focus['right'] is None:
138 return None
139
140 # Create a new path that includes the move to the right child
141 new_path = self.path + [(self.focus, 'right')]
142 return Zipper(self.tree, new_path)
143
22144 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
145 """
146 Set the right child of the focus node.
147
148 Args:
149 tree: The new right subtree (or None to remove the right child)
150
151 Returns:
152 A new Zipper with the updated right child
153 """
154 # Edge Case: Handle None focus
155 if self.focus is None:
156 return self
157
158 # Create a deep copy of the tree
159 new_tree = self._deep_copy_tree(self.tree)
160
161 # Navigate to the focus in the new tree and update the right child
162 new_focus = new_tree
163 for parent, side in self.path:
164 if side == 'left':
165 new_focus = new_focus['left']
166 else: # side == 'right'
167 new_focus = new_focus['right']
168
169 new_focus['right'] = tree
170
171 return Zipper(new_tree, self.path)
172
25173 def up(self) -> "Zipper | None":
26 pass
27
174 """
175 Move the focus to the parent of the current focus node.
176
177 Returns:
178 A new Zipper focused on the parent, or None if the focus is already at the root
179 """
180 # Edge Case: Handle focus at root (empty path)
181 if len(self.path) == 0:
182 return None
183
184 # Create a new path that removes the last step (moving up)
185 new_path = self.path[:-1]
186 return Zipper(self.tree, new_path)
187
28188 def to_tree(self) -> dict:
29 pass
189 """
190 Get the full tree from the zipper.
191
192 Returns:
193 The full binary tree
194 """
195 return self.tree
196
197 def _deep_copy_tree(self, tree):
198 """
199 Create a deep copy of a binary tree.
200
201 Args:
202 tree: The tree to copy
203
204 Returns:
205 A deep copy of the tree
206 """
207 if tree is None:
208 return None
209
210 return {
211 'value': tree['value'],
212 'left': self._deep_copy_tree(tree['left']),
213 'right': self._deep_copy_tree(tree['right'])
214 }
215
216 # Handled Edge Cases: None tree in from_tree, None focus in value, set_value, left, set_left, right, set_right, focus at root in up
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.