Title: CS473-Algorithms
1CS473-Algorithms
- Lecture
- RED-BLACK TREES (RBT)
2Overview
- Previous lecture showed that a BST of height h
can implement any of the dynamic set operations
in O(h) time. - Thus, set operations are fast if the height of
BST is small. - But, if h is large, its performance is no better
than linked list.
3Overview
- Red-Black trees are one of many search tree
schemes that are balanced to guarantee h O(lgn) - First, we will show that h O(lgn) in RBTs
- Then, show how to manipulate maintain RBTs
during dynamic set operations.
4Properties of Red-Black Trees
- Add color field (1 bit) to each node of a BST.
- Consider NIL child fields as pointer to
external nodes (leafs) - i.e. Leaf nodes do not have keys
- Consider normal key-bearing nodes as internal
nodes. - Hence, each internal node has exactly 2 children.
- Thus, RBTs are full binart trees (FBTs)
- FBT Each node is either a leaf or has degree
exactly 2. - i.e. There are no degree-1 nodes.
5Red-Black Properties
- Every node is either red or black
- Every leaf (NIL) is black
- If a node is red, then both of its children are
black. - i.e. There cant be 2 consecutive reds on a
simple path. - Every simpe path from a node to a descendant leaf
contains the same number of black nodes. - ( Additional propery) The root is black.
- Simplifies explanation of insertion which assumes
and guarantees that the root is black.
6A Sample Red Black Tree
bh 2
h 4
bh 1
bh 2
h 1
h 3
bh 1
bh 2
h 2
h 4
bh 1
h 1
bh 0
h 0
7Properties of Red-Black Trees
- Black-Height bh (x)
- of blacks on the simple paths to all descendant
leaves not counting x. - By the property 4, the black-height is well
defined. - Red-Black properties ensure that
- No simpe path from a node to a descendant leaf
is more that twice as long as any other. - So, red-black trees are apptoximately balanced.
8Properties of Red-Black Trees
- Lemma 1 A height h node has black height h/2
- Height is length of the longest path to a leaf.
- By property 3, of reds on this path h/2
- Hence, of blacks h/2
- Lemma 2 The subtree Tx rooted at any node x
contains at least Tx 2bh(x) 1 internal
nodes.
9Properties of Red-Black Trees
- Basis h(x)0 x must be a leaf (NIL)
bh(x)0 - Tx contains 0 internal nodes 2bh(x) -12-1 0
- Inductive Step h(x) gt 0 x is internal node
with 2 children -
- lx leftx
- rx rightx
x
h(x)
bh(x)
rx
lx
Trx
Tlx
10Properties of Red-Black Trees
- Depending on lx and rx being red or black
- bh(lx) bh(x) or bh(x) -1, respectively,
- bh(rx) bh(x) or bh(x) -1, respectively.
- Since h(lx), h(rx) lt h(x)
- we can apply inductive hypothesis.
- Tx Tl Tr 1
- (2bh(x)-1 1) (2bh(x)-1 1) 1
- 2bh(x)-1 1 Q.E.D.
11Properties of Red-Black Trees
- Theorem A red-black tree with n internal nodes
has height at most 2lg(n1) - Due to lemma 2
- Troot n 2bh(root) 1
- Due to lemma 1
- bh(root) h(root) / 2 H / 2
- n 2H/2 -1
- (n1) 2H/2
- H 2 lg(n1)
12Properties of Red-Black Trees
- Corollary Dynamic set operations MIN, MAX,
SEARCH, SUCCESSOR, PREDECESSOR take O(lgn) time. - Fact Let lmin(x) lmax(x) denote the lengths of
the shortest longest paths from a node x to its
leafs, respectively. - Then, lmax(x) lmin(x) for any node x.
- Proof lmax(x) h(x) 2bh(x) lmin(x)
Lemma 1
13The Problem of Insertion into Red-Black Trees
- Draw a R-B tree
- Color choices starting at 12
- Forced choices 12 ? R 5 and 9 ? B
7
5
9
12
14The Problem of Insertion into Red-Black Trees
- Insert 8 (Left child of 9)
- No problem, Just color it red
7
5
9
8
? ? R
12
15The Problem of Insertion into Red-Black Trees
- Insert 11 (Left child of 12)
- 11 cant be red (Violate property 3)
- 11 cant be black (Violate property 4)
7
5
9
8
12
?
11
16The Problem of Insertion into Red-Black Trees
- Can fix up by recoloring the tree
- New 11 to Red
- Change 9 to Red
- Change 8 and 12 to Black
7
5
9
B ? R
8
R ? B
R ? B
12
??R
11
17The Problem of Insertion into Red-Black Trees
- Insert 10 (Left child of 11)
- Recoloring is not enough, why?
- -Because of tree imbalance
- -Cant satisfy property 4 (Equal of blacks on
paths) - without violating property 3 (No consecutive
reds on paths) - Must change tree structure
- -Goal Restructure in O(lgn) time.
7
5
9
8
12
11
?
10