Treap Trees MIT
Treap Trees MIT
Treap Trees MIT
Problem Set 4
MIT students: This problem set is due in lecture on Monday, October 24, 2005. The homework
lab for this problem set will be held 2–4 P. M . on Sunday, October 23, 2005.
2. At least one worked example or diagram to show more precisely how your algorithm works.
Remember, your goal is to communicate. Full credit will be given only to correct solutions
which are described clearly. Convoluted and obtuse descriptions will receive low marks.
(This combination of properties is why the tree is called a “treap”: it has features of both a binary
search tree and a heap.)
G: 4
B: 7 H: 5
A: 10 E: 23 K: 65
I: 73
Figure 1: A treap. Each node x is labeled with key[x] : priority[x]. For example, the root has key
G and priority 4.
It helps to think of treaps in the following way. Suppose that we insert nodes x 1 , x2 , . . . , xn , each
with an associated key, into a treap in arbitrary order. Then the resulting treap is the tree that would
have been formed if the nodes had been inserted into a normal binary search tree in the order given
by their (randomly chosen) priorities. In other words, priority[xi ] < priority[xj ] means that xi is
effectively inserted before xj .
Handout 15: Problem Set 4 3
(a) Given a set of nodes x1 , x2 , . . . , xn with keys and priorities all distinct, show that there�
is a unique treap with these nodes.�
(b) Show that the expected height of a treap is O(lg n), and hence the expected time to�
search for a value in the treap is O(lg n).�
Let us see how to insert a new node x into an existing treap. The first thing we do is assign x a
random priority priority[x]. Then we call the insertion algorithm, which we call T REAP -I NSERT ,
whose operation is illustrated in Figure 2.
(c) Explain how T REAP -I NSERT works. Explain the idea in English and give pseudocode.�
(Hint: Execute the usual binary search tree insert and then perform rotations to restore�
the min-heap order property.)�
(d) Show that the expected running time of T REAP -I NSERT is O(lg n).
T REAP -I NSERT performs a search and then a sequence of rotations. Although searching and ro
tating have the same asymptotic running time, they have different costs in practice. A search reads
information from the treap without modifying it, while a rotation changes parent and child pointers
within the treap. On most computers, read operations are much faster than write operations. Thus
we would like T REAP -I NSERT to perform few rotations. We will show that the expected number
of rotations performed is bounded by a constant (in fact, less than 2)!
In order to show this property, we need some definitions, illustrated in Figure 3. The left spine
of a binary search tree T is the path which runs from the root to the item with the smallest key.
In other words, the left spine is the maximal path from the root that consists only of left edges.
Symmetrically, the right spine of T is the maximal path from the root consisting only of right
edges. The length of a spine is the number of nodes it contains.
(e) Consider the treap T immediately after x is inserted using T REAP -I NSERT . Let C�
be the length of the right spine of the left subtree of x. Let D be the length of the�
left spine of the right subtree of x. Prove that the total number of rotations that were�
performed during the insertion of x is equal to C + D.�
We will now calculate the expected values of C and D. For simplicity, we assume that the keys are
1, 2, . . . , n. This assumption is without loss of generality because we only compare keys.
For two distinct nodes x and y, let k = key[x] and i = key[y], and define the indicator random
variable
1 if y is a node on the right spine of the left subtree of x (in T ),
�
Xi,k =
0 otherwise.
(f) Show that Xi,k = 1 if and only if (1) priority[y] > priority[x], (2) key[y] < key[x], and
(3) for every z such that key[y] < key[z] < key[x], we have priority[y] < priority[z].
4 Handout 15: Problem Set 4�
G: 4 G: 4
B: 7 H: 5 C: 25 B: 7 H: 5
A: 10 E: 23 K: 65 A: 10 E: 23 K: 65
I: 73 C: 25 I: 73
(a) (b)
G: 4 G: 4
D: 9 B: 7 H: 5 B: 7 H: 5
A: 10 E: 23 K: 65 A: 10 E: 23 K: 65
C: 25 I: 73 D: 9 I: 73
D: 9 C: 25
(c) (d)
G: 4 F: 2
B: 7 H: 5 F: 2 B: 7 G: 4
...
A: 10 D: 9 K: 65 A: 10 D: 9 H: 5
C: 25 E: 23 I: 73 C: 25 E: 23 K: 65
I: 73
(e) (f)
Figure 2: Operation of T REAP -I NSERT . As in Figure 1, each node x is labeled with key[x] :
priority[x]. (a) Original treap prior to insertion. (b) The treap after inserting a node with key
C and priority 25. (c)–(d) Intermediate stages when inserting a node with key D and priority 9.
(e) The treap after insertion of parts (c) and (d) is done. (f) The treap after inserting a node with
key F and priority 2.
Handout 15: Problem Set 4� 5
15 15
9 18 9 18
3 12 25 3 12 25
6 21 6 21
(a)� (b)
Figure 3: Spines of a binary search tree. The left spine is shaded in (a), and the right spine is
shaded in (b).
(a) Every node of the tree is either a leaf or it has two children.
(b) The size of each subtree can be written as 2k − 1, where k is an integer (k is not the�
same for each subtree).�
6 Handout 15: Problem Set 4
(c) There is a constant c > 0 such that, for each node of the tree, the size of the smaller�
child subtree of this node is at least c times the size of the larger child subtree.�
(d) There is a constant c such that, for each node of the tree, the heights of its children�
subtrees differ by at most c.�
(e) The average depth of a node is O(lg n). (Recall that the depth of a node x is the�
number of edges along the path from the root of the tree to x.)