sn62

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 navigation and modification operations while preserving the tree structure.
7 """
8
9 def __init__(self, tree, breadcrumbs=None):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree (dict): The current subtree with keys 'value', 'left', 'right'
15 breadcrumbs (list): List of breadcrumb dictionaries for navigation
16 """
17 self.tree = tree
18 self.breadcrumbs = breadcrumbs or []
19
320 @staticmethod
421 def from_tree(tree: dict) -> "Zipper":
5 pass
22 """
23 Create a zipper from a binary tree.
24
25 Args:
26 tree (dict): A binary tree with keys 'value', 'left', 'right'
27
628
29 Returns:
30 Zipper: A new zipper focused on the root of the tree
31 """
32 # Edge Case: Handle None tree input
33 if tree is None:
34 return None
35 return Zipper(tree)
36
737 def value(self) -> int:
8 pass
38 """
39 Get the value of the focus node.
40
941
42 Returns:
43 int: The value of the current node
44 """
45 # Edge Case: Handle None tree
46 if self.tree is None:
47 return None
48 return self.tree["value"]
49
1050 def set_value(self, value: int) -> "Zipper":
11 pass
51 """
52 Set the value of the focus node.
53
1254
55 Args:
56 value (int): The new value for the focus node
57
58
59 Returns:
60 Zipper: A new zipper with the updated value
61 """
62 # Edge Case: Handle None tree
63 if self.tree is None:
64 return self
65 new_tree = {
66 "value": value,
67 "left": self.tree["left"],
68 "right": self.tree["right"]
69 }
70 return Zipper(new_tree, self.breadcrumbs)
71
1372 def left(self) -> "Zipper | None":
14 pass
73 """
74 Move the focus to the left child.
75
1576
77 Returns:
78 Zipper | None: A new zipper focused on the left child, or None if no left child
79 """
80 # Edge Case: Handle None tree
81 if self.tree is None or self.tree["left"] is None:
82 return None
83
84 # Create breadcrumb to remember current node when going left
85 breadcrumb = {
86 "parent": self.tree,
87 "side": "left",
88 "right": self.tree["right"]
89 }
90
91 return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb])
92
1693 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
94 """
95 Set the left subtree of the focus node.
96
97
98 Args:
99 tree (dict | None): The new left subtree
100
18101
102 Returns:
103 Zipper: A new zipper with the updated left subtree
104 """
105 # Edge Case: Handle None tree
106 if self.tree is None:
107 return self
108 new_tree = {
109 "value": self.tree["value"],
110 "left": tree,
111 "right": self.tree["right"]
112 }
113 return Zipper(new_tree, self.breadcrumbs)
114
19115 def right(self) -> "Zipper | None":
20 pass
116 """
117 Move the focus to the right child.
118
21119
120 Returns:
121 Zipper | None: A new zipper focused on the right child, or None if no right child
122 """
123 # Edge Case: Handle None tree
124 if self.tree is None or self.tree["right"] is None:
125 return None
126
127 # Create breadcrumb to remember current node when going right
128 breadcrumb = {
129 "parent": self.tree,
130 "side": "right",
131 "left": self.tree["left"]
132 }
133
134 return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb])
135
22136 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
137 """
138 Set the right subtree of the focus node.
139
140
141 Args:
142 tree (dict | None): The new right subtree
143
24144
145 Returns:
146 Zipper: A new zipper with the updated right subtree
147 """
148 # Edge Case: Handle None tree
149 if self.tree is None:
150 return self
151 new_tree = {
152 "value": self.tree["value"],
153 "left": self.tree["left"],
154 "right": tree
155 }
156 return Zipper(new_tree, self.breadcrumbs)
157
25158 def up(self) -> "Zipper | None":
26 pass
159 """
160 Move the focus to the parent node.
161
27162
163 Returns:
164 Zipper | None: A new zipper focused on the parent, or None if at root
165 """
166 # Edge Case: Handle when already at root (no breadcrumbs)
167 if not self.breadcrumbs:
168 return None
169
170 # Get the last breadcrumb
171 parent_breadcrumb = self.breadcrumbs[-1]
172
173 # Reconstruct parent tree with current subtree in the correct position
174 parent_tree = parent_breadcrumb["parent"].copy()
175
176 if parent_breadcrumb["side"] == "left":
177 parent_tree["left"] = self.tree
178 parent_tree["right"] = parent_breadcrumb["right"]
179 else: # side == "right"
180 parent_tree["left"] = parent_breadcrumb["left"]
181 parent_tree["right"] = self.tree
182
183 return Zipper(parent_tree, self.breadcrumbs[:-1])
184
28185 def to_tree(self) -> dict:
29 pass
186 """
187 Get the entire tree from the zipper.
188
189
190 Returns:
191 dict: The complete tree
192 """
193 # Navigate back to root to get the full tree
194 current = self
195 while True:
196 parent = current.up()
197 if parent is None:
198 break
199 current = parent
200 return current.tree
201
202 # Handled Edge Cases: None tree input, None left/right child, 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.