Data Structures19 sections · 729 units
Open in Course

Treap Operations - Split

Dividing by key

Split divides a treap into two treaps: one with keys <x< x, one with keys x\geq x.

function split(node, x)
    if node is null then
        return (null, null)

    if node.key < x then
        (left, right) := split(node.right, x)
        node.right := left
        return (node, right)
    else
        (left, right) := split(node.left, x)
        node.left := right
        return (left, node)

Time: O(logn)O(\log n) expected. The recursion follows a path from root to leaf, and expected path length is O(logn)O(\log n).