Skip to content

Commit d69d82a

Browse files
fix(treap): make inorder() return string to fix thread-unsafe doctest
The inorder() function previously used print() to output values, which made its doctests thread-unsafe (pytest-run-parallel does not support doctests that use print). This fix: - Changes inorder() to return a comma-separated string instead of printing - Updates all interact_treap doctests to assert the returned string value - Adds a doctest to inorder() itself Fixes the CI failure: FAILED data_structures/binary_tree/treap.py::data_structures.binary_tree.treap.interact_treap
1 parent 68519a8 commit d69d82a

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

data_structures/binary_tree/treap.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,16 @@ def erase(root: Node | None, value: int) -> Node | None:
106106
return merge(left, right)
107107

108108

109-
def inorder(root: Node | None) -> None:
109+
def inorder(root: Node | None) -> str:
110110
"""
111-
Just recursive print of a tree
111+
Returns a comma-separated string of tree values in sorted order.
112+
113+
>>> inorder(None)
114+
''
112115
"""
113-
if not root: # None
114-
return
115-
else:
116-
inorder(root.left)
117-
print(root.value, end=",")
118-
inorder(root.right)
116+
if root is None:
117+
return ""
118+
return inorder(root.left) + str(root.value) + "," + inorder(root.right)
119119

120120

121121
def interact_treap(root: Node | None, args: str) -> Node | None:
@@ -126,19 +126,19 @@ def interact_treap(root: Node | None, args: str) -> Node | None:
126126
127127
>>> root = interact_treap(None, "+1")
128128
>>> inorder(root)
129-
1,
129+
'1,'
130130
>>> root = interact_treap(root, "+3 +5 +17 +19 +2 +16 +4 +0")
131131
>>> inorder(root)
132-
0,1,2,3,4,5,16,17,19,
132+
'0,1,2,3,4,5,16,17,19,'
133133
>>> root = interact_treap(root, "+4 +4 +4")
134134
>>> inorder(root)
135-
0,1,2,3,4,4,4,4,5,16,17,19,
135+
'0,1,2,3,4,4,4,4,5,16,17,19,'
136136
>>> root = interact_treap(root, "-0")
137137
>>> inorder(root)
138-
1,2,3,4,4,4,4,5,16,17,19,
138+
'1,2,3,4,4,4,4,5,16,17,19,'
139139
>>> root = interact_treap(root, "-4")
140140
>>> inorder(root)
141-
1,2,3,5,16,17,19,
141+
'1,2,3,5,16,17,19,'
142142
>>> root = interact_treap(root, "=0")
143143
Unknown command
144144
"""

0 commit comments

Comments
 (0)