In most examples I've seen on the web, it is taken as given that there's some sort of external random number generator that will yield random (distinct!) priorities.
From what I recall, there was a method to actually generate the priorities randomly and when inserting them down the treap to untie possible priorities clashes on-the-go.
I have two questions, then:
- Do the treaps actually need to have its priorities all distinct, or only the priorities of nodes that may end up being compared? That is, if I have two nodes with a priority
p
but I can make sure they will never meet in any way, is there a problem in having them have the same priority? - How should I go about generating and untying priorities in my treap?
Thanks
EDIT: Here's a description of what I'm talking about:
Problem 8.12 @ Randomized Algorithms
Let us now analyze the number of random bits needed to implement the operations of a treap. Suppose we pick each priority Pi uniformly at random from the unit interval [0,1]. Then, the binary representation of each Pi can be generated as a (potentially infinite) sequence of bits that are the outcome of unbiased coin flips. The idea is to generate only as many bits in this sequence as is necessary for resolving comparisons between different priorities. Suppose we have only generated some prefixes of the binary representations of the priorities of the elements in the treap T. Now, while inserting an item y, we compare its priority Py to others' priorities to determine how y should be rotated. While comparing Py to some PI. if their current partial binary representation can resolve the comparison, then we are done. Otherwise. they have the same partial binary representation and we keep generating more bits for each till they first differ.