Kratos

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
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
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
628
29 Returns:
30 A new Zipper instance focused on the root of the tree
31 """
32 # Edge Case: Handle None input
33 if tree is None:
34 return None
35 return Zipper(tree.copy())
36
737 def value(self) -> int:
8 pass
38 """
39 Get the value of the focus node.
40
941
42 Returns:
43 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: The new value for the focus node
57
58
59 Returns:
60 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 = self.tree.copy()
66 new_tree["value"] = value
67 return Zipper(new_tree, self.breadcrumbs)
68
1369 def left(self) -> "Zipper | None":
14 pass
70 """
71 Move the focus to the left child.
72
1573
74 Returns:
75 A new Zipper focused on the left child, or None if no left child exists
76 """
77 # Edge Case: Handle None tree
78 if self.tree is None or self.tree["left"] is None:
79 return None
80
81 # Create breadcrumb to remember how to get back to this node
82 breadcrumb = {
83 "parent": self.tree,
84 "side": "left",
85 "sibling": self.tree["right"]
86 }
87
88 return Zipper(self.tree["left"], self.breadcrumbs + [breadcrumb])
89
1690 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
91 """
92 Set the left child of the focus node.
93
94
95 Args:
96 tree: The new left subtree (or None to remove the left child)
97
1898
99 Returns:
100 A new Zipper with the updated left child
101 """
102 # Edge Case: Handle None tree
103 if self.tree is None:
104 return self
105 new_tree = self.tree.copy()
106 new_tree["left"] = tree
107 return Zipper(new_tree, self.breadcrumbs)
108
19109 def right(self) -> "Zipper | None":
20 pass
110 """
111 Move the focus to the right child.
112
21113
114 Returns:
115 A new Zipper focused on the right child, or None if no right child exists
116 """
117 # Edge Case: Handle None tree
118 if self.tree is None or self.tree["right"] is None:
119 return None
120
121 # Create breadcrumb to remember how to get back to this node
122 breadcrumb = {
123 "parent": self.tree,
124 "side": "right",
125 "sibling": self.tree["left"]
126 }
127
128 return Zipper(self.tree["right"], self.breadcrumbs + [breadcrumb])
129
22130 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
131 """
132 Set the right child of the focus node.
133
134
135 Args:
136 tree: The new right subtree (or None to remove the right child)
137
24138
139 Returns:
140 A new Zipper with the updated right child
141 """
142 # Edge Case: Handle None tree
143 if self.tree is None:
144 return self
145 new_tree = self.tree.copy()
146 new_tree["right"] = tree
147 return Zipper(new_tree, self.breadcrumbs)
148
25149 def up(self) -> "Zipper | None":
26 pass
150 """
151 Move the focus to the parent node.
152
27153
154 Returns:
155 A new Zipper focused on the parent, or None if already at the root
156 """
157 # Edge Case: Handle when already at root (no breadcrumbs)
158 if not self.breadcrumbs:
159 return None
160
161 # Get the last breadcrumb
162 parent_info = self.breadcrumbs[-1]
163
164 # Reconstruct the parent with the current tree in the correct position
165 parent = parent_info["parent"].copy()
166 if parent_info["side"] == "left":
167 parent["left"] = self.tree
168 parent["right"] = parent_info["sibling"]
169 else: # parent_info["side"] == "right"
170 parent["right"] = self.tree
171 parent["left"] = parent_info["sibling"]
172
173 return Zipper(parent, self.breadcrumbs[:-1])
174
28175 def to_tree(self) -> dict:
29 pass
176 """
177 Get the complete tree from the zipper.
178
179
180 Returns:
181 The complete binary tree
182 """
183 # Navigate back to the root to get the complete tree
184 current = self
185 while True:
186 parent = current.up()
187 if parent is None:
188 break
189 current = parent
190 return current.tree
191
192 # Handled Edge Cases: None tree input, None left/right child, Already at root when moving 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.