seahawk

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, breadcrumbs=None):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree: The current subtree (focus)
15 breadcrumbs: Path information to navigate back up the tree
16 """
17 self.tree = tree
18 self.breadcrumbs = breadcrumbs or []
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: A binary tree represented as a dict with keys "value", "left", and "right"
27
28 Returns:
29 A new Zipper instance focused on the root of the tree
30
31 Edge Case: Empty tree (None) is handled by creating an empty zipper
32 """
33 # Edge Case: Handle None tree
34 if tree is None:
35 return Zipper(None)
36 return Zipper(tree)
37
738 def value(self) -> int:
8 pass
9
39 """
40 Get the value of the focus node.
41
42 Returns:
43 The value of the current node
44
45 Edge Case: If the current node is None, this would raise an error in a real implementation,
46 but according to the problem definition, we assume valid operations
47 """
48 return self.tree["value"]
49
1050 def set_value(self, value: int) -> "Zipper":
11 pass
12
51 """
52 Set the value of the focus node.
53
54 Args:
55 value: The new value for the focus node
56
57 Returns:
58 A new Zipper with the updated value
59
60 Edge Case: If the current node is None, creates a new node
61 """
62 # Edge Case: Handle None tree
63 if self.tree is None:
64 new_tree = {"value": value, "left": None, "right": None}
65 else:
66 new_tree = {"value": value, "left": self.tree["left"], "right": self.tree["right"]}
67 return Zipper(new_tree, self.breadcrumbs)
68
1369 def left(self) -> "Zipper | None":
14 pass
15
70 """
71 Move the focus to the left child.
72
73 Returns:
74 A new Zipper focused on the left child, or None if no left child exists
75
76 Edge Case: If there's no left child or current node is None, returns None
77 """
78 # Edge Case: Handle None tree or missing left child
79 if self.tree is None or self.tree["left"] is None:
80 return None
81
82 # Create breadcrumb to remember how to get back to this node
83 breadcrumb = {
84 "parent": {"value": self.tree["value"], "right": self.tree["right"]},
85 "side": "left"
86 }
87
88 return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb])
89
1690 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
91 """
92 Set the left child of the focus node.
93
94 Args:
95 tree: The new left subtree (or None to remove the left child)
96
97 Returns:
98 A new Zipper with the updated left child
99
100 Edge Case: If the current node is None, creates a new node
101 """
102 # Edge Case: Handle None tree
103 if self.tree is None:
104 new_tree = {"value": 0, "left": tree, "right": None} # Default value of 0
105 else:
106 new_tree = {"value": self.tree["value"], "left": tree, "right": self.tree["right"]}
107 return Zipper(new_tree, self.breadcrumbs)
108
19109 def right(self) -> "Zipper | None":
20 pass
21
110 """
111 Move the focus to the right child.
112
113 Returns:
114 A new Zipper focused on the right child, or None if no right child exists
115
116 Edge Case: If there's no right child or current node is None, returns None
117 """
118 # Edge Case: Handle None tree or missing right child
119 if self.tree is None or self.tree["right"] is None:
120 return None
121
122 # Create breadcrumb to remember how to get back to this node
123 breadcrumb = {
124 "parent": {"value": self.tree["value"], "left": self.tree["left"]},
125 "side": "right"
126 }
127
128 return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb])
129
22130 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
131 """
132 Set the right child of the focus node.
133
134 Args:
135 tree: The new right subtree (or None to remove the right child)
136
137 Returns:
138 A new Zipper with the updated right child
139
140 Edge Case: If the current node is None, creates a new node
141 """
142 # Edge Case: Handle None tree
143 if self.tree is None:
144 new_tree = {"value": 0, "left": None, "right": tree} # Default value of 0
145 else:
146 new_tree = {"value": self.tree["value"], "left": self.tree["left"], "right": tree}
147 return Zipper(new_tree, self.breadcrumbs)
148
25149 def up(self) -> "Zipper | None":
26 pass
27
150 """
151 Move the focus to the parent node.
152
153 Returns:
154 A new Zipper focused on the parent, or None if already at the root
155
156 Edge Case: If already at the root (no breadcrumbs), returns None
157 """
158 # Edge Case: Handle already at root
159 if not self.breadcrumbs:
160 return None
161
162 # Get the last breadcrumb
163 last_breadcrumb = self.breadcrumbs[-1]
164 parent = last_breadcrumb["parent"]
165 side = last_breadcrumb["side"]
166
167 # Reconstruct the parent with the current tree in the correct position
168 if side == "left":
169 new_parent = {"value": parent["value"], "left": self.tree, "right": parent["right"]}
170 else: # side == "right"
171 new_parent = {"value": parent["value"], "left": parent["left"], "right": self.tree}
172
173 return Zipper(new_parent, self.breadcrumbs[:-1])
174
28175 def to_tree(self) -> dict:
29 pass
176 """
177 Get the complete tree from the zipper.
178
179 Returns:
180 The complete binary tree
181
182 Edge Case: If the current node is None, reconstructs the tree from breadcrumbs
183 """
184 # Navigate back to the root to get the complete tree
185 current = self
186 while True:
187 parent = current.up()
188 if parent is None:
189 break
190 current = parent
191
192 return current.tree
193
194 # Handled Edge Cases: Empty tree, None nodes, missing children, already at root
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.