fix: correct treap split() comparison to match docstring behavior - #14762
Conversation
…gorithms#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 TheAlgorithms#7854
Closing this pull request as invalid@maitriupadhyay03-cell, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines. If you're facing any problem on how to mark a checkbox, please read the following instructions:
NOTE: Only |
|
Thanks for this @maitriupadhyay03-cell — the One thing to finish before it can close #7854: this one-liner breaks Updating def erase(root: Node | None, value: int) -> Node | None:
left, right = split(root, value)
_, right = split(right, value + 1)
return merge(left, right)With both |
Adjust split logic to include the value in the right subtree.
|
Nice — that's exactly it. ✅ Pulled your branch and ran |
Describe your change:
The
split()function indata_structures/binary_tree/treap.pyuses a strict<comparison, but the docstring states:This means when splitting by a value that already exists in the treap, the existing node ends up in the LEFT subtree instead of the RIGHT subtree, which contradicts the documented behavior.
Fix: Changed
elif value < root.value:toelif value <= root.value:so that equal values go to the right subtree as documented.Fixes #7854
Checklist: