October 2025
Intermediate to advanced
485 pages
13h 2m
English
Because a node’s priority is randomly generated, the number of rotations that occur upon treap insertion is determined by randomization. For instance, if our V node from the previous example happened to have been randomly assigned the priority of 100, we wouldn’t have performed any rotations at all. This is because a node with a priority of 100 indeed belongs at the bottom of the treap. And if the V received the priority of 1, we would have rotated the V until it became the root of the treap.
The crazy thing is that this randomization allows treaps to achieve a level of balance that is similar to red-black trees. While red-black trees have a rotation scheme that is complex and follows a precise set of rules, ...
Read now
Unlock full access