0% found this document useful (0 votes)
2 views5 pages

week12_RedBlackTree

The document provides an overview of Red-Black Trees, a type of binary search tree characterized by specific properties regarding node colors and structure. It details the insertion process, including algorithms for left and right rotations, and outlines cases for fixing the Red-Black properties after insertion. The document also includes notations and lemmas relevant to the structure and performance of Red-Black Trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

week12_RedBlackTree

The document provides an overview of Red-Black Trees, a type of binary search tree characterized by specific properties regarding node colors and structure. It details the insertion process, including algorithms for left and right rotations, and outlines cases for fixing the Red-Black properties after insertion. The document also includes notations and lemmas relevant to the structure and performance of Red-Black Trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DATA STRUCTURES

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

Fix RB property Fix RB property


• Case 1: T is empty, make z as the root, assign black color to z • 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 2: (node p – parent of z – with black color) RB property is satisfied, do nothing
• Case 3.2: (node u (if exists) has color black)  perform a single-rotation or double-rotation
• Case 3: (node p has color red), property RB (4) is not satisfied. Node g has color black (as before depending on z is the left child or right child of p
insertion, T is a RB tree with RB property)
• 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
• Case 3.1: (node u (if exists) has color red)  change the color of p and u from red to black and the color of p and g (Figure below)
change the color of g by the color red, repeat this process with node g (Figure below).

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

Fix RB property Insertion: Fix RB property


• Case 3: (node p has color red), property RB (4) is not satisfied. Node g has color black (as before Insert(r, k) { insertFixUp(r,z) {
x = r; while z.p != NULL and z.p.color = ‘RED' do {
insertion, T is a RB tree with RB property) y = NULL; if z.p = z.p.p.left then {// z->p is a left-child of its parent
• Case 3.2: (node u (if exists) has color black)  perform a single-rotation or double-rotation while x != NULL do { u = z.p.p.right; // y is the uncle of z
depending on z is the left child or right child of p 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';
• 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 x = x->left; z = z.p.p;// repeat with the grand-parent of z
below) return to the case 3.2.3 (process as in the case 3.2.3) 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;
}

15 16
THANK YOU !

17

You might also like