fishlium

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
9 def __init__(self, tree, path):
10 """
11 Initialize a Zipper instance.
12
13 Args:
14 tree (dict): The tree structure
15 path (list): The path from root to focus node
16 """
17 self.tree = tree
18 self.path = path
19
320 @staticmethod
421 def from_tree(tree: dict) -> "Zipper":
5 pass
6
22 """
23 Create a zipper from a binary tree.
24
25 Args:
26 tree (dict): A binary tree with keys "value", "left", and "right"
27
28 Returns:
29 Zipper: A new zipper with focus on the root node
30 """
31 # Edge Case: Handle None tree input
32 if tree is None:
33 return None
34 return Zipper(tree, [])
35
36 def _get_focus_node(self):
37 """
38 Get the current focus node by following the path.
39
40 Returns:
41 dict: The focus node
42 """
43 node = self.tree
44 for direction, parent in self.path:
45 if direction == 'left':
46 node = parent['left']
47 else: # direction == 'right'
48 node = parent['right']
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 int: The value of the focus node
57 """
58 return self._get_focus_node()['value']
59
1060 def set_value(self, value: int) -> "Zipper":
11 pass
12
61 """
62 Set the value of the focus node.
63
64 Args:
65 value (int): The new value
66
67 Returns:
68 Zipper: A new zipper with the updated value
69 """
70 # Edge Case: Handle None value
71 if value is None:
72 raise ValueError("Value cannot be None")
73
74 new_tree = self._copy_tree()
75 focus = self._get_node_at_path(new_tree, self.path)
76 focus['value'] = value
77 return Zipper(new_tree, self.path)
78
1379 def left(self) -> "Zipper | None":
14 pass
15
80 """
81 Move the focus to the left child of the current node.
82
83 Returns:
84 Zipper | None: A new zipper with focus on the left child, or None if no left child
85 """
86 focus = self._get_focus_node()
87 # Edge Case: Handle missing left child
88 if focus['left'] is None:
89 return None
90 new_path = self.path + [('left', focus)]
91 return Zipper(self.tree, new_path)
92
1693 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
94 """
95 Set the left child of the focus node.
96
97 Args:
98 tree (dict | None): The new left subtree
99
100 Returns:
101 Zipper: A new zipper with the updated left child
102 """
103 new_tree = self._copy_tree()
104 focus = self._get_node_at_path(new_tree, self.path)
105 focus['left'] = tree
106 return Zipper(new_tree, self.path)
107
19108 def right(self) -> "Zipper | None":
20 pass
21
109 """
110 Move the focus to the right child of the current node.
111
112 Returns:
113 Zipper | None: A new zipper with focus on the right child, or None if no right child
114 """
115 focus = self._get_focus_node()
116 # Edge Case: Handle missing right child
117 if focus['right'] is None:
118 return None
119 new_path = self.path + [('right', focus)]
120 return Zipper(self.tree, new_path)
121
22122 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
123 """
124 Set the right child of the focus node.
125
126 Args:
127 tree (dict | None): The new right subtree
128
129 Returns:
130 Zipper: A new zipper with the updated right child
131 """
132 new_tree = self._copy_tree()
133 focus = self._get_node_at_path(new_tree, self.path)
134 focus['right'] = tree
135 return Zipper(new_tree, self.path)
136
25137 def up(self) -> "Zipper | None":
26 pass
27
138 """
139 Move the focus to the parent of the current node.
140
141 Returns:
142 Zipper | None: A new zipper with focus on the parent, or None if at root
143 """
144 # Edge Case: Handle attempt to move up from root
145 if not self.path:
146 return None
147 new_path = self.path[:-1]
148 return Zipper(self.tree, new_path)
149
28150 def to_tree(self) -> dict:
29 pass
151 """
152 Get the complete tree from the zipper.
153
154 Returns:
155 dict: The complete tree
156 """
157 return self.tree
158
159 def _copy_tree(self):
160 """
161 Create a deep copy of the tree.
162
163 Returns:
164 dict: A deep copy of the tree
165 """
166 def copy_node(node):
167 if node is None:
168 return None
169 return {
170 'value': node['value'],
171 'left': copy_node(node['left']),
172 'right': copy_node(node['right'])
173 }
174 return copy_node(self.tree)
175
176 def _get_node_at_path(self, tree, path):
177 """
178 Get the node at the specified path in the given tree.
179
180 Args:
181 tree (dict): The tree to navigate
182 path (list): The path to follow
183
184 Returns:
185 dict: The node at the path
186 """
187 node = tree
188 for direction, parent in path:
189 if direction == 'left':
190 node = node['left']
191 else: # direction == 'right'
192 node = node['right']
193 return node
194
195# Handled Edge Cases: None tree input, missing left child, missing right child, attempt to move up from root, None value input
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.