Skip to main content
Back to the DSA atlas
GraphsTrees Data structure Advanced

Treap

Combine binary-search-tree ordering with heap priorities to get a balanced randomized tree with elegant split and merge operations.

Trees is nested under Graphs , so techniques in the broader family usually still apply here.

treebstrandomizedrare

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

40p=7020p=8560p=6010p=4030p=6550p=5080p=35

key 40

priority 70

key 20

priority 85

key 60

priority 60

key 10

priority 40

key 30

priority 65

key 50

priority 50

key 80

priority 35

BST by key

Keys preserve inorder traversal: left keys smaller, right keys larger.

Family

Graphs → Trees

Hierarchies and constrained graphs. A tree is a connected acyclic graph.

Builds on

3 topics

Read these first if you want the surrounding context.

Unlocks

0 next topics

Use these follow-ups when you want to apply the current idea in a richer setting.

Learning paths

1

This topic appears in curated progressions so you can keep moving without guessing the next step.

Problem

A plain binary search tree is elegant, but it can become skewed and degrade to O(n)O(n) per operation.

Balanced BSTs fix that, but many of them are implementation-heavy. A treap gets balance in a much simpler way: add randomness.

What a treap is

A treap is simultaneously:

  • a binary search tree by key
  • a heap by random priority

Each node stores:

  • key
  • priority

The inorder traversal is sorted by key. The priorities satisfy heap order.

That dual constraint is what keeps the tree balanced in expectation.

Why it works

Imagine inserting keys into a BST in the order of decreasing priority. You would get exactly the same tree shape as the treap.

Because priorities are random, the expected height stays logarithmic.

So a treap is basically a randomized balanced BST.

Rotations or split/merge

Treaps are often implemented in one of two styles:

Rotation style

Insert like a BST, then rotate upward while the heap priority is violated.

Split/merge style

Use two beautiful primitives:

  • split(root, key) separates keys < key from keys >= key
  • merge(left, right) combines two treaps when every key in left is smaller

This split/merge form is why treaps are beloved in competitive programming. The code is short and expressive.

Complexity

OperationExpected time
SearchO(logn)O(\log n)
InsertO(logn)O(\log n)
DeleteO(logn)O(\log n)
Split / mergeO(logn)O(\log n)

The guarantee is expected, not worst-case deterministic.

When treaps are useful

Treaps shine when you want:

  • a balanced ordered set or map
  • order statistics or implicit sequence tricks
  • split/merge-friendly tree operations
  • something lighter than implementing a red-black tree from scratch

They are rarer in interviews, but they appear often in algorithmic programming because the abstraction is so clean.

Key takeaways

  • A treap is a BST by key and a heap by random priority at the same time
  • Random priorities make the height logarithmic in expectation
  • Split and merge are the signature operations that make treaps powerful
  • This is a rare structure, but it teaches a great design lesson: randomness can simplify balance dramatically

Practice problems

ProblemDifficultyKey idea
Implicit Treap tutorials🔴 HardSplit/merge sequence operations
Ordered Set🟡 MediumBalanced BST concepts apply
Treap references🔴 HardRandomized balancing and splits

Relation to other topics

  • Binary search tree provides the ordering rule
  • Heap provides the priority rule
  • Treap is one of the cleanest examples of combining two simpler structures into one richer one

Build on these first

These topics supply the mental model or underlying structure that this page assumes.

Related directions

These topics live nearby conceptually, even if they are not direct prerequisites.

More from Trees

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

Ordered structures

See how order can come from sorted arrays, search trees, skip lists, disk-friendly indexes, heaps, and prefix trees.

From the blog

Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.