0% found this document useful (0 votes)
18 views

RB Trees

The document discusses red-black trees, which are a type of self-balancing binary search tree. Red-black trees use an extra bit in each node to track the node's color as either red or black. This allows red-black trees to guarantee an O(log n) height, ensuring search, insert, and delete operations take O(log n) time. The document provides examples and proofs regarding the properties and height of red-black trees.

Uploaded by

Rahul Teja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

RB Trees

The document discusses red-black trees, which are a type of self-balancing binary search tree. Red-black trees use an extra bit in each node to track the node's color as either red or black. This allows red-black trees to guarantee an O(log n) height, ensuring search, insert, and delete operations take O(log n) time. The document provides examples and proofs regarding the properties and height of red-black trees.

Uploaded by

Rahul Teja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

CMPS 2200 – Fall 2017

Red-black trees
Carola Wenk

Slides courtesy of Charles Leiserson with changes by Carola Wenk

9/13/17 CMPS 2200 Intro. to Algorithms 1


Dynamic Set
A dynamic set, or dictionary, is a data
structure which supports operations
• Insert
7
• Delete 3 18

• Find 10 22
8 15

12 17

Using balanced binary search trees we can


implement a dictionary data structure such that
each operation takes O(log n) time.
9/13/17 CMPS 2200 Intro. to Algorithms 2
Search Trees
• A binary search tree is a binary tree. Each node stores a
key. The tree fulfills the binary search tree property:
For every node x holds:
• y  x , for all y in the subtree left of x
x
• x < y, for all y in the subtree right of x
7
x x
3 18

10 22

8 15

12 17

9/13/17 CMPS 2200 Intro. to Algorithms 3


Search Trees 7

3 18

Different variants of search trees: 1 12 10 22

8 15
• Balanced search trees (guarantee
height of O(log n) for n elements) 12 17

10 25

• k-ary search trees (such as B-trees, 6 12 15 21 30 45

2-3-4-trees) 2 7 8 11 14 20 23 24 27 40 50

17

• Search trees that store keys only 8 42

in leaves, and store copies of 1 14 35 43

keys as split-values in 1 6 12 17 26 41 43 59

internal nodes 6 8 12 14 26 35 41 42 59 61

9/13/17 CMPS 2200 Intro. to Algorithms 4


Balanced search trees
Balanced search tree: A search-tree data
structure for which a height of O(log 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

9/13/17 CMPS 2200 Intro. to Algorithms 5


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 is black.
3. The leaves (null’s) are black.
4. If a node is red, then both its children are black.
5. All simple paths from any node x, excluding x,
to a descendant leaf have the same number of
black nodes = black-height(x).
9/13/17 CMPS 2200 Intro. to Algorithms 6
Example of a red-black tree
7

3 18
null null
10 22 h=4
null
8 11 26
null null null null null null

1. Every node is either red or black.


9/13/17 CMPS 2200 Intro. to Algorithms 7
Example of a red-black tree
7

3 18
null null
10 22 h=4
null
8 11 26
null null null null null null

2., 3. The root and leaves (null’s) are black.


9/13/17 CMPS 2200 Intro. to Algorithms 8
Example of a red-black tree
7

3 18
null null
10 22 h=4
null
8 11 26
null null null null null null

4. If a node is red, then both its children are


black.
9/13/17 CMPS 2200 Intro. to Algorithms 9
Example of a red-black tree
7 bh = 2

3 18 bh = 2
null null
bh = 1 10 22 h=4
null
bh = 1 8 11 26
null null null null null null
bh = 0
5. All simple paths from any node x, excluding
x, to a descendant leaf have the same
number of black nodes = black-height(x).
9/13/17 CMPS 2200 Intro. to Algorithms 10
Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 log(n + 1).
Proof.
INTUITION: T
• Merge red nodes
into their black
parents.

9/13/17 CMPS 2200 Intro. to Algorithms 11


Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 log(n + 1).
Proof.
INTUITION: T
• Merge red nodes
into their black
parents.

9/13/17 CMPS 2200 Intro. to Algorithms 12


Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 log(n + 1).
Proof.
INTUITION: T
• Merge red nodes
into their black
parents.

9/13/17 CMPS 2200 Intro. to Algorithms 13


Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 log(n + 1).
Proof.
INTUITION: T
• Merge red nodes
into their black
parents.

9/13/17 CMPS 2200 Intro. to Algorithms 14


Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 log(n + 1).
Proof.
INTUITION: T’
• Merge red nodes
into their black
parents.

9/13/17 CMPS 2200 Intro. to Algorithms 15


Height of a red-black tree
Theorem. A red-black tree with n keys has height
h  2 log(n + 1).
Proof.
INTUITION: T’
• 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.
9/13/17 CMPS 2200 Intro. to Algorithms 16
Proof (continued)
• We have h  h/2,
since at most half T
the vertices on
any path are red.
• # leaves in T = # leaves in T’ h
• # leaves in T = n+1 (fact about
binary trees with exactly 2
children per internal node)
• # leaves in T’  2h’(fact about T’
binary trees; T’ can only have
more) h
 n + 1  2h'
 log(n + 1)  h'  h/2
 h  2 log(n + 1).
9/13/17 CMPS 2200 Intro. to Algorithms 17
Query operations
Corollary. The queries SEARCH, MIN,
MAX, SUCCESSOR, and PREDECESSOR
all run in O(log n) time on a red-black
tree with n nodes.
7
3 18
10 22
8 11 26

9/13/17 CMPS 2200 Intro. to Algorithms 18


Modifying operations
The operations INSERT and DELETE cause
modifications to the red-black tree:
1. the operation itself,
2. color changes,
3. restructuring the links of the tree
via “rotations”.

