0% found this document useful (0 votes)
20 views32 pages

Red-Black Trees Operation

The document discusses red-black trees, a type of balanced search tree that ensures O(lg n) height for dynamic sets of n items. It outlines the properties of red-black trees, the process of insertion and deletion, and the necessary rotations to maintain balance. The operations of searching, inserting, and deleting in red-black trees are all performed in O(lg n) time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views32 pages

Red-Black Trees Operation

The document discusses red-black trees, a type of balanced search tree that ensures O(lg n) height for dynamic sets of n items. It outlines the properties of red-black trees, the process of insertion and deletion, and the necessary rotations to maintain balance. The operations of searching, inserting, and deleting in red-black trees are all performed in O(lg n) time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 32

Red-black Trees, Rotations,

Insertions, Deletions

CSE18R5002-Advanced Data Structure

Dr.K.Kartheeban
Associate Professor/CSE
Balanced search trees
Balanced search tree: A search-tree data
structure for which a height of O(lg n) is
guaranteed when implementing a dynamic
set of n items.
• AVL trees
• 2-3 trees
Examples: • 2-3-4 trees
• B-trees
• Red-black trees
L10.2

Red-black trees
This data structure requires an extra
one-bit color field in each node.
Red-black properties:
1. Every node is either red or black.
2. The root and leaves (NIL’s) are black.
3. If a node is red, then its parent is black.
4. All simple paths from any node x to a
descendant leaf have the same number
of black nodes = black-height(x).
Example of a red-black tree
7

3 18
NILNIL 10 22 h=4

NIL
8 11 26
NIL NIL NIL NIL NIL NIL

L10.4
Example of a red-black tree
7

3 18
NIL NIL
10 22
NIL
8 11 26
NIL NIL NIL NIL NIL NIL

1. Every node is either red or black.


L10.5
Example of a red-black tree
7

3 18
NIL NIL
10 22
NIL
8 11 26
NIL NIL NIL NIL NIL NIL

2. The root and leaves (NIL’s) are black.


L10.6
Example of a red-black tree
7

3 18
NIL NIL
10 22
NIL
8 11 26
NIL NIL NIL NIL NIL NIL

3. If a node is red, then its parent is black.


L10.7
Example of a red-black tree
7 bh = 2

3 18 bh = 2
NILNIL
bh = 1 10 22
NIL
bh = 1 8 11 26
bh = 0 NIL NIL NIL NIL NIL NIL
4. All simple paths from any node x to a
descendant leaf have the same number of
black nodes = black-height(x).
L10.8
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:
• Merge red
nodes into their
black parents.

L10.9
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:
• Merge red
nodes into their
black parents.

L10.10
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:
• Merge red
nodes into their
black parents.

L10.11
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:
• Merge red
nodes into their
black parents.

L10.12
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:
• Merge red
nodes into their
black parents.

L10.13
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 lg(n + 1).
Proof. (The book uses induction. Read carefully.)
INTUITION:
• Merge red nodes
h
into their black
parents.
• This process produces a tree in which each node
has 2, 3, or 4 children.
• The 2-3-4 tree has uniform depth h of leaves.
L10.14
Proof (continued)
• We have
h h/2,
since at most h
half
the leaves on any
path are red.
• The number of leaves
in each tree is n + 1 h
h'
n+12 
 lg(n + 1)  h'  h/2
 h  2 lg(n + 1).
L10.15
Query operations
Corollary. The queries SEARCH, MIN,
MAX, SUCCESSOR, and PREDECESSOR
all run in O(lg n) time on a red-black
tree with n nodes.

L10.16
Modifying operations
The operations INSERT and DELETE cause
modifications to the red-black tree:
• the operation itself,
• color changes,
• restructuring the links of the tree:
“rotations”.

L10.17
Rotations
B RIGHT-ROTATE(A) A
A LEFT-ROTATE(B) B
 
   

Rotations maintain the inorder ordering of keys:


• a , b , c   a  A  b  B
 c. A rotation can be performed in O(1)
time.
L10.18
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
7
Example:
3 18

10 22

8 11 26

L10.19
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
7
Example:
3 18
• Insert x =15.
• Recolor, moving the 10 22
violation up the tree. 8 11 26

15

L10.20
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
7
Example:
3 18
• Insert x =15.
• Recolor, moving the 10 22
violation up the tree. 8 11 26
• RIGHT-ROTATE(10).
15

L10.21
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
7

Example:
3 10
• Insert x =15.
• Recolor, moving the 8 18
violation up the tree. 11 22
• RIGHT-ROTATE(10). 15 26
• LEFT-ROTATE(7) and recolor.
L10.22
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
Example:
10

• Insert x =15. 7 18
• Recolor, moving the 3 8 11 22
violation up the tree. 15 26
• RIGHT-ROTATE(10).
• LEFT-ROTATE(7) and recolor.
L10.23
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
Example:
10

• Insert x =15. 7 18
• Recolor, moving the 3 8 11 22
violation up the tree. 15 26
• RIGHT-ROTATE(10).
• LEFT-ROTATE(7) and recolor.
L10.24
Pseudocode
RB-INSERT(T, x)
TREE-INSERT(T, x)
color[x]  RED ⊳ only RB property 3 can be violated while x  root[T] and color[p[x]] = RED
do if p[x] = left[p[p[x]]
then y  right[p[p[x]] ⊳ y = aunt/uncle of x

if color[y] = RED
then Case 1
else if x = right[p[x]]
then Case 2 ⊳ Case 2 falls into Case 3

Case 3
else “then” clause with “left” and “right”
swapped color[root[T]]  BLACK
L10.25
Graphical notation

Let denote a subtree with a black root.

All ’s have the same black-height.

L10.26
Case 1

Recolor
C C new x
y
A D A D
x B B

(Or, children of Push C’s black onto


A are swapped.) A and D, and recurse,
since C’s parent may
be red.
L10.27
Case 2

C LEFT-ROTATE(A) C
y y
A B
x B x A

Transform to Case 3.

L10.28
Case 3

C
RIGHT-ROTATE(C)
B
y
B
A C
x A

Done! No more
violations of RB
property 3 are
possible.
L10.29
Analysis

• Go up the tree performing Case 1, which


only recolors nodes.
• If Case 2 or Case 3 occurs, perform 1 or 2
rotations, and terminate.
Running time: O(lg n) with O(1) rotations.
RB-DELETE — same asymptotic running
time (see textbook).

L10.30

You might also like