ArticleslgStudy

computer science

Treap

Treap is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Treap rather than just read about it. In short: In computer science, the treap and the randomized binary search tree are two closely related forms of binary search tree data structures that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of insertions and deletions of keys, the shape of the tree is a random variable with the same probability distribution as a random binary tree; in particular, with high probabil…

Treap — main illustration
Treap — illustration

Key takeaways

  • Treap belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Treap to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Treap from memory before moving on to harder problems.

Reference excerpt

In computer science, the treap and the randomized binary search tree are two closely related forms of binary search tree data structures that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of insertions and deletions of keys, the shape of the tree is a random variable with the same probability distribution as a random binary tree; in particular, with high probability its height is proportional to the logarithm of the number of keys, so that each search, insertion, or deletion operation takes logarithmic time to perform.

Description

The treap was first described by Raimund Seidel and Cecilia R. Aragon in 1989; its name is a portmanteau of tree and heap. It is a Cartesian tree in which each key is given a (randomly chosen) numeric priority. As with any binary search tree, the inorder traversal order of the nodes is the same as the sorted order of the keys. The structure of the tree is determined by the requirement that it be heap-ordered: that is, the priority number for any non-leaf node must be greater than or equal to the priority of its children. Thus, as with Cartesian trees more generally, the root node is the maximum-priority node, and its left and right subtrees are formed in the same manner from the subsequences of the sorted order to the left and right of that node. An equivalent way of describing the treap is that it could be formed by inserting the nodes highest priority-first into a binary search tree without doing any rebalancing. Therefore, if the priorities are independent random numbers (from a distribution over a large enough space of possible priorities to ensure that two nodes are very unlikely to have the same priority) then the shape of a treap has the same probability distribution as the shape of a random binary search tree, a search tree formed by inserting the nodes without rebalancing in a randomly chosen insertion order. Because random binary search trees are known to have logarithmic height with high probability, the same is true for treaps. This mirrors the binary search tree argument that quicksort runs in expected O ( n log ⁡ n ) {\displaystyle O(n\log n)} time. If binary search trees are solutions to the dynamic problem version of sorting, then Treaps correspond specifically to dynamic quicksort where priorities guide pivot choices. Aragon and Seidel also suggest assigning higher priorities to frequently accessed nodes, for instance by a process that, on each access, chooses a random number and replaces the priority of the node with that number if it is higher than the previous priority. This modification would cause the tree to lose its random shape; instead, frequently accessed nodes would be more likely to be near the root of the tree, causing searches for them to be faster. Naor and Nissim describe an application in maintaining authorization certificates in public-key cryptosystems.

Operations

Basic operations Treaps support the following basic operations:

To search for a given key value, apply a standard binary search algorithm in a binary search tree, ignoring the priorities. To insert a new key x into the treap, generate a random priority y for x. Binary search for x in the tree, and create a new node at the leaf position where the binary search determines a node for x should exist. Then, as long as x is not the root of the tree and has a larger priority number than its parent z, perform a tree rotation that reverses the parent-child relation between x and z. To delete a node x from the treap, if x is a leaf of the tree, simply remove it. If x has a single child z, remove x from the tree and make z be the child of the parent of x (or make z the root of the tree if x had no parent). Finally, if x has two children, swap its position in the tree with the position of its immediate successor z in the sorted order, resulting in one of the previous cases. In this final case, the swap may violate the heap-ordering property for z, so additional rotations may need to be performed to restore this property.

Building a treap To build a treap we can simply insert n values in the treap where each takes O ( log ⁡ n ) {\displaystyle O(\log n)} time. Therefore a treap can be built in O ( n log ⁡ n ) {\displaystyle O(n\log n)} time from a list values.

Bulk operations In addition to the single-element insert, delete and lookup operations, several fast "bulk" operations have been defined on treaps: union, intersection and set difference. These rely on two helper operations, split and join.

To split a treap into two smaller treaps, those smaller than key x, and those larger than key x, insert x into the treap with maximum priority—larger than the priority of any node in the treap. After this insertion, x will be the root node of the treap, all values less than x will be found in the left subtreap, and all values greater than x will be found in the right subtreap. This costs as much as a single insertion into the treap. Joining two treaps that are the product of a former split, one can safely assume that the greatest value in the first treap is less than the smallest value in the second treap. Create a new node with value x, such that x is larger than this max-value in the first treap and smaller than the min-value in the second treap, assign it the minimum priority, then set its left child to the first heap and its right child to the second heap. Rotate as necessary to fix the heap order. After that, it will be a leaf node, and can easily be deleted. The result is one treap merged from the two original treaps. This is effectively "undoing" a split, and costs the same. More generally, the join operation can work on two treaps and a key with arbitrary priority (i.e., not necessary to be the highest).

The join algorithm is as follows:

function join(L, k, R) if prior(k, k(L)) and prior(k, k(R)) return Node(L, k, R) if prior(k(L), k(R)) return Node(left(L), k(L), join(right(L), k, R)) return Node(join(L, k, left(R)), k(R), right(R))

The split algorithm is as follows:

… excerpt ends here. Continue reading the full article.

Illustrations

Treap illustration
Treap: A treap with alphabetic key and numeric max heap order
A treap with alphabetic key and numeric max heap order
Treap: Join performed on treaps 
  
    
      
        
          T
          
            1
          
        
      
    
    {\displaystyle T_{1}}
  
 and 
  
    
      
        
          T
          
            2
          
        
      
    
    {\displaystyle T_{2}}
  
. Right child of 
  
    
      
        
          T
          
            1
          
        
      
    
    {\displaystyle T_{1}}
  
 after the join is defined as a join of its former right child and 
  
    
      
        
          T
          
            2
          
        
      
    
    {\displaystyle T_{2}}
  
.
Join performed on treaps T 1 {\displaystyle T_{1}} and T 2 {\displaystyle T_{2}} . Right child of T 1 {\displaystyle T_{1}} after the join is defined as a join of its former right child and T 2 {\displaystyle T_{2}} .
Treap: To split 
  
    
      
        T
      
    
    {\displaystyle T}
  
 by 
  
    
      
        x
      
    
    {\displaystyle x}
  
, recursive split call is done to either left or right child of 
  
    
      
        T
      
    
    {\displaystyle T}
  
.
To split T {\displaystyle T} by x {\displaystyle x} , recursive split call is done to either left or right child of T {\displaystyle T} .

Worked examples

Example 1 — a first encounter with Treap

Start with the simplest possible case. Write down what Treap claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Treap before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Treap ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Treap

In research
Treap appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Treap in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Treap is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary trees, Heaps (data structures), Probabilistic data structures, so understanding it makes those chapters shorter.
In everyday life
Look for Treap outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Treap” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Treap in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Treap means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Treap out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Treap in simple terms?

In computer science, the treap and the randomized binary search tree are two closely related forms of binary search tree data structures that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of insertions and deletions of keys, the shape of the tre…

Why does Treap matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Treap?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Treap.

Tags

  • Binary trees
  • Heaps (data structures)
  • Probabilistic data structures
  • Search trees

Keep exploring