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 A zipper contains a data structure and a pointer (focus) into that structure.
6 It supports moving through the tree and modifying it in a functional way.
7
8 Attributes:
9 _focus: The current node that is focused
10 _path: A list of tuples representing the path from root to focus,
11 each tuple contains (parent_node, direction) where direction is 'left' or 'right'
12 """
13
14 def __init__(self, focus: dict, path: list):
15 """
16 Initialize a Zipper.
17
18 Args:
19 focus: The node that is currently focused
20 path: The path from root to this node
21 """
22 self._focus = focus
23 self._path = path
24
325 @staticmethod
426 def from_tree(tree: dict) -> "Zipper":
5 pass
6
27 """
28 Create a zipper from a binary tree.
29
30 Args:
31 tree: A binary tree represented as a dict with keys "value", "left", and "right"
32 where left and right are either dicts or None
33
34 Returns:
35 A new Zipper instance focused on the root of the tree
36
37 # Handled Edge Cases: None - this is a static method that creates a new zipper
38 """
39 return Zipper(tree, [])
40
741 def value(self) -> int:
8 pass
9
42 """
43 Get the value of the focus node.
44
45 Returns:
46 The value of the currently focused node
47
48 # Handled Edge Cases: None - assumes focus is valid
49 """
50 return self._focus["value"]
51
1052 def set_value(self, value: int) -> "Zipper":
11 pass
12
53 """
54 Set the value of the focus node, returning a new zipper.
55
56 Args:
57 value: The new value for the focus node
58
59 Returns:
60 A new Zipper with the updated value
61
62 # Handled Edge Cases: None - assumes focus is valid
63 """
64 new_focus = {
65 "value": value,
66 "left": self._focus["left"],
67 "right": self._focus["right"]
68 }
69 return Zipper(new_focus, self._path.copy())
70
1371 def left(self) -> "Zipper | None":
14 pass
15
72 """
73 Move the focus to the left child of the current node.
74
75 Returns:
76 A new Zipper focused on the left child, or None if there is no left child
77
78 # Edge Case: No left child - returns None
79 """
80 if self._focus["left"] is None:
81 return None
82
83 new_path = self._path.copy()
84 new_path.append((self._focus, "left"))
85 return Zipper(self._focus["left"], new_path)
86
1687 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
88 """
89 Set the left child of the focus node, returning a new zipper.
90
91 Args:
92 tree: A binary tree (dict) or None to remove the left child
93
94 Returns:
95 A new Zipper with the updated left child
96
97 # Handled Edge Cases: None - assumes focus is valid
98 """
99 new_focus = {
100 "value": self._focus["value"],
101 "left": tree,
102 "right": self._focus["right"]
103 }
104 return Zipper(new_focus, self._path.copy())
105
19106 def right(self) -> "Zipper | None":
20 pass
21
107 """
108 Move the focus to the right child of the current node.
109
110 Returns:
111 A new Zipper focused on the right child, or None if there is no right child
112
113 # Edge Case: No right child - returns None
114 """
115 if self._focus["right"] is None:
116 return None
117
118 new_path = self._path.copy()
119 new_path.append((self._focus, "right"))
120 return Zipper(self._focus["right"], 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, returning a new zipper.
125
126 Args:
127 tree: A binary tree (dict) or None to remove the right child
128
129 Returns:
130 A new Zipper with the updated right child
131
132 # Handled Edge Cases: None - assumes focus is valid
133 """
134 new_focus = {
135 "value": self._focus["value"],
136 "left": self._focus["left"],
137 "right": tree
138 }
139 return Zipper(new_focus, self._path.copy())
140
25141 def up(self) -> "Zipper | None":
26 pass
27
142 """
143 Move the focus to the parent of the current node.
144
145 Returns:
146 A new Zipper focused on the parent, or None if there is no parent (root)
147
148 # Edge Case: No parent (at root) - returns None
149 """
150 if not self._path:
151 return None
152
153 parent, direction = self._path[-1]
154 new_focus = parent.copy()
155
156 # Update the parent's child reference to match the current focus
157 if direction == "left":
158 new_focus["left"] = self._focus
159 else: # direction == "right"
160 new_focus["right"] = self._focus
161
162 return Zipper(new_focus, self._path[:-1])
163
28164 def to_tree(self) -> dict:
29 pass
165 """
166 Get the complete binary tree from the zipper.
167
168 Returns:
169 The complete binary tree as a dict
170
171 # Handled Edge Cases: None - reconstructs tree from current focus and path
172 """
173 # If we're at the root, just return the focus
174 if not self._path:
175 return self._focus
176
177 # Otherwise, we need to rebuild the tree by going up to the root
178 current_zipper = self
179 while current_zipper._path:
180 current_zipper = current_zipper.up()
181
182 return current_zipper._focus
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.