2. Red-Black Trees
• A Red-Black tree is a binary search tree with
these traits:
– Every node is either red or black.
– The root is always black.
– If a node is red, its children must be black.
– Every path from any node to a null must have the
same number of black nodes.
• Operations on Red-Black trees are O(log N).
Data Structure || JRMSU Computer Science
4. Red-Black Trees
• Red-black trees can be thought of as binary
tree representations of a 2-3-4 tree.
• A 2-3-4 tree is a B-tree of degree 4.
• This means each internal node has either 2, 3,
or 4 children.
• The next slide illustrates this.
Data Structure || JRMSU Computer Science
6. How the Trees Relate
c
c
a
f
d
e
f
a
b
d
b
g
g
e
Each black node represents a node in the 2-3-4 tree.
Each red node is in the same node with its black parent.
This explains why every path has the same number of black nodes.
Data Structure || JRMSU Computer Science
8. Inserting into a Red-Black Tree
• An inserted node is placed as in a binary
search tree as a red node, unless it is the root.
• If the parent of the new node is also red,
called a “double red”, the tree will have to be
adjusted, since a red child must have a black
parent.
Data Structure || JRMSU Computer Science
9. Two Cases
1.
a
u
w
c
b
2.
a
Inserting node z causes double red,
and z’s parent has black sibling w.
This occurs when a 4-node is
malformed: must restructure.
v
z
u
c
w
b
z
v
Inserting node z causes double red,
and z’s parent has red sibling w.
This occurs when a 4-node
overflows: must recolor.
Note: these are partial trees so not all
Data Structure || JRMSU Computer subtrees are shown.
of their Science
12. Example
e
e
e
h
h
k
1. Insert e
2. Insert h
h
h
e
k
e
3. Insert k, h has
null (black) sibling
h
e
k
n
4. Restructure
5. Insert n, k has
red sibling
Data Structure || JRMSU Computer Science
k
n
6. Recolor (root left
black)