Page 4 of 33
CSE 100, UCSD:
LEC8
Uniqueness of treaps
✔
Given a set of (key,priority) pairs, with all the key values unique, you could always
construct a treap containing those (key,priority) pairs:
✗
Start with an empty treap
✗
Insert the (key,priority) pairs in decreasing order of priority, using the usual binary
search tree insert algorithm that pays attention to the key values only
✗
The result is a treap:
BST ordering of keys is enforced by the BST insert, heap
ordering of priorities is enforced by inserting in priority sorted order
✔
If the priority values as well as the key values are unique, the treap containing the
(key,priority) pairs is unique
✔
For example, the treap on the previous page is the unique treap containing these
pairs:
(G,50),(C,35),(E,33),(H,29),(I,25),(B,24),(A,21),(L,16),(J,13),(K,9),(D,8)
✔
Of course we will really be interested in algorithms that create a treap from
(key,priority) pairs no matter what order they are inserted in.