Home/Trees/Treap
Search algorithmsCTRL K

Treap

· BST by key, heap by random priority
/trees/treap
NODES
1/ 12
in the tree
ROTATIONS
0
so far
HEIGHT
1
levels, root included
EXPECTED
1.4
≈ 1.4 log₂ n for a random treap
COMPARISONS
0
key against key
comparingpathnew noderotating
12 keys · seed 7 · 91 steps
step 0 / 90
Keys12
treap.ts
1
function insert(node: Node | null, key: number): Node {
2
  if (node === null) return { key, priority: Math.random(), left: null, right: null };
3
  if (key < node.key) {
4
    node.left = insert(node.left, key);
5
    if (node.left.priority > node.priority) return rotateRight(node);
6
  } else {
7
    node.right = insert(node.right, key);
8
    if (node.right.priority > node.priority) return rotateLeft(node);
9
  }
10
  return node;
11
}
CURRENT STEP
line 2

The treap is empty: 24 with random priority 45 becomes the root.

// how it works

Understanding Treap

treap · a coin flip per node keeps the tree balanced
01

The idea

A treap gives every node a key and a random priority and keeps two rules at once: the keys form a binary search tree and the priorities form a max-heap. Together they pin the shape down completely: the treap is exactly the BST you would get by inserting the keys in decreasing priority order.

Since the priorities are random, that insertion order is random, and a random-order BST has expected height O(log n) whatever order the keys really arrived in. Insertion is a BST insert followed by rotations up while the new node's priority beats its parent's, with no heights or colours to maintain.

02

The four stages

1
descendby key, like a plain BST
2
attachas a leaf with a random priority
3
rotate upwhile the priority beats the parent's
4
settlekeys still ordered, priorities a heap
03

Complexity

best
O(log n)expected height 1.4 log₂ n
average
O(log n)expected, over the random priorities
worst
O(n)an unlucky priority draw; vanishingly rare
space
O(n)a priority per node
04

Versus its siblings

HEIGHT · 1 000 000 KEYS INSERTED IN SORTED ORDER
treap
40
avl
29
red-black
40
skip list
40
plain bst
1M
the treap and the skip list are random; their heights are typical, not guaranteed
05

Pseudocode

INSERT(node, key)
  if node is null: return a new node with key and a random priority
  insert into the left or right subtree by key, as in a BST
  if that child's priority is now larger than node's: rotate it up (right rotation for the left child, left for the right)
  return the root of this subtree
06

When to use

Ordered sets with split and merge: text editor buffers, interval sets, persistent versions, because a treap splits by key in O(log n).
When AVL or red-black code is too much: a treap is the shortest balanced tree to write.
07

Pitfalls

Priorities must come from a good random source; a predictable one lets an adversary build a chain.
Equal priorities break the heap invariant; use a wide range or break ties by key.
The expected bound is over the randomness, not the input; there is no worst-case guarantee like AVL's.
08

History

Cecilia Aragon and Raimund Seidel introduced treaps in 1989 as randomized search trees, with the observation that a random priority is all the balance information a tree needs. Jean Vuillemin's Cartesian trees from 1980 are the same structure with priorities given by position rather than chance.

Back to Trees