This preview shows pages 1–3. Sign up to view the full content.
Selected Solutions for Chapter 14:
Augmenting Data Structures
Solution to Exercise 14.17
Let
AŒ1 :: n±
be the array of
n
distinct numbers.
One way to count the inversions is to add up, for each element, the number of larger
elements that precede it in the array:
# of inversions
D
n
X
j
D
1
j
In
².j/
j
;
where
In
².j/
D f
i
W
i < j
and
AŒi± > AŒj±
g
.
Note that
j
In
².j/
j
is related to
AŒj±
’s rank in the subarray
AŒ1 : : j±
because the
elements in
In
².j/
are the reason that
AŒj±
is not positioned according to its rank.
Let
r.j/
be the rank of
AŒj±
in
AŒ1 : : j±
. Then
j
D
r.j/
C j
In
².j/
j
, so we can
compute
j
In
².j/
j D
j
N
r.j/
by inserting
AŒ1±; :: : ; AŒn±
into an orderstatistic tree and using OSRANK to find
the rank of each
AŒj±
in the tree immediately after it is inserted into the tree. (This
OSRANK value is
r.j/
.)
Insertion and OSRANK each take
O.
lg
n/
time, and so the total time for
n
ele
ments is
O.n
lg
n/
.
Solution to Exercise 14.22
Yes, we can maintain blackheights as attributes in the nodes of a redblack tree
without affecting the asymptotic performance of the redblack tree operations. We
appeal to Theorem 14.1, because the blackheight of a node can be computed from
the information at the node and its two children. Actually, the blackheight can
be computed from just one child’s information: the blackheight of a node is the
blackheight of a red child, or the black height of a black child plus one. The
second child does not need to be checked because of property 5 of redblack trees.
Within the RBINSERTFIXUP and RBDELETEFIXUP procedures are color
changes, each of which potentially cause
O.
lg
n/
blackheight changes. Let us
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 142
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 02/22/2010 for the course CS 310 taught by Professor Fussell during the Spring '08 term at University of Texas at Austin.
 Spring '08
 Fussell
 Data Structures

Click to edit the document details