CMPT 307 — Data Structures and Algorithms
Outline Solutions to Exercises on RedBlack Trees
1.
Show that any arbitrary
n
node binary search tree can be transformed into any other arbitrary
n
node
binary search tree using
O
(
n
)
rotations.
Since the exercise asks about binary search trees rather than the more specific redblack trees, we assume here
that leaves are fullfledged nodes, and we ignore the sentinels.
We start by showing that with at most
n

1
right rotations, we can convert any binary search tree into one that
is just a rightgoing chain.
The idea is simple. Let us define a
right spine
as the root and all descendants of the root that are reachable by
following only right pointers from the root. A binary search tree is just a rightgoing chain has all
n
nodes in
the right spine.
As long as the tree is not just a right spine, repeatedly find some node
y
on the right spine that has a nonleaf
left child
x
and perform a right rotation on
y
. Observe that this right rotation adds
x
to the right spine, and no
other nodes leave the right spine. Thus, this right rotation increases the number of nodes in the right spine by
n
. Any binary search tree starts out with at least one node — the root — in the right spine. Moreover, if there
are any nodes not on the right spine, then at least one such node has a parent on the right spine. Thus, at most
n

1
right rotations are needed to put all nodes in the right spine, so that the tree consists of a single rightgoing
chain.
If we new the sequence of right rotations that transforms an arbitrary search tree
T
to a single rightgoing chain
T
prime
, then we could perform this sequence in reverse — turning each right rotation into its inverse left rotation —
to transform
T
prime
back into
T
.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '09
 A.BULATOV
 Algorithms, Binary Search, Data Structures, Redblack tree

Click to edit the document details