unicode

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: dict, path: list = None):
9 """
10 Initialize a Zipper.
11
12 Args:
13 tree: The binary tree represented as a dict with keys 'value', 'left', 'right'
14 path: A list of directions ('left' or 'right') from root to current focus
15 """
16 self.tree = tree
17 self.path = path or []
18
319 @staticmethod
420 def from_tree(tree: dict) -> "Zipper":
5 pass
6
21 """
22 Create a zipper from a binary tree.
23
24 Args:
25 tree: The binary tree to create a zipper from
26
27 Returns:
28 A new Zipper instance focused on the root of the tree
29
30 Edge Case: Empty tree (None) is handled by creating an empty path
31 """
32 # Edge Case: Handle None tree
33 if tree is None:
34 return Zipper({}, [])
35 return Zipper(tree, [])
36
37 def _get_focus_node(self) -> dict:
38 """
39 Get the current focus node based on the path.
40
41 Returns:
42 The node dict at the current focus
43 """
44 node = self.tree
45 for direction in self.path:
46 if node is None:
47 return None
48 node = node.get(direction)
49 return node
50
751 def value(self) -> int:
8 pass
9
52 """
53 Get the value of the focus node.
54
55 Returns:
56 The value of the focus node
57
58 Edge Case: If focus node is None, this will raise a KeyError
59 """
60 focus = self._get_focus_node()
61 # Edge Case: If focus is None, accessing 'value' will raise KeyError
62 if focus is None:
63 raise KeyError("Cannot get value of None node")
64 return focus['value']
65
1066 def set_value(self, value: int) -> "Zipper":
11 pass
12
67 """
68 Set the value of the focus node.
69
70 Args:
71 value: The new value for the focus node
72
73 Returns:
74 A new Zipper with the updated value
75
76 Edge Case: If focus node is None, this will raise a KeyError
77 """
78 # Edge Case: If focus is None, accessing 'value' will raise KeyError
79 if self._get_focus_node() is None:
80 raise KeyError("Cannot set value of None node")
81
82 new_tree = self._copy_tree(self.tree)
83 node = new_tree
84 for direction in self.path:
85 node = node[direction]
86 node['value'] = value
87 return Zipper(new_tree, self.path.copy())
88
1389 def left(self) -> "Zipper | None":
14 pass
15
90 """
91 Move focus to the left child of the current focus node.
92
93 Returns:
94 A new Zipper focused on the left child, or None if no left child exists
95
96 Edge Case: If focus node is None or has no left child, returns None
97 """
98 focus = self._get_focus_node()
99 # Edge Case: If focus is None or has no left child, return None
100 if focus is None or focus.get('left') is None:
101 return None
102 return Zipper(self.tree, self.path + ['left'])
103
16104 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
105 """
106 Set the left child of the focus node.
107
108 Args:
109 tree: The new left subtree (or None to remove left child)
110
111 Returns:
112 A new Zipper with the updated left child
113
114 Edge Case: If focus node is None, this will raise a KeyError
115 """
116 # Edge Case: If focus is None, accessing 'left' will raise KeyError
117 if self._get_focus_node() is None:
118 raise KeyError("Cannot set left of None node")
119
120 new_tree = self._copy_tree(self.tree)
121 node = new_tree
122 for direction in self.path:
123 node = node[direction]
124 node['left'] = tree
125 return Zipper(new_tree, self.path.copy())
126
19127 def right(self) -> "Zipper | None":
20 pass
21
128 """
129 Move focus to the right child of the current focus node.
130
131 Returns:
132 A new Zipper focused on the right child, or None if no right child exists
133
134 Edge Case: If focus node is None or has no right child, returns None
135 """
136 focus = self._get_focus_node()
137 # Edge Case: If focus is None or has no right child, return None
138 if focus is None or focus.get('right') is None:
139 return None
140 return Zipper(self.tree, self.path + ['right'])
141
22142 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
143 """
144 Set the right child of the focus node.
145
146 Args:
147 tree: The new right subtree (or None to remove right child)
148
149 Returns:
150 A new Zipper with the updated right child
151
152 Edge Case: If focus node is None, this will raise a KeyError
153 """
154 # Edge Case: If focus is None, accessing 'right' will raise KeyError
155 if self._get_focus_node() is None:
156 raise KeyError("Cannot set right of None node")
157
158 new_tree = self._copy_tree(self.tree)
159 node = new_tree
160 for direction in self.path:
161 node = node[direction]
162 node['right'] = tree
163 return Zipper(new_tree, self.path.copy())
164
25165 def up(self) -> "Zipper | None":
26 pass
27
166 """
167 Move focus to the parent of the current focus node.
168
169 Returns:
170 A new Zipper focused on the parent, or None if already at root
171
172 Edge Case: If already at root (empty path), returns None
173 """
174 # Edge Case: If already at root (empty path), return None
175 if not self.path:
176 return None
177 return Zipper(self.tree, self.path[:-1])
178
28179 def to_tree(self) -> dict:
29 pass
180 """
181 Get the complete tree from the zipper.
182
183 Returns:
184 The complete binary tree
185
186 Edge Case: If original tree was None, returns {}
187 """
188 # Edge Case: If original tree was None, return empty dict
189 if not self.tree:
190 return {}
191 return self.tree
192
193 def _copy_tree(self, tree: dict) -> dict:
194 """
195 Create a deep copy of a binary tree.
196
197 Args:
198 tree: The tree to copy
199
200 Returns:
201 A deep copy of the tree
202 """
203 if tree is None:
204 return None
205 return {
206 'value': tree['value'],
207 'left': self._copy_tree(tree.get('left')),
208 'right': self._copy_tree(tree.get('right'))
209 }
210 # Handled Edge Cases: Empty tree, None focus node, missing children, root node navigation
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.