COT 5410-01 Fall 2004
Algorithms
Chris Lacher
Notes 9: Set < Red-Black Tree >
Set < BinarySearchTree >
Simple translation
Search, Insert, Remove runtimes all O(depth)
Set::Iterator < BinarySearchTree::Iterator >
Iterator Interface: Bidirectional Iterator
Simple Translation
All iterator operations have constant or amortized constant runtime
Traversal order is sorted
RedBlackTree
Challenge goals:
Maintain BinarySearchTree property
Maintain depth = Θ(size) property
Cost of maintenance <= O(log size)
RedBlackTree is a BinarySearchTree such that:
Every node is either Red or Black
The root is Black
Every nil-leaf is Black
If a node is Red then both its children are Black
For each node, all paths from the node to descendent nil-leaves contain the same number of Black nodes
Challenge goals (i) and (ii) met:
RedBlackTree is a BinarySearchTree, by definition
A RedBlackTree with
n
non-nil nodes has height <= 2 + 2 log (
n
+ 1)
Proof:
The subtree rooted at
x
contains at least 2
bh(
x
) - 1
- 1 internal (non-nil) nodes. (Prove by induction on height of
x
.)
Let
h
= height of tree. Then
n
= number of non-nil nodes entire tree, and:
2
bh(root) - 1
- 1 <=
n
(by (1) above)
h
<= 2bh(root) (by definition property (4))
2
h
/2 - 1
- 1 <=
n
h
/2 - 1 <= log (
n
+ 1)
h
<= 2 + 2 log (
n
+ 1)
Consequence:
h
= Θ(log
n
)
Goal (iii): Repair at cost O(log
n
)
Animation