week12_RedBlackTree
week12_RedBlackTree
AND ALGORITHMS
CONTENT
• Definition
• Insetion
Chapter 8 - Searching
Week 12. Red-Black Tree
3 4
Definition Definition
• Red-Black tree is a binary search tree with properties: • Red-Black (RB) tree is a binary search tree with properties:
• Each node has a color (red or black) • (1) Each node has a color (red or black)
• Color of root is black • (2) Color of root is black
• Leaf (or NULL node) has color black (is presented by a rectangle) • (3) Leaf (or NULL node) has color black (is presented by a
• A red node has 2 children with color black rectangle)
• Paths from a node to leaf nodes have the same number of black • (4) A red node has 2 children with color black
nodes • (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)
5 6
Definition Insertion
• Typical data structure of a node on a RB tree • When inserting a new node (node z) in the RB tree T
• Insert node z into T as in binary search tree
Node { • Assign red color to this node z
key; // key of the node • If the RB property is not satisfied, then we perform rotations
color; // color of the node and change the color of some nodes to recover the RB property
p; // pointer to the parent • Notation
• p: the parent node of z
left; // pointer to the left-child
• u: sibling node of p (uncle node of z)
right: // pointer to the right-child • g: parent of p (grand parent of z)
}
7 8
Insertion: Left Rotation Insertion: Right Rotation
Algorithm leftRotate(r, x) Thuật toán rightRotate(r, y)
Input: pointer r to the root of the RB tree T, pointer x to Input: pointer r to the root of the RB tree T, pointer y to
some node of T some node of T
Output: perform left rotation on x, return the pointer to the Output: perform right rotation on y, return the pointer to
root of the resulting tree the root of the resulting tree
1. y = x.right; 1. x = y.left;
2. x.right = y.left; 2. y.left = x.right;
3. if y.left != NULL then { 3. If x.right != NULL {
4. y.left.p = x; 4. x.right.p = x;
5. } 5. }
6. y.p = x.p; 6. x.p = y.p;
7. if x.p = NULL then { 7. if y.p == NULL {
8. r = y; 8. r = x;
9. } else if x = x.p.left then { 9. } else if y = y.p.left {
10. x.p.left = y; 10. y.p.left = x;
11. } else { 11. } else {
12. x.p.right = y; 12. y.p.right = x;
13. } 13. }
14. y.left = x; 14. x.right = y;
15. x.p = y; 15. y->p = x;
16. return r; 16. return r;
9 10
11 12
Fix RB property Fix RB property
• Case 3: (node p has color red), property RB (4) is not satisfied. Node g has color black (as before • 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) 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 • 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 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 • 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
below) return to the case 3.2.1 (process as in the case 3.2.1) the color of p and g (Figure below)
13 14
15 16
THANK YOU !
17