9/13/17 CMPS 2200 Intro. to Algorithms 19


Rotations
B RIGHT-ROTATE(B) A
A LEFT-ROTATE(A) B
 
   
• Rotations maintain the inorder ordering of keys:
a  , b  , c   a  A  b  B  c.
• Rotations maintain the binary search tree property
 Can be applied to any BST, not just red-black trees
• A rotation can be performed in O(1) time.
9/13/17 CMPS 2200 Intro. to Algorithms 20
B RIGHT-ROTATE(B) A

Rotation Example A

LEFT-ROTATE(A)

B

   

31 RIGHT-ROTATE(20) 31

9 41 9 41

6 20 39 42 6 16 39 42

2 8 16 22 2 8 15 20
In-order(v){
15 17 11 17 22
if v==null return;
In-order(v.left);
11 print(v.key+” ”);
In-order(v.right);
}
In-order traversal: In-order traversal:
2 6 8 9 11 15 16 17 20 22 31 39 41 42 2 6 8 9 11 15 16 17 20 22 31 39 41 42

 Maintains sorted order of keys, and can reduce height


9/13/17 CMPS 2200 Intro. to Algorithms 21
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 is black.
3. The leaves (null’s) are black.
4. If a node is red, then both its children are black.
5. All simple paths from any node x, excluding x,
to a descendant leaf have the same number of
black nodes = black-height(x).
9/13/17 CMPS 2200 Intro. to Algorithms 22
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 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.
10 22

8 11 26

15

9/13/17 CMPS 2200 Intro. to Algorithms 23


Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 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

9/13/17 CMPS 2200 Intro. to Algorithms 24


Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 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

9/13/17 CMPS 2200 Intro. to Algorithms 25


Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 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(18).
15

9/13/17 CMPS 2200 Intro. to Algorithms 26


Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 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(18). 15 26

9/13/17 CMPS 2200 Intro. to Algorithms 27


Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 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(18). 15 26
• LEFT-ROTATE(7)
9/13/17 CMPS 2200 Intro. to Algorithms 28
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
10
Example:
• Insert x =15. 7 18
• Recolor, moving the 3 8 11 22
violation up the tree. 15 26
• RIGHT-ROTATE(18).
• LEFT-ROTATE(7)
9/13/17 CMPS 2200 Intro. to Algorithms 29
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
10
Example:
• Insert x =15. 7 18
• Recolor, moving the 3 8 11 22
violation up the tree. 15 26
• RIGHT-ROTATE(18).
• LEFT-ROTATE(7) and recolor.
9/13/17 CMPS 2200 Intro. to Algorithms 30
Insertion into a red-black tree
IDEA: Insert x in tree. Color x red. Only red-
black property 4 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
10
Example:
• Insert x =15. 7 18
• Recolor, moving the 3 8 11 22
violation up the tree. 15 26
• RIGHT-ROTATE(18).
• LEFT-ROTATE(7) and recolor.
9/13/17 CMPS 2200 Intro. to Algorithms 31
Pseudocode
RB-INSERT(T, x)
TREE-INSERT(T, x)
color[x]  RED ⊳ only RB property 4 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
9/13/17 CMPS 2200 Intro. to Algorithms 32
Graphical notation

Let denote a subtree with a black root.

All ’s have the same black-height.

9/13/17 CMPS 2200 Intro. to Algorithms 33


Case 1
Recolor Continue
C C new x
A D y A D
x B B

(Or, A’s children are swapped.) Push C’s black onto A


p[x] = left[p[p[x]]
and D, and recurse,
since C’s parent may be
y  right[p[p[x]] red.
color[y] = RED
9/13/17 CMPS 2200 Intro. to Algorithms 34
Case 2

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

p[x] = left[p[p[x]]
y  right[p[p[x]]
Transform to Case 3.
color[y] = BLACK
x = right[p[x]]
9/13/17 CMPS 2200 Intro. to Algorithms 35
Case 3

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

Done! No more
p[x] = left[p[p[x]] violations of RB
y  right[p[p[x]] property 4 are
color[y] = BLACK possible.
x = left[p[x]]
9/13/17 CMPS 2200 Intro. to Algorithms 36
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(log n) with O(1) rotations.
RB-DELETE — same asymptotic running time
and number of rotations as RB-INSERT.

9/13/17 CMPS 2200 Intro. to Algorithms 37


Pseudocode (part II)
else “then” clause with “left” and “right” swapped
⊳ p[x] = right[p[p[x]]
then y  left[p[p[x]] ⊳ y = aunt/uncle of x
if color[y] = RED
then Case 1’
else if x = left[p[x]]
then Case 2’ ⊳ Case 2’ falls into Case 3’
Case 3’
color[root[T]]  BLACK

9/13/17 CMPS 2200 Intro. to Algorithms 38


Case 1’
Recolor Continue
C C new x
D y A D y A
x B x B

(Or, A’s children are swapped.) Push C’s black onto A


p[x] = right[p[p[x]]
and D, and recurse,
since C’s parent may be
y  left[p[p[x]] red.
color[y] = RED
9/13/17 CMPS 2200 Intro. to Algorithms 39
Case 2’

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

p[x] = right[p[p[x]]
y  left[p[p[x]]
Transform to Case 3’.
color[y] = BLACK
x = left[p[x]]
9/13/17 CMPS 2200 Intro. to Algorithms 40
Case 3’
LEFT-ROTATE(C)
C
x (and recolor) B
y B
C A

Done! No more
p[x] = right[p[p[x]] violations of RB
y  left[p[p[x]] property 4 are
color[y] = BLACK possible.
x = right[p[x]]
9/13/17 CMPS 2200 Intro. to Algorithms 41

You might also like