Lecture10 RBT
Lecture10 RBT
B Trees
Binomial Heaps Amortized Analysis (Not mentioned in
syllabus, but needs to be studied) Fibonacci Heaps Disjoint Sets Data Structures (Not mentioned in syllabus, but needs to be studied)
Red-Black Trees
Lecture 10
Red-Black Trees
Red-black trees:
Binary search trees augmented with node color
Operations designed to guarantee that the height
h = O(lg n)
First: describe the properties of red-black trees
Then: prove that these guarantee h = O(lg n) Finally: describe operations on red-black trees
Red-Black Properties
The red-black properties:
1. Every node is either red or black 2. Every leaf (NULL pointer) is black
Note: this means every real node has 2 children
4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black
Example: RED-BLACK-TREE
26 17
NIL NIL
41 30 47
NIL
38
NIL NIL
NIL
50
NIL NIL
NIL[T] has the same fields as an ordinary node The other fields may be set to arbitrary values
Black-Height of a Node
26
h=1 bh = 1 NIL h=4 bh = 2
17
NIL h=2 30 bh = 1
41
h=3 bh = 2
NIL
h=1 bh = 1 38 NIL
47
h=2 bh = 1
50
NIL
h=1 bh = 1 NIL
NIL
NIL
Black-height of a node x: bh(x) is the number of black nodes (including NIL) on the path from x to leaf, not counting x
Successor(), Predecessor()
Search()
Insert() and Delete(): Will also take O(lg n) time But will need special care since they modify tree
Red-black properties: 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black
7 5 9 12
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 9 12
should it be?
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 9 12
should it be?
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 9 12
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 11 9 12
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 11 9 12
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 11 9 12
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 11 9 12
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 11 9 12
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
7 5 8 11 9 12
1. 2. 3. 4. 5.
Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
10
7 5 8 11 10 9 12
O(lg n) time
RB Trees: Rotation
Our basic operation for changing tree structure is
called rotation:
y x A C
leftRotate(x)
rightRotate(y)
x A y
B B C Does rotation preserve inorder key ordering? What would the code for rightRotate() actually do?
RB Trees: Rotation
y x A B C
rightRotate(y)
x A B y C
Answer: A lot of pointer manipulation x keeps its left child y keeps its right child xs right child becomes ys left child xs and ys parents change
What is the running time?
Rotation Example
Rotate left about 9:
7
5
8
9
12
11
Rotation Example
Rotate left about 9:
7
5
9
12
11
rbInsert(T, x)
{treeInsert(T, x); color[x] = RED; while (color[p[x]] == RED) { if (p[x] == left[p[p[x]]])/*if xs parent is a left child*/ {y = right[p[p[x]]]; /*y is xs uncle */ if (color[y] == RED) { color[p[x]] = BLACK; Case 1: color[y] = BLACK; Uncle is color[p[p[x]]] = RED; x = p[p[x]];} RED else {if (x == right[p[x]]) /* x is a right child*/ Case 2: Uncle is {x = p[x]; BLACK & x is right leftRotate(x);} child color[p[x]] = BLACK; Case 3: Uncle is color[p[p[x]]] = RED; BLACK & x is left rightRotate(p[p[x]]);}} child else {(same as above, but with right & left exchanged)} } color[root[T]] = BLACK; }
RB Insert: Case 1
if (color[y] == RED) { color[p[x]] = BLACK; color[y] = BLACK; color[p[p[x]]] = RED; x = p[p[x]];}
equal-black-height subtrees
C A D y B x
case 1
C new x A D B
Change colors of some nodes, preserving #4: all downward paths have equal b.h. The while loop now continues with xs grandparent as the new x
RB Insert: Case 2
{if (x == right[p[x]]) {x = p[x]; leftRotate(x);}
// continue with case 3 code
left-rotation C A
case 2
C B A x
B x
Transform case 2 into case 3 (x is left child) with a left rotation This preserves property 4: all downward paths contain same number of black nodes
RB Insert: Case 3
color[p[x]] = BLACK; color[p[p[x]]] = RED; rightRotate(p[p[x]]);
Case 3: Uncle is black Node x is a left child Change colors; rotate right
C B A x
case 3
B
x A
Perform some color changes and do a right rotation Again, preserves property 4: all downward paths contain same number of black nodes
Analysis of Insertion
Insertion consists of two phases
First phase goes down the tree O(lg n)
Second phase goes up the tree fixing things While loop repeats only in case 1 moving the pointer up by two levels O(lg n) The most no of rotations performed are two O(1)
Examples
Insert the following keys into an initially empty RB Tree 1. 41, 38, 31, 12, 19, 8 2. 8, 19, 12, 31, 38, 41