blog/content/posts/2024-07-20-treap/index.md

1 KiB

title date draft description tags categories series favorite disable_feed graphviz
Treap 2024-07-20T14:12:27+01:00 false A simpler BST
algorithms
data structures
python
programming
Cool algorithms
false false true

The Treap is a mix between a Binary Search Tree and a Heap.

Like a Binary Search Tree, it keeps an ordered set of keys in the shape of a tree, allowing for binary search traversal.

Like a Heap, it associates each node with a priority, making sure that a parent's priority is always higher than any of its children.

What does it do?

By randomizing the priority value of each key at insertion time, we ensure a high likelihood that the tree stays roughly balanced, avoiding degenerating to unbalanced O(N) height.

Here's a sample tree created by inserting integers from 0 to 250 into the tree:

{{< graphviz file="treap.gv" />}}