IT3011 Week 12 Red-black trees
IT3011 Week 12 Red-black trees
AND ALGORITHMS
Chapter 8 - Searching
Week 12. Red-Black Tree
3
CONTENT
• Definition
• Insetion
4
Definition
• Red-Black tree is a binary search tree with properties:
• Each node has a color (red or black)
• Color of root is black
• Leaf (or NULL node) has color black (is presented by a rectangle)
• A red node has 2 children with color black
• Paths from a node to leaf nodes have the same number of black
nodes
5
Definition
• Red-Black (RB) tree is a binary search tree with properties:
• (1) Each node has a color (red or black)
• (2) Color of root is black
• (3) Leaf (or NULL node) has color black (is presented by a
rectangle)
• (4) A red node has 2 children with color black
• (5) Paths from a node to leaf nodes have the same number of
black nodes
• Notation: bh(x): number of black nodes (except x) on the path from x
to a leaf
• Lemma 1. A RB tree contains at least 2bh(x)-1 internal nodes
• Lemma 2. Height of a RB tree containing n nodes is at most
2log(n+1)
6
Definition
• Typical data structure of a node on a RB tree
Node {
key; // key of the node
color; // color of the node
p; // pointer to the parent
left; // pointer to the left-child
right: // pointer to the right-child
}
7
Insertion
• When inserting a new node (node z) in the RB tree T
• Insert node z into T as in binary search tree
• Assign red color to this node z
• If the RB property is not satisfied, then we perform rotations
and change the color of some nodes to recover the RB property
• Notation
• p: the parent node of z
• u: sibling node of p (uncle node of z)
• g: parent of p (grand parent of z)
8
Insertion: Left Rotation
Algorithm leftRotate(r, x)
Input: pointer r to the root of the RB tree T, pointer x to
some node of T
Output: perform left rotation on x, return the pointer to the
root of the resulting tree
1. y = x.right;
2. x.right = y.left;
3. if y.left != NULL then {
4. y.left.p = x;
5. }
6. y.p = x.p;
7. if x.p = NULL then {
8. r = y;
9. } else if x = x.p.left then {
10. x.p.left = y;
11. } else {
12. x.p.right = y;
13. }
14. y.left = x;
15. x.p = y;
16. return r;
9
Insertion: Right Rotation
Thuật toán rightRotate(r, y)
Input: pointer r to the root of the RB tree T, pointer y to
some node of T
Output: perform right rotation on y, return the pointer to
the root of the resulting tree
1. x = y.left;
2. y.left = x.right;
3. If x.right != NULL {
4. x.right.p = x;
5. }
6. x.p = y.p;
7. if y.p == NULL {
8. r = x;
9. } else if y = y.p.left {
10. y.p.left = x;
11. } else {
12. y.p.right = x;
13. }
14. x.right = y;
15. y->p = x;
16. return r;
10
Fix RB property
• Case 1: T is empty, make z as the root, assign black color to z
• Case 2: (node p – parent of z – with black color) RB property is satisfied, do nothing
• Case 3: (node p has color red), property RB (4) is not satisfied. Node g has color black (as before
insertion, T is a RB tree with RB property)
• Case 3.1: (node u (if exists) has color red) change the color of p and u from red to black and
change the color of g by the color red, repeat this process with node g (Figure below).
11
Fix RB property
• Case 3: (node p has color red), property RB (4) is not satisfied. Node g has color black (as before
insertion, T is a RB tree with RB property)
• Case 3.2: (node u (if exists) has color black) perform a single-rotation or double-rotation
depending on z is the left child or right child of p
• Case 3.2.1 (p is a right child of g and z is a right child of p), perform left rotation on g and flip
the color of p and g (Figure below)
12
Fix RB property
• Case 3: (node p has color red), property RB (4) is not satisfied. Node g has color black (as before
insertion, T is a RB tree with RB property)
• Case 3.2: (node u (if exists) has color black) perform a single-rotation or double-rotation
depending on z is the left child or right child of p
• Case 3.2.2 (p is a right child of g and z is a left child of p), perform right rotation on p (Figure
below) return to the case 3.2.1 (process as in the case 3.2.1)
13
Fix RB property
• Case 3: (node p has color red), property RB (4) is not satisfied. Node g has color black (as before
insertion, T is a RB tree with RB property)
• Case 3.2: (node u (if exists) has color black) perform a single-rotation or double-rotation
depending on z is the left child or right child of p
• Case 3.2.3 (p is a left child of g and z is a left child of p), perform right rotation on g and flip
the color of p and g (Figure below)
14
Fix RB property
• Case 3: (node p has color red), property RB (4) is not satisfied. Node g has color black (as before
insertion, T is a RB tree with RB property)
• Case 3.2: (node u (if exists) has color black) perform a single-rotation or double-rotation
depending on z is the left child or right child of p
• Case 3.2.4 (p is a left child of g and z is a right child of p), perform left rotation on p (Figure
below) return to the case 3.2.3 (process as in the case 3.2.3)
15
Insertion: Fix RB property
Insert(r, k) { insertFixUp(r,z) {
x = r; while z.p != NULL and z.p.color = ‘RED' do {
y = NULL; if z.p = z.p.p.left then {// z->p is a left-child of its parent
while x != NULL do { u = z.p.p.right; // y is the uncle of z
y = x; if u != NULL and u.color = 'RED'){// case 3.1
if k < x.key then z.p.color = 'BLACK'; u.color = 'BLACK'; z.p.p.color = 'RED';
x = x->left; z = z.p.p;// repeat with the grand-parent of z
else } else {
x = x->right; if z = z.p.right then { z = z.p; r = leftRotate(r,z); }
} z.p.color = 'BLACK'; z.p.p.color = 'R'; r = rightRotate(r, z.p.p);
z = createNode(k,'RED'); }
z.p = y; } else {// z->p is the right-child of its parent
if y = NULL then { u = z.p.p.left;
z.color = 'BLACK'; if u != NULL and u.color = 'RED' then {// case 3.1
return z; z.p.color = 'BLACK'; u.color = 'BLACK'; z.p.p.color = 'RED'; z = z.p.p;
} else if z.key < y.key then } else {
y.left = z; if z = z.p.left then { z = z.p; r = rightRotate(r, z); }
else z.p.color = 'BLACK'; z.p.p.color = 'RED'; r = leftRotate(r, z.p.p);
y.right = z; }
return insertFixUp(r,z); }
} }
r.color = 'BLACK'; return r;
}
16
THANK YOU !
17