bohdan

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
6 for functional navigation and modification operations.
7 """
8
9 def __init__(self, tree, path=None):
10 """
11 Initialize a Zipper with a tree and a path to the focus node.
12
13 Args:
14 tree (dict): The complete tree structure
15 path (list): A list of tuples representing the path to the focus node.
16 Each tuple contains (parent_node, direction) where direction
17 is 'left' or 'right'.
18 """
19 self.tree = tree
20 self.path = path or []
21
322 @staticmethod
423 def from_tree(tree: dict) -> "Zipper":
5 pass
24 """
25 Create a zipper from a binary tree.
26
27 Args:
28 tree (dict): A binary tree with keys "value", "left", and "right"
29
30 Returns:
31 Zipper: A new zipper with focus on the root node
32 """
33 # Edge Case: Handle None tree
34 if tree is None:
35 return None
36 return Zipper(tree)
637
38 def _get_focus_node(self):
39 """
40 Get the current focus node based on the path.
41
42 Returns:
43 dict: The focus node
44 """
45 node = self.tree
46 for parent, direction in self.path:
47 node = node[direction]
48 return node
49
750 def value(self) -> int:
8 pass
9
51 """
52 Get the value of the focus node.
53
54 Returns:
55 int: The value of the focus node
56 """
57 return self._get_focus_node()["value"]
58
1059 def set_value(self, value: int) -> "Zipper":
11 pass
12
60 """
61 Set the value of the focus node, returning a new zipper.
62
63 Args:
64 value (int): The new value for the focus node
65
66 Returns:
67 Zipper: A new zipper with the updated value
68 """
69 # Edge Case: Handle empty path (root node)
70 if not self.path:
71 new_tree = {"value": value, "left": self.tree["left"], "right": self.tree["right"]}
72 return Zipper(new_tree, self.path)
73
74 # Create a deep copy of the tree
75 new_tree = self._deep_copy_tree(self.tree)
76
77 # Navigate to the focus node and update its value
78 node = new_tree
79 for parent, direction in self.path:
80 node = node[direction]
81 node["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 Zipper | None: A new zipper with focus on the left child, or None if no left child
91 """
92 focus = self._get_focus_node()
93 # Edge Case: Handle missing left child
94 if focus["left"] is None:
95 return None
96
97 new_path = self.path + [(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, returning a new zipper.
103
104 Args:
105 tree (dict | None): The new left subtree or None
106
107 Returns:
108 Zipper: A new zipper with the updated left child
109 """
110 # Edge Case: Handle empty path (root node)
111 if not self.path:
112 new_tree = {"value": self.tree["value"], "left": tree, "right": self.tree["right"]}
113 return Zipper(new_tree, self.path)
114
115 # Create a deep copy of the tree
116 new_tree = self._deep_copy_tree(self.tree)
117
118 # Navigate to the focus node and update its left child
119 node = new_tree
120 for parent, direction in self.path:
121 node = node[direction]
122 node["left"] = tree
123
124 return Zipper(new_tree, self.path)
125
19126 def right(self) -> "Zipper | None":
20 pass
21
127 """
128 Move the focus to the right child of the current node.
129
130 Returns:
131 Zipper | None: A new zipper with focus on the right child, or None if no right child
132 """
133 focus = self._get_focus_node()
134 # Edge Case: Handle missing right child
135 if focus["right"] is None:
136 return None
137
138 new_path = self.path + [(focus, "right")]
139 return Zipper(self.tree, new_path)
140
22141 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
142 """
143 Set the right child of the focus node, returning a new zipper.
144
145 Args:
146 tree (dict | None): The new right subtree or None
147
148 Returns:
149 Zipper: A new zipper with the updated right child
150 """
151 # Edge Case: Handle empty path (root node)
152 if not self.path:
153 new_tree = {"value": self.tree["value"], "left": self.tree["left"], "right": tree}
154 return Zipper(new_tree, self.path)
155
156 # Create a deep copy of the tree
157 new_tree = self._deep_copy_tree(self.tree)
158
159 # Navigate to the focus node and update its right child
160 node = new_tree
161 for parent, direction in self.path:
162 node = node[direction]
163 node["right"] = tree
164
165 return Zipper(new_tree, self.path)
166
25167 def up(self) -> "Zipper | None":
26 pass
27
168 """
169 Move the focus to the parent of the current node.
170
171 Returns:
172 Zipper | None: A new zipper with focus on the parent, or None if at root
173 """
174 # Edge Case: Handle already at root
175 if not self.path:
176 return None
177
178 new_path = self.path[:-1]
179 return Zipper(self.tree, new_path)
180
28181 def to_tree(self) -> dict:
29 pass
182 """
183 Get the complete tree from the zipper.
184
185 Returns:
186 dict: The complete tree structure
187 """
188 return self.tree
189
190 def _deep_copy_tree(self, tree):
191 """
192 Create a deep copy of a tree.
193
194 Args:
195 tree (dict): The tree to copy
196
197 Returns:
198 dict: A deep copy of the tree
199 """
200 if tree is None:
201 return None
202
203 return {
204 "value": tree["value"],
205 "left": self._deep_copy_tree(tree["left"]),
206 "right": self._deep_copy_tree(tree["right"])
207 }
208
209 # Handled Edge Cases: None tree in from_tree, missing left child in left,
210 # missing right child in right, empty path in set_value/set_left/set_right,
211 # already 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.