A treap is a binary search tree ordered by keys and simultaneously a binary heap ordered by priorities. Independent random priorities give logarithmic expected search depth. An implicit treap uses subtree sizes to address sequence positions instead of storing explicit keys.
An implicit treap represents a sequence by the inorder traversal of a treap, without storing explicit positional search keys. Each node stores its subtree size. If the left subtree has size , the root has local index ; smaller indices descend left, while larger indices descend right after subtracting .
An empty tree has size zero; splitting it at zero returns two empty trees. A split at returns the first entries and the remainder. If , split the left subtree and attach its returned suffix as the root's new left child. Otherwise split the right subtree at and attach its returned prefix as the root's new right child. Recompute sizes on the return path. A merge assumes that every entry of its first operand precedes every entry of its second; choose the higher-priority root and recursively merge the adjoining inner subtrees.
Both operations preserve inorder traversal order and heap order on priorities. They permit insertion, deletion or moving a contiguous segment through a few splits and merges. Independent identically distributed continuous random priorities give the underlying randomized search-tree balance and expected logarithmic operation cost; a particular tree can still be tall.[ref]

Articles by others on the same topic (0)

There are currently no matching articles.