The Power of Random Priorities
As I noted earlier, when we insert values in random order into a BST, the BST will likely be well-balanced. I’ll call this data structure a randomized BST.
I’ll now demonstrate intuitively why randomized treaps should be as well-balanced as randomized BSTs. The key is that the two data structures share an attribute, which is that all the values have equal odds of landing at any particular spot within the tree. Here’s what I mean.
Say that we’re inserting the values 1 through 100 into a regular BST. When we insert the values in perfectly ascending order, we are dictating that the 1 becomes the root of the tree. This already causes a severe imbalance since the root can now only have right descendants because it’s ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access