Questions tagged [treap]

A treap is a form of a binary search tree data structure that maintains a dynamic set of ordered keys and allows binary searches among the keys.

A treap (tree+heap) 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.


Useful links


Related tags

29 questions
25
votes
4 answers

When to use a treap

Can anyone provide real examples of when is the best way to store your data is treap? I want to understand in which situations treap will be better than heaps and tree structures. If it's possible, please provide some examples from real…
user2281439
  • 603
  • 1
  • 11
  • 18
20
votes
8 answers

Why is insertion into my tree faster on sorted input than random input?

Now I've always heard binary search trees are faster to build from randomly selected data than ordered data, simply because ordered data requires explicit rebalancing to keep the tree height at a minimum. Recently I implemented an immutable treap, a…
Juliet
  • 76,873
  • 44
  • 191
  • 224
11
votes
4 answers

Treap with implicit keys

There's a data structure called treap: that's a randomized binary search tree, which is also a heap on randomly generated so-called "priorities". There's a variation of this structure, where keys are implicit, they aren't stored in the tree, but we…
Skiminok
  • 2,521
  • 1
  • 21
  • 29
6
votes
1 answer

Which data structure is most suitable to implement a Dictionary?

I have to write a Dictionary program as a semester project for an undergraduate course on Data Structures and Algorithms, and I am expected to find the most suitable solution (Data Structure) to the problem. I considered using either a hash table or…
سیف خان
  • 171
  • 1
  • 1
  • 18
4
votes
2 answers

When is a treap useful?

In what kind of situation is a treap the optimal data structure to use? I have been searching for answers on this but haven't really found anything concrete. There's another stackoverflow question asking when to use a treap but no real world…
Just some guy
  • 1,729
  • 4
  • 18
  • 27
3
votes
1 answer

How does a treap help to update this ordered queue?

I'm having trouble understanding this solution to a problem on HackerRank. Please see the solution code below, apparently by Kimiyuki Onaka. The problem is: given a list of unique numbers, and m queries of the type, "move the current ith to jth…
גלעד ברקן
  • 21,095
  • 3
  • 19
  • 57
2
votes
1 answer

Generating random priorities for a treap in C++

I am creating a treap, and I want to know, which random number generator is most suitable for generating priorities at insertion. The data set is about 6000 items long. I am modifying an existing template class(largely just declared methods without…
dodekja
  • 403
  • 6
  • 16
2
votes
2 answers

How to generate random priorities for Treap nodes?

In most examples I've seen on the web, it is taken as given that there's some sort of external random number generator that will yield random (distinct!) priorities. From what I recall, there was a method to actually generate the priorities randomly…
devoured elysium
  • 90,453
  • 117
  • 313
  • 521
2
votes
1 answer

Using "Treap" to compare two set

I want to use Treap structure,but I don't familiar with this type of tree well. I have two set and i want to write a method to compare them with Treap. This method should return a value that show the similarity of two set. (My work is retrieve a set…
SahelSoft
  • 586
  • 2
  • 7
  • 18
1
vote
1 answer

Treap Data Structure

The height of Treap is said to be logarithmic in order. But while performing an online insertion for given key (1,2),(1,3),(3,4),(4,5) in order, the height of the treap is of the order of input. So the worst case complexity is turning out to be…
1
vote
1 answer

What is the utility of treap data structure?

I am currently studying advanced data structures and I came across a weird data structure called Treap. I understand what Treap is but I can't seem to find it's utility in a valid use case scenario. Why should you use such a data structure and in…
Vlad
  • 35
  • 7
1
vote
1 answer

What is faster in practice: Treap or Splay tree?

I've learned both Treap and Splay tree and solved few problems using them. In theory, their complexity is O(log n) on average, but in worst-case Treap's complexity is O(n) while Splay tree's is amortized O(log n). In which case does worst case…
NikolaTECH
  • 66
  • 8
1
vote
0 answers

segment fault in some special unknown cases, driving me CRAZY

I've been debugging for a whole day and I really am completely lost. Help! I tested tons of cases locally in Visual Studio. It works fine. But on our school's online judge system, there are several cases reporting RE(exitcode 6) which means a…
laplacecrame
  • 23
  • 1
  • 4
1
vote
0 answers

How this code generates random number?

I am learning treap nowadays and I came to an implementation in which the guy used some weird way to generate random numbers for priority.I am not able to get it.Would anybody mind explaining me how does it work. struct xor_128 { …
1
vote
0 answers

Balanced search tree (Treap) with multiple equal keys

I have to create a balanced search tree with multiple equal keys. I implemented the function I need: #include #include #include typedef struct tip_nod { int key, priority; struct tip_nod *left, *right; } NOD; …
Tandura
  • 684
  • 1
  • 6
  • 16
1
2