LearnViz CompSci
Home / Learn / Data Structures / Treaps

Intermediate Topic 35 of 52 Data Structures

Treaps

Randomized trees combining BST order with heap priorities.

How it works

A treap combines a binary search tree on keys with a heap on randomly assigned priorities. Because the shape is determined by random priorities, the tree behaves like a randomly built BST with expected height O(log n). Insertion adds a node as a leaf and rotates it up until the heap property holds, and deletion rotates it down. Split and merge operations are especially elegant.

Step-by-step walkthrough

  1. Each node has a key ordered like a BST.
  2. It also has a random priority ordered like a max-heap.
  3. Keys left of the root are smaller.
  4. Keys to the right are bigger.
  5. Inserting then rotates the node up until heap order holds, keeping the tree balanced in expectation.

Where it is used

Ordered sets with split and merge for competitive programming, implicit treaps for sequence operations like reversing a subarray, and simple randomized balanced trees use treaps.

Common pitfall

Balance is only probabilistic, so a poor random source or duplicated priorities can cause deep trees. Priorities must be assigned once and kept. The extra priority field costs memory. Recursion depth is usually small, but an adversary who knows the random seed could degrade it.

Where it fits in the course

Treaps is topic 35 of 52 in Data Structures, rated Intermediate. It follows Splay Trees. The next topic is Sparse Tables.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Treaps?
Randomized trees combining BST order with heap priorities. It belongs to Data Structures and is an intermediate-level topic.
Where do people use Treaps?
Ordered sets with split and merge for competitive programming, implicit treaps for sequence operations like reversing a subarray, and simple randomized balanced trees use treaps.
What is the most common mistake with Treaps?
Balance is only probabilistic, so a poor random source or duplicated priorities can cause deep trees.
What should I learn before this topic?
Start with Splay Trees, the topic just before it in Data Structures.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Data Structures quiz.
Is Treaps hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Treaps?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Treaps?
Splay Trees, Sparse Tables, Bloom Filters are closely connected. The full list is in the related topics section of this page.
What should I study after Treaps?
Continue with Sparse Tables, the next topic in Data Structures.

Related topics