Merry

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 and allows
6 traversal and modification operations.
7 """
8
9 def __init__(self, tree, path=None):
10 """
11 Initialize a Zipper.
12
13 Args:
14 tree (dict): The tree structure
15 path (list): Path from root to current focus node
16 """
17 # Edge Case: Handle None tree by initializing with empty structure
18 if tree is None:
19 tree = {"value": 0, "left": None, "right": None}
20 self.tree = tree
21 # Edge Case: Initialize path as empty list if not provided
22 self.path = path or []
23
324 @staticmethod
425 def from_tree(tree: dict) -> "Zipper":
5 pass
6
26 """
27 Create a zipper from a tree, with focus on the root node.
28
29 Args:
30 tree (dict): The tree to create a zipper for
31
32 Returns:
33 Zipper: A new zipper focused on the root of the tree
34 """
35 # Edge Case: Handle None tree input
36 if tree is None:
37 return Zipper(None)
38 return Zipper(tree)
39
40 def _get_focus_node(self):
41 """
42 Get the current focus node based on the path.
43
44 Returns:
45 dict: The focus node
46 """
47 node = self.tree
48 for direction, parent in self.path:
49 node = node[direction]
50 return node
51
752 def value(self) -> int:
8 pass
9
53 """
54 Get the value of the focus node.
55
56 Returns:
57 int: The value of the focus node
58 """
59 return self._get_focus_node()["value"]
60
1061 def set_value(self, value: int) -> "Zipper":
11 pass
12
62 """
63 Set the value of the focus node, returning a new zipper.
64
65 Args:
66 value (int): The new value
67
68 Returns:
69 Zipper: A new zipper with updated value
70 """
71 # Edge Case: Handle None value by using 0 as default
72 if value is None:
73 value = 0
74
75 new_tree = self._copy_tree()
76 node = new_tree
77 for direction, parent in self.path:
78 node = node[direction]
79 node["value"] = value
80 return Zipper(new_tree, list(self.path))
81
1382 def left(self) -> "Zipper | None":
14 pass
15
83 """
84 Move the focus to the left child, returning a new zipper.
85
86 Returns:
87 Zipper | None: A new zipper focused on the left child, or None if no left child
88 """
89 focus = self._get_focus_node()
90 # Edge Case: Return None if no left child exists
91 if focus["left"] is None:
92 return None
93 new_path = list(self.path)
94 new_path.append(("left", focus))
95 return Zipper(self.tree, new_path)
96
1697 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
98 """
99 Set the left child of the focus node, returning a new zipper.
100
101 Args:
102 tree (dict | None): The new left subtree
103
104 Returns:
105 Zipper: A new zipper with updated left child
106 """
107 new_tree = self._copy_tree()
108 node = new_tree
109 for direction, parent in self.path:
110 node = node[direction]
111 node["left"] = tree
112 return Zipper(new_tree, list(self.path))
113
19114 def right(self) -> "Zipper | None":
20 pass
21
115 """
116 Move the focus to the right child, returning a new zipper.
117
118 Returns:
119 Zipper | None: A new zipper focused on the right child, or None if no right child
120 """
121 focus = self._get_focus_node()
122 # Edge Case: Return None if no right child exists
123 if focus["right"] is None:
124 return None
125 new_path = list(self.path)
126 new_path.append(("right", focus))
127 return Zipper(self.tree, new_path)
128
22129 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
130 """
131 Set the right child of the focus node, returning a new zipper.
132
133 Args:
134 tree (dict | None): The new right subtree
135
136 Returns:
137 Zipper: A new zipper with updated right child
138 """
139 new_tree = self._copy_tree()
140 node = new_tree
141 for direction, parent in self.path:
142 node = node[direction]
143 node["right"] = tree
144 return Zipper(new_tree, list(self.path))
145
25146 def up(self) -> "Zipper | None":
26 pass
27
147 """
148 Move the focus to the parent, returning a new zipper.
149
150 Returns:
151 Zipper | None: A new zipper focused on the parent, or None if at root
152 """
153 # Edge Case: Return None if already at root (no parent)
154 if not self.path:
155 return None
156 new_path = list(self.path)
157 new_path.pop()
158 return Zipper(self.tree, new_path)
159
28160 def to_tree(self) -> dict:
29 pass
161 """
162 Get the tree representation from the zipper.
163
164 Returns:
165 dict: The tree structure
166 """
167 return self.tree
168
169 def _copy_tree(self):
170 """
171 Create a deep copy of the tree.
172
173 Returns:
174 dict: A deep copy of the tree
175 """
176 def _deep_copy(node):
177 if node is None:
178 return None
179 return {
180 "value": node["value"],
181 "left": _deep_copy(node["left"]),
182 "right": _deep_copy(node["right"])
183 }
184 return _deep_copy(self.tree)
185
186 # Handled Edge Cases: None tree input, None value input, no left child, no 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.