13 Red Black Trees
13 Red Black Trees
Red-Black Trees
All binary search tree operations take (ℎ) time, where h is the
height of the tree
Therefore, it is important to `balance’ the tree so that its height is as
small as possible
There are many ways to achieve this
One of them: Red-Black trees
Every node of such a tree contains one extra bit, its color
Another agreement: the Nil pointer is treated as a leaf, an extra node
The rest of the nodes are called internal
Algorithms – Red-Black Trees 13-3
Red-Black Properties
A binary search tree is a red-black tree if it satisfies the following
red-black properties:
Every node is either red or black
The root is black
Every leaf (Nil) is black
If a node is red, then both its children are black
For each node, all paths from the node to descendant leaves
contain the same number of black nodes
Algorithms – Red-Black Trees 13-4
Example
Nil Nil
Nil Nil
Algorithms – Red-Black Trees 13-5
Example
nil[ ]
Algorithms – Red-Black Trees 13-6
Black Height
The number of black nodes on paths from node to its descendant
leaves in a red-black tree is called its black height, denoted bh( )
Lemma
A red-black tree with internal nodes has height at most 2 log( + 1)
Proof
We show first that the subtree rooted at contains at least 2 ( ) − 1
nodes
Induction on bh( )
Base Case: If bh( ) = 0, then is a leaf, nil[ ]
In this case, 2 ( ) − 1 = 2 − 1 = 0 internal nodes
Algorithms – Red-Black Trees 13-7
QED
Algorithms – Red-Black Trees 13-9
Rotations
Sometimes we will need to rearrange pointers inside an RB-tree
Left-Rotate( , )
# %
Right-Rotate( , )
$ % # $
Algorithms – Red-Black Trees 13-10
Rotations: Pseudocode
Left-Rotate( , )
set : =right[ ]
set right[ ]: =left[ ]
set parent[left[ ]]: =
set parent[ ]: =parent[ ]
if parent[ ] =nil[ ] then
set root[ ]: =
else if =left[parent[ ]] then
set left[parent[ ]]: =
else
right[parent[ ]]: =
set left[ ]: =
set parent[ ]: =
Algorithms – Red-Black Trees 13-11
Rotations: Example
Algorithms – Red-Black Trees 13-12
Insertion
Insertion for RB-trees is done in the same way as for ordinary binary
search trees.
Except:
we should be careful about Nil links
the new node is colored red
the resulting tree may not be an RB-tree, we need to fix it
Algorithms – Red-Black Trees 13-13
Insertion: Pseudocode
RB-Insert( , ()
set : =Nil[ ], : =root[ ]
while ≠ Nil[ ] do
set : =
if *+ [(] < *+ [ ] then set : =left[ ]
else set : =right[ ]
endwhile
set parent[(]: =
if =Nil[ ] then set root[ ]: = (
else if *+ [(] < *+ [ ] then set left[ ]: = (
else set right[ ]: = (
set left[(]: =Nil[ ] right[(]: =Nil[ ] color[(]: =RED
RB-Insert-FixUp( , ()
Algorithms – Red-Black Trees 13-14
Insertion: FixUp
Step 1
Step 2
Step 3
Algorithms – Red-Black Trees 13-15
FixUp: Pseudocode
RB-Insert-FixUp( , ()
while color[parent[(]] =RED do
if parent[(] =left[parent[parent[(]]] then do
set : =right[parent[parent[(]]]
if color[ ] =RED then do
set color[parent[(]]: =BLACK color[ ]: =BLACK
set color[parent[parent[(]]]: =RED
set (: =parent[parent[(]]
else do
if ( =right[parent[(]] then do
set (: =parent[(] Left-Rotate( , ()
set color[parent[(]]: =BLACK
set color[parent[parent[(]]]: =RED
Right-Rotate( ,parent[parent[(]])
else (same as then with left and right swopped)
color[root[ ]]: =BLACK