1
Red-Black Trees
2
Red-Black Trees
! Motivation: a binary search tree that is guaranteed to
be balanced (operations take time in the worst-case).
! A red-black tree is a binary search tree where each
node has a color attribute: either red or black and the following properties are satisfied:
– Every node is either red or black. – Every leaf (empty child) is black. – Both children of a red node are black. – Every simple path from a node to a descendant leaf contains the same number of black nodes.
( )
n O lg
3
The height of Red-Black Trees
! Lemma: A red-black tree with n internal nodes has
height at most
! Definition: Black-height, bh(x), is the number of black
nodes on any path from x to a leaf (not counting x itself).
! Proof: The subtree rooted at any node x contains at
least internal nodes (by induction on height of x). The black-height of the root is at least h/2, and thus,
) 1 lg( 2 + n
1 2
) (
−
x bh
) 1 lg( 2 1 2
2 /
+ ≤ ≤ − n h n
h
4
Rotations
y x A B C x y C B A
left right ! Note: rotations preserve the inorder key ordering in a