0% found this document useful (0 votes)
12 views56 pages

L17 Red Black Trees Clean

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views56 pages

L17 Red Black Trees Clean

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Lecture 17

Red-Black Trees

Slides taken from lectures by Prof. Naveen Garg


2-4 Tree
• insert, search, and delete of
O(log n)
• but nodes are dynamic with
2-4 keys
Can we have
benefits of 2-4 trees
with simple binary
nodes?
Lets convert 2-4 tree
to a binary tree!
Stretch it from top and
bottom!
a b c

d e m t A

f g h i j k l n o p q r s u v w x y B C D
Red Black Tree
b

a c

e m t A

d
l o r v y C D

g j k n p q s u w x B

f h i
Convert the below 2-4 tree to Red
Black Tree!
13

3 8 10 18

1 2 4 5 6 9 11 12 14 15 20

13

8 18

3 10 15 20

2 5 9 12 14

1 4 6 11
Red Black Trees
• A BST with Red and Black colored nodes
• Root is black
• Black node can have black children, but
red node can only have black children
• Every external node has the same black
depth
Red Black Tree
Examples

Black Black
height of height of
tree is 2 tree is 2
Are these Red-Black
Trees?

Double red Black height not uniform


Red-Black Tree to 2-
4 Tree
• Take a black node and its red
children and combine them into one
node of a 2-4 tree.
• Each node so formed has at least 1
and at most 3 keys.
• Black height = number of levels in 2-
4 tree
Convert Red-Black Tree to 2-4
Tree
9

4 13

2 7 11 19

1 3 5 17

4 9 13

1 2 3 5 7 11 1719
Height of Red-Black
Tree
Minimum number of
nodes for black
height h?
n≥2 -1
h+1

h≤log2(n+1)-1
Maximum number of
nodes for black
height h?
n≤2 2h+1
-1
n≤4 -1
h+1

h≥log4(n+1)-1
Red-Black Tree
Black Height
log4(n+1)-1 ≤ h ≤ log2(n+1)-1

1/2log2(n+1) ≤ h+1 ≤ log2(n+1)

O(log n)
Height of a Red-Black
tree is at max twice the
black height, i.e.
O(log n)
Insert (k)
• Search for k as in BST that will give
you the correct place to insert
• Create a new node at this place
and insert the key
• The new node is colored red
• Restore the red-black tree property
Two Cases
k k

No problem Double red problem

Red root problem


Color the root black
Case 1: Sibling of the
parent of the inserted node
is black or NULL
a
b

NULL or c a b k

k c

Equivalent 2-4 tree node contains {b,a,k} but malformed.


The rotation corrects the defect.
Case 2: Sibling of the
parent of the inserted
node is red
b
b
c a
c a
k
k

c b a
k c a k
The double red
problem can move
up the tree…
d

c a

What if d is root?
Time complexity
of insert?
• BST search O(log n)
• Rebalancing: 1 rotation, O(log n)
recoloring
• O(log n)
Delete
• To delete a node we proceed as
in a BST.
• Thus the node which is deleted is
the parent of an external node.
• Hence it is either a leaf or the
parent of a leaf.
Three Situations
1 19
11

17

17
Deleting black
Node
• Reduces the height by 1
• In general, it may reduce the
height of a subtree from h to h-1
The cases:
a is red a a is black

b is red a b is black
a
b a
b Both c a
are black Both c
c c b are
b Both d
are black c c black
c
Some c is
a
red d d a Some c is a
b Some d is red red
a b b
a
b c a
b
c
c b

d c
Deletion:
case1.1
a c
b b a
T4
h to h-1
h-1 T1 c T4
T1 T2 T3
h-1 h-1 h-1 h-1
h-1 T2 T3 h-1

a
a
b h to h-1
c h-1
b c

h-1 h-1
Deletion:
case1.2
a a
b b
h to h-1
h-1
h-1 h-1 h-1 h-1

b b a
Deletion:
case2.1.1 c
a
b a
b h to h-1

h c d
h h-1 h-1
d h-1
h-1 h-1
h-1 h-1

b a b c

d c d a
Deletion:
case2.1.2 b
a
a
b h
h to h-1 c h-1
c
h h-1 h-1
h-1 h-1
b a b

c c a
Deletion:
case2.2.1
a d

c c a
h to h-1

h-1 d
h-1 h-1 h-1 h-1

h-1 h-1
a d

c d c a
Deletion:
case2.2.2
a a

c c
h to h-1
h-1

h-1 h-1 h-1 h-1

a
c a

c
Summary
• In all cases, except 2.2.2, deletion can be
completed by a simple rotation/recoloring
• In case 2.2.2, the height of the subtree
reduces and so we need to proceed up
the tree.
• But in case 2.2.2 we only recolor nodes.
• Thus, if we proceed up the tree then we
only need to recolor. Eventually we would
do a rotation.
Insert and
Delete Summary
• In both insertion and deletion we
need to make at most one rotation.
• We might have to move up the tree
but in doing so we only recolor nodes.
• Time taken is O(log n).
In what situations would
you prefer red-black tree
over AVL Tree?
(a,b) Trees
• A multiway search tree.
• Each node has at least a and at
most b children.
• Root can have less than a children
but it has at least 2 children.
• All leaf nodes are at the same level.
• Height h of (a,b) tree is at least log b
n and at most loga n.
B-Tree
• Every leaf has the same depth.
• Except the root, all nodes have
between t and 2t children.
• The root node has between 0
and 2t children.
• Height is O(log n)
• A large value of t is used for disk
based storage.
Examples

Courtesy: Jan Manuch


Insertion
Insert 65 Example
47

32 71

93
Insertion
Insert 65 Example
47

32 71

65 93
Insertion
Insert 65
Insert 82
Example
47

32 71

65 93
Insertion
Insert 65
Insert 82
Example
47

32 71

65 93

82
Insertion
Insert 65
Insert 82
Example
47

32 71

65 93

82
Insertion
Insert 65
Insert 82
Example
47
Insert 87
32 71

65 93

82
Insertion
Insert 65
Insert 82
Example
47
Insert 87
32 71

65 93

82

87
Insertion
Insert 65
Insert 82
Example
47
Insert 87
32 71

65 93

82

87
Insertion
Insert 65
Insert 82
Example
47
Insert 87
32 71

65 87

82 93
Deletion
Delete 87 Example 1
47

32 71

65 87
82

82 93
Deletion
Delete 71 Example 2 47

32 71
65

65 87

Replace with predecessor


Attach predecessor’s child
51 82 93
Deletion
Delete 32 Example 3
47

32 71

65 87

82 93
Deletion
Delete 32 Example 3
47

32 71

65 87

82 93
Deletion
Example 3 71

47 87

65 82 93
Deletion
Delete 25 Example 4
47

32 71

25 40
65 87

50
82 93
Deletion
Delete 25 Example 4
47

32 71

25 40
65 87

50
82 93
Deletion
Example 4
47

32 71

40
65 87

50
82 93

You might also like