Konoha

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 and allows
6 functional navigation and modification operations.
7 """
8
9 def __init__(self, tree, path):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree: The root tree structure
15 path: A list of (parent_tree, is_left_child) tuples representing the path from root to focus
16 """
17 self.tree = tree
18 self.path = path
19
20 # Navigate to the focus node based on the path
21 self.focus = tree
22 for parent, is_left in self.path:
23 if is_left:
24 self.focus = parent['left']
25 else:
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, with focus on the root node.
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 input
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, returning a new zipper.
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 new tree structure with the updated value
71 new_tree = self._copy_tree(self.tree)
72
73 # Navigate to the focus position in the new tree
74 current = new_tree
75 for parent, is_left in self.path:
76 if is_left:
77 current = current['left']
78 else:
79 current = current['right']
80
81 # Update the value
82 current['value'] = value
83
84 return Zipper(new_tree, self.path)
85
1386 def left(self) -> "Zipper | None":
14 pass
15
87 """
88 Move the focus to the left child of the current node.
89
90 Returns:
91 A new Zipper focused on the left child, or None if no left child exists
92 """
93 # Edge Case: Handle None focus
94 if self.focus is None or self.focus['left'] is None:
95 return None
96
97 # Create a new path with the current focus added
98 new_path = self.path + [(self.focus, True)]
99 return Zipper(self.tree, new_path)
100
16101 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
102 """
103 Set the left child of the focus node, returning a new zipper.
104
105 Args:
106 tree: The new left subtree (or None to remove the left child)
107
108 Returns:
109 A new Zipper with the updated left child
110 """
111 # Edge Case: Handle None focus
112 if self.focus is None:
113 return self
114
115 # Create a new tree structure with the updated left child
116 new_tree = self._copy_tree(self.tree)
117
118 # Navigate to the focus position in the new tree
119 current = new_tree
120 for parent, is_left in self.path:
121 if is_left:
122 current = current['left']
123 else:
124 current = current['right']
125
126 # Update the left child
127 current['left'] = tree
128
129 return Zipper(new_tree, self.path)
130
19131 def right(self) -> "Zipper | None":
20 pass
21
132 """
133 Move the focus to the right child of the current node.
134
135 Returns:
136 A new Zipper focused on the right child, or None if no right child exists
137 """
138 # Edge Case: Handle None focus
139 if self.focus is None or self.focus['right'] is None:
140 return None
141
142 # Create a new path with the current focus added
143 new_path = self.path + [(self.focus, False)]
144 return Zipper(self.tree, new_path)
145
22146 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
147 """
148 Set the right child of the focus node, returning a new zipper.
149
150 Args:
151 tree: The new right subtree (or None to remove the right child)
152
153 Returns:
154 A new Zipper with the updated right child
155 """
156 # Edge Case: Handle None focus
157 if self.focus is None:
158 return self
159
160 # Create a new tree structure with the updated right child
161 new_tree = self._copy_tree(self.tree)
162
163 # Navigate to the focus position in the new tree
164 current = new_tree
165 for parent, is_left in self.path:
166 if is_left:
167 current = current['left']
168 else:
169 current = current['right']
170
171 # Update the right child
172 current['right'] = tree
173
174 return Zipper(new_tree, self.path)
175
25176 def up(self) -> "Zipper | None":
26 pass
27
177 """
178 Move the focus to the parent of the current node.
179
180 Returns:
181 A new Zipper focused on the parent, or None if already at the root
182 """
183 # Edge Case: Handle None path (already at root)
184 if not self.path:
185 return None
186
187 # Create a new path without the last element
188 new_path = self.path[:-1]
189 return Zipper(self.tree, new_path)
190
28191 def to_tree(self) -> dict:
29 pass
192 """
193 Get the complete tree from the zipper.
194
195 Returns:
196 The complete binary tree
197 """
198 return self.tree
199
200 def _copy_tree(self, tree):
201 """
202 Create a deep copy of a binary tree.
203
204 Args:
205 tree: The tree to copy
206
207 Returns:
208 A deep copy of the tree
209 """
210 # Edge Case: Handle None tree
211 if tree is None:
212 return None
213
214 return {
215 'value': tree['value'],
216 'left': self._copy_tree(tree['left']),
217 'right': self._copy_tree(tree['right'])
218 }
219
220 # Handled Edge Cases: None tree input, None focus, None path (root), None left/right 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.