Introduction to Algorithms
October 29, 2005
Massachusetts Institute of Technology
6.046J/18.410J
Professors Erik D. Demaine and Charles E. Leiserson
Handout 18
Problem Set 4 Solutions
Problem 41.
Treaps
If we insert a set of
n
items into a binary search tree using T
REE
I
NSERT
, the resulting tree may
be horribly unbalanced. As we saw in class, however, we expect randomly built binary search
trees to be balanced. (Precisely, a randomly built binary search tree has expected height
O
(lg
n
)
.)
Therefore, if we want to build an expected balanced tree for a ﬁxed set of items, we could randomly
permute the items and then insert them in that order into the tree.
What if we do not have all the items at once? If we receive the items one at a time, can we still
randomly build a binary search tree out of them?
We will examine a data structure that answers this question in the afﬁrmative. A
treap
is a binary
search tree with a modiﬁed way of ordering the nodes. Figure 1 shows an example of a treap. As
usual, each item
x
in the tree has a key
key
[
x
]
. In addition, we assign
priority
[
x
]
, which is a random
number chosen independently for each
x
. We assume that all priorities are distinct and also that all
keys are distinct. The nodes of the treap are ordered so that (1) the keys obey the binarysearchtree
property and (2) the priorities obey the minheap order property. In other words,
•
if
v
is a left child of
u
, then
key
[
v
]
<
key
[
u
]
;
•
if
v
is a right child of
u
, then
key
[
v
]
>
key
[
u
]
; and
•
if
v
is a child of
u
, then
priority
(
v
)
>
priority
(
u
)
.
(This combination of properties is why the tree is called a “treap”: it has features of both a binary
search tree and a heap.)
73
I:
G:
B:
H:
E:
7
K:
5
65
23
4
A: 10
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
,x
2
,...,x
n
, each
with an associated key, into a treap in arbitrary order. Then the resulting treap is the tree that would
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Handout 18: Problem Set 4 Solutions
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
[
x
i
]
<
priority
[
x
j
]
means that
x
i
is
effectively inserted before
x
j
.
(a)
Given a set of nodes
x
1
,x
2
,...,x
n
with keys and priorities all distinct, show that there
is a unique treap with these nodes.
Solution:
Prove by induction on the number of nodes in the tree. The base case is a tree with
zero nodes, which is trivially unique. Assume for induction that treaps with
k
−
1
or
fewer nodes are unique. We prove that a treap with
k
nodes is unique. In this treap, the
item
x
with minimum priority must be at the root. The left subtree has items with keys
<
key
[
x
]
and the right subtree has items with keys
>
key
[
x
]
. This uniquely deﬁnes the
root and both subtrees of the root. Each subtree is a treap of size
≤
k
−
1
, so they are
unique by induction.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '05
 ErikD.DemaineandCharlesE.Leiserson
 LG

Click to edit the document details