1

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 what type of problems/conditions treaps are best used?

I find myself much more into using either hash maps, min/max heaps, binary search tree or balanced binary search trees, but I can't tell on why should you use a treap.

Vlad
  • 35
  • 7
  • 1
    a key benefit being simplicity of implementation :). The *priority* aspect of treap nodes allows the treap to be constructed in a similar way to a binary search tree without the requirement for complex rotation functions that entail a more complex data structure used for a similar purpose like an AVL tree. – Srini Mar 29 '18 at 19:38
  • https://en.wikipedia.org/wiki/Treap - "*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..", http://jeffe.cs.illinois.edu/teaching/algorithms/notes/10-treaps.pdf - "In other words, *a treap is simultaneously a binary search tree for the search keys and a (min-)heap for the priorities*.." – user2864740 Mar 29 '18 at 19:38
  • @Srini yeah, but then the question is why should I use a treap if I could just use a hash map instead if the key benefit would be the only one? – Vlad Mar 29 '18 at 19:42
  • Yes hash based structures do offer significant benefits over trees when it comes to independent lookup, but don't discount them :). When you want to preserve some kind of order or relation between nodes as they are extracted from the structure trees/treaps are the way to go! eg: tracing the path down the tree/treap to a node – Srini Mar 29 '18 at 19:47
  • from an end user's perspective, i mean average developer here, I dont think (not guaranteeing anything) treap offers any extra benefit. If you were a library designer however, treap is simpler to implement. – Shihab Shahriar Khan Mar 30 '18 at 13:42

1 Answers1

0

They are easier to implement and more importantly, that makes them easier to modify/maintain into the future if you want to make slight variations on them or change them some way. They also allow for efficient parallel versions of set operations Union/Intersect/Difference which is extremely valuable. Using them simultaneously as a heap and binary tree isn't really very handy unless the stuff you use for priorities are coincidentally really nicely randomly distributed/permuted. I suppose there might be a case where that would be handy, but it seems really unlikely. Stuff so randomly distributed is usually more like a hash key which typically aren't useful as ordered data. How often do you want to pull people out in order of their SSNs? I guess it's possible but unlikely.

Darrell Plank
  • 1,034
  • 7
  • 5