CSE 2320 Notes 12:
RedBlack Trees
(Last updated 10/30/11 3:50 PM)
Sedgewick 13.313.4
12.A.
S
TRUCTURAL
P
ROPERTIES
A
redblack tree
is a binary search tree whose height is
Ο
log
n
( )
in the number of keys (
n
) stored.
1. Every node is colored red or black.
(Colors are only examined during insertion and deletion)
2.
null
is colored black and is optionally drawn as a sentinel node in diagrams.
3. Both children of a red node are black.
4. Every simple path from a child of node X to
null
has the same number of black nodes.
This number is known as the
blackheight
of X (bh(X)).
These are not stored, but appear below
nodes in some diagrams.
Example (
notes12.page1.dat
):
[170 1 1]
(160 3 1)
[150 1 1]
[140 5 2]
(130 1 1)
(120 7 2)
(110 1 1)
[100 13 3]
(90 1 1)
[80 3 2]
(70 1 1)
(60 5 2)
(50 1 1)
(40 17 3)
(30 1 1)
(20 3 2)
(10 1 1)
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentObservations:
1. A redblack tree with
n
internal nodes (“keys”) has height at most 2 lg(
n
+1).
2. If a node X is not
null
and its sibling is
null
, then X must be red.
3. There may be many ways to color a binary search tree to make it a redblack tree.
4. If the root is colored red, then it may be switched to black without violating structural properties.
(Implementations usually color root as black.)
1
2
3
4
5
6
7
8
1
2
4
5
6
7
3
1
2
12.B.
T
OP
D
OWN
(R
ECURSIVE
)
I
NSERTION
1. Start with unbalanced insert.
a. If a (black) node (y) with both children colored red is encountered during downward search, flip
colors on all three nodes before proceeding to the appropriate child (x or z):
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '12
 BOBWEEMS
 Binary Search, Harshad number, INSERT, Redblack tree, consecutive red nodes

Click to edit the document details