



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Fall 2005;
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Jared Saia
University of New Mexico
Red-Black Trees
depth of a node in a randomly built tree?”We want to answer the question: “What will be the average
Define the random variable
to be the depth of a node
chosen uniformly at random in the tree
takes on
n
possible values, it takes on each value with
probability 1
/n
For a tree
and node
x , let
d ( x, T
(^) ) be the depth of node
x
in
Define the total path length,
(^) ), to be the sum over all
nodes
x
in
of
d ( x, T
Then
n 1
x ∑ ∈ T
d ( x, T
n 1 P (^) ( T (^) )
Thus we want to show that
n (^) log
(^) n
)
“Shut up brain or I’ll poke you with a Q-Tip” - Homer Simpson
Let
l ,
T r
be the left and right subtrees of
respectively.
Let
n
be the number of nodes in
Then
l ) +
r ) +
(^) n
(^) 1. Why?
Let
n ) be the expected total depth of all nodes in a ran-
domly built binary tree with
n
nodes
Note that for all
i , 0
i ≤
n (^) −
(^) 1, the probability that
l has
i
nodes and
r
has
n (^) −
i (^) −
(^) 1 nodes is 1
/n
Thus
n ) =
n 1 ∑
n − 1
i ) +
n (^) −
i −
(^) 1) +
(^) n
(^) −
n )
n 1
n − 1
i=0 ∑
i ) +
n (^) −
i (^) −
(^) n
(^) −
n 1 ( n − 1
i=0 ∑
i ) +
n (^) −
i −
(^) 1)) +
n 1 ( n − 1
i=0 ∑
n (^) −
n 1 ( n − 1
i=0 ∑
i ) +
n (^) −
i −
(^) 1)) + Θ(
n )
n 2 ( n − 1
k=1 ∑
k )) + Θ(
n )
We have
n ) =
n 2 ( ∑
n − 1
k )) + Θ(
n )
This is the same as the recurrence for randomized Quicksort
rence isRecall from hw problem 7-2, that the solution to this recur-
n ) =
n (^) log
(^) n
)
A RB-Tree is a balanced binary search tree
The
height
of
the
tree
is
always
(log
(^) n
)
where
n
is
the
number of nodes in the tree
and parent pointerEach node has a “color” field in addition to a key, left, right,
sponding pointer field will contain the value NILIf the child or parent of a node does not exist, the corre-
internal nodes of the tree(leaves) of the tree, and say that all key-bearing nodes areWe will say that these NIL’s are pointers to external nodes
A BST is a red-black tree if it satisfies the RB-Properties
contain the same number of black nodes
Black-height
of a node
x , bh(x) is the number of black nodes
on any path from, but not including
x
down to a leaf node.
paths have the same number of black nodesNote that the black-height of a node is well-defined since all
the rootThe black-height of an RB-Tree is just the black-height of
Lemma: A RB-Tree with
n
internal nodes has height at most
2 log(
n
x
contains at least
bh
( x ) −
(^) 1 internal nodes
r ,
bh
( r )
≥
h/
2, thus
n
h/
2
−
Taking
logs of both sides, we get that
h
≤
(^) 2 log(
n (^) + 1)
x
contains at least 2
bh
( x ) −
internal nodes. Show by induction on the height of
x .
BC: If the height of
x
is 0,
then
x
is a leaf,
and subtree
rooted at
x
does indeed contain 2
0
−
(^) 1 = 0 internal nodes
IH: For all nodes
y
of height less than
x , the subtree rooted
at
y
contains at least 2
bh
( y ) −
(^) 1 internal nodes.
IS: Consider a node
x
which is an internal node with two
children(all internal nodes have two children).
Each child
has black-height of either
bh
x ) or
bh
x ) −
1 (the former if
it is red, the latter if it is black).
Since the height of these
children is less than
x , we can apply the inductive hypothesis
to conclude that each child has at least 2
bh
( x ) − 1
−
(^) 1 internal
nodes. This implies that the subtree rooted at
x
has at least
bh
( x ) − 1 −^
(^) 1) + (
bh
( x ) − 1 −^
(^) 1) + 1 = 2
bh
( x ) −^
(^) 1 internal nodes.
This proves the claim.
tained?How do we ensure that the Red-Black Properties are main-
holds?we re-arrange the new tree so that the Red-Black PropertyI.e. when we insert a new node, what do we color it? How do
How about for deletions?
words, Show that Left-Rotate(x) maintains the BST Property. In other
show
that
if
the
Property
was true
for
the
tree
after the operation.before the Left-Rotate(x) operation, then it’s true for the tree
Show that after rotation,
the BST property holds for the
entire subtree rooted at
x
Show that after rotation,
the BST property holds for the
subtree rooted at
y
the entire treeNow argue that after rotation, the BST property holds for