Python icon indicating copy to clipboard operation
Python copied to clipboard

treap not working expected when splitting on value that existed

Open vbvg2008 opened this issue 4 years ago • 4 comments

Repository commit

master

Python version (python --version)

any

Dependencies version (pip freeze)

any

Expected behavior

File: data_structures/binary_tree/treap.py

according to the docstring of treap's split function below, when we split a treap into two, the left treap should contain values less than split value, and the right should contain values greater or equal than split value.

def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
    """
    We split current tree into 2 trees with value:
    Left tree contains all values less than split value.
    Right tree contains all values greater or equal, than split value
    """

Which means the expected return of following code should be:

root = interact_treap(None, "+1")
r1, r2 = split(root, 1)  # r1 should be None,  r2 should have value 1.

Actual behavior

However, the actual behavior of the same code is the opposite:

root = interact_treap(None, "+1")
r1, r2 = split(root, 1)  # r1 is 1, r2 is None when running the code

This can be fixed by either changing the docstring or by using <= instead of < in the if condition within split function.

vbvg2008 avatar Oct 29 '22 19:10 vbvg2008

additional note: once you update the split function, erase function needs to change accordingly too, as it is currently relying on the wrong behavior of split

vbvg2008 avatar Oct 30 '22 02:10 vbvg2008

Solved the issue through the request #7899. Changed the docstring according to the standard implementation found on resources available online. More details in the pull request description. I suggest merging the request, or please provide feedback in case it is not merge-able. Also, please add hacktoberfest-accepted label, in case it is okay for merging :wink::smile:

utpalsavliya avatar Nov 28 '22 16:11 utpalsavliya

@priya-sundaram-dev Could #14762 fix this issue?

cclauss avatar Sep 10 '26 13:09 cclauss

@cclauss Partially — #14762 fixes half of it.

Changing split to value <= root.value makes split match its docstring (equal values now land in the right subtree). I verified it on the exact case in this issue:

split(treap, 4)  # 4 exists in the tree
# before: left=[0,2,3,4,4]  right=[5,16,17,19]   ← 4s wrongly in LEFT
# after:  left=[0,2,3]      right=[4,4,5,16,17,19] ← matches docstring

But that one-line change breaks erase, which was written to rely on the old (equal-goes-left) behavior — precisely what @vbvg2008 flagged in 2022 above. With #14762 as-is, the existing interact_treap doctest fails:

-0  → Expected 1,2,...  Got 0,1,2,...   (0 not erased)
-4  → Expected 1,2,3,5,...  Got 0,1,2,4,4,4,4,5,...

erase needs to be updated in the same PR:

    left, right = split(root, value)
    _, right = split(right, value + 1)
    return merge(left, right)

With both changes, split matches its docstring and all doctests pass. So #14762 should close this only after it also patches erase (or a companion PR does). Happy to leave this note on #14762 for the author.

priya-sundaram-dev avatar Sep 10 '26 13:09 priya-sundaram-dev