Skip to content

Commit 3d11d0c

Browse files
fix: correct treap split() comparison to match docstring behavior (#14762)
* fix: correct treap split() to match docstring for equal values (#7854) The split() function in treap.py uses `<` comparison but the docstring states that the right subtree should contain values "greater or equal" to the split value. This fix changes `elif value < root.value:` to `elif value <= root.value:` so that equal values go to the right subtree as documented. Fixes #7854 * Fix typo in Treap node docstringfix: remove accidental typo in Node class docstring * Fix erase logic in treap.py Adjust split logic to include the value in the right subtree. --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 28dafc1 commit 3d11d0c

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

data_structures/binary_tree/treap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
4141
"""
4242
if root is None or root.value is None: # None tree is split into 2 Nones
4343
return None, None
44-
elif value < root.value:
44+
elif value <= root.value:
4545
"""
4646
Right tree's root will be current node.
4747
Now we split(with the same value) current node's left son
@@ -101,8 +101,8 @@ def erase(root: Node | None, value: int) -> Node | None:
101101
Split all nodes with values greater into right.
102102
Merge left, right
103103
"""
104-
left, right = split(root, value - 1)
105-
_, right = split(right, value)
104+
left, right = split(root, value)
105+
_, right = split(right, value + 1)
106106
return merge(left, right)
107107

108108

0 commit comments

Comments
 (0)