Immune

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 navigation and modification operations while preserving the tree structure.
7 """
8
9 def __init__(self, tree, path):
10 """
11 Initialize a Zipper instance.
12
13 Args:
14 tree (dict): The current focus node of the tree
15 path (list): A list of breadcrumb dictionaries representing the path from root to focus
16 """
17 self.tree = tree
18 self.path = path
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 (dict): A binary tree represented as nested dictionaries with "value", "left", and "right" keys
27
28 Returns:
29 Zipper: A new zipper with focus on the root node
30 """
31 # Edge Case: Handle None tree input
32 if tree is None:
33 return None
34 return Zipper(tree, [])
35
736 def value(self) -> int:
8 pass
9
37 """
38 Get the value of the focus node.
39
40 Returns:
41 int: The value of the focus node
42
43 Raises:
44 AttributeError: If the focus node is None
45 """
46 # Edge Case: Handle None tree (should not happen in normal usage but added for robustness)
47 if self.tree is None:
48 raise AttributeError("Cannot get value of None node")
49 return self.tree["value"]
50
1051 def set_value(self, value: int) -> "Zipper":
11 pass
12
52 """
53 Set the value of the focus node.
54
55 Args:
56 value (int): The new value for the focus node
57
58 Returns:
59 Zipper: A new zipper with the updated focus node
60 """
61 # Edge Case: Handle None tree
62 if self.tree is None:
63 return self
64 new_tree = {"value": value, "left": self.tree["left"], "right": self.tree["right"]}
65 return Zipper(new_tree, self.path)
66
1367 def left(self) -> "Zipper | None":
14 pass
15
68 """
69 Move the focus to the left child of the current node.
70
71 Returns:
72 Zipper | None: A new zipper focused on the left child, or None if no left child exists
73 """
74 # Edge Case: Handle None tree
75 if self.tree is None or self.tree["left"] is None:
76 return None
77
78 # Create breadcrumb to store current node context when moving down
79 breadcrumb = {
80 "parent": self.tree,
81 "side": "left",
82 "right": self.tree["right"]
83 }
84
85 return Zipper(self.tree["left"], self.path + [breadcrumb])
86
1687 def set_left(self, tree: dict | None) -> "Zipper":
17 pass
18
88 """
89 Set the left child of the focus node.
90
91 Args:
92 tree (dict | None): The new left subtree or None
93
94 Returns:
95 Zipper: A new zipper with the updated focus node
96 """
97 # Edge Case: Handle None tree
98 if self.tree is None:
99 return self
100 new_tree = {"value": self.tree["value"], "left": tree, "right": self.tree["right"]}
101 return Zipper(new_tree, self.path)
102
19103 def right(self) -> "Zipper | None":
20 pass
21
104 """
105 Move the focus to the right child of the current node.
106
107 Returns:
108 Zipper | None: A new zipper focused on the right child, or None if no right child exists
109 """
110 # Edge Case: Handle None tree
111 if self.tree is None or self.tree["right"] is None:
112 return None
113
114 # Create breadcrumb to store current node context when moving down
115 breadcrumb = {
116 "parent": self.tree,
117 "side": "right",
118 "left": self.tree["left"]
119 }
120
121 return Zipper(self.tree["right"], self.path + [breadcrumb])
122
22123 def set_right(self, tree: dict | None) -> "Zipper":
23 pass
24
124 """
125 Set the right child of the focus node.
126
127 Args:
128 tree (dict | None): The new right subtree or None
129
130 Returns:
131 Zipper: A new zipper with the updated focus node
132 """
133 # Edge Case: Handle None tree
134 if self.tree is None:
135 return self
136 new_tree = {"value": self.tree["value"], "left": self.tree["left"], "right": tree}
137 return Zipper(new_tree, self.path)
138
25139 def up(self) -> "Zipper | None":
26 pass
27
140 """
141 Move the focus to the parent of the current node.
142
143 Returns:
144 Zipper | None: A new zipper focused on the parent node, or None if already at root
145 """
146 # Edge Case: Handle when already at root (no path)
147 if not self.path:
148 return None
149
150 # Get the most recent breadcrumb
151 breadcrumb = self.path[-1]
152
153 # Reconstruct the parent node with the current tree in the correct position
154 parent_tree = {
155 "value": breadcrumb["parent"]["value"],
156 "left": self.tree if breadcrumb["side"] == "left" else breadcrumb["left"],
157 "right": self.tree if breadcrumb["side"] == "right" else breadcrumb["right"]
158 }
159
160 # Return a new zipper focused on the parent with the rest of the path
161 return Zipper(parent_tree, self.path[:-1])
162
28163 def to_tree(self) -> dict:
29 pass
164 """
165 Get the complete tree from the zipper.
166
167 Returns:
168 dict: The complete binary tree
169 """
170 # Navigate up to the root to get the complete tree
171 current = self
172 while True:
173 parent = current.up()
174 if parent is None:
175 break
176 current = parent
177 return current.tree
178
179 # Handled Edge Cases: None tree input, None focus node, moving when no child exists, moving up from 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.