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.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
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 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:
keypriority
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< keyfrom keys>= keymerge(left, right)combines two treaps when every key inleftis smaller
This split/merge form is why treaps are beloved in competitive programming. The code is short and expressive.
Complexity
| Operation | Expected time |
|---|---|
| Search | |
| Insert | |
| Delete | |
| Split / merge |
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
| Problem | Difficulty | Key idea |
|---|---|---|
| Implicit Treap tutorials | 🔴 Hard | Split/merge sequence operations |
| Ordered Set | 🟡 Medium | Balanced BST concepts apply |
| Treap references | 🔴 Hard | Randomized 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.
Tree Fundamentals
Understand roots, parents, children, depth, height, and traversal orders. A tree is a graph with enough structure to make many problems much simpler.
Binary Search Tree (BST)
A sorted tree structure enabling O(log n) search, insert, and delete. Foundation for balanced trees, sets, and maps.
Heap & Priority Queue
A complete binary tree where every parent is smaller (or larger) than its children. The backbone of Dijkstra, event scheduling, and top-k problems.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
Fenwick Tree (Binary Indexed Tree)
Maintain prefix sums with O(log n) updates and queries using one tiny bit trick. Fenwick trees are the lightest dynamic range-query structure worth knowing.
Segment Tree
Answer range queries and point updates in O(log n). Segment trees are the go-to structure for range sum, range min, and similar problems.
Skip List
Layer multiple forward-pointer lists on top of each other so ordered search behaves like a randomized tree while staying list-based.
More from Trees
Stay in the same family when you want parallel variations of the same mental model.
Tree Fundamentals
Understand roots, parents, children, depth, height, and traversal orders. A tree is a graph with enough structure to make many problems much simpler.
Binary Search Tree (BST)
A sorted tree structure enabling O(log n) search, insert, and delete. Foundation for balanced trees, sets, and maps.
Heap & Priority Queue
A complete binary tree where every parent is smaller (or larger) than its children. The backbone of Dijkstra, event scheduling, and top-k problems.
Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
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.