# Red Black Tree - PowerPoint PPT Presentation

Loading...

PPT – Red Black Tree PowerPoint presentation | free to download - id: 67c53b-ZDA5N

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

## Red Black Tree

Description:

### Red Black Tree Outline Red-Black Trees Motivation Definition Operation Motivation Binary Search Trees should be balanced. AVL Trees need 2 passes: top-down insertion ... – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Date added: 23 July 2019
Slides: 33
Provided by: dn5
Learn more at: http://staf.cs.ui.ac.id
Category:
Tags:
User Comments (0)
Transcript and Presenter's Notes

Title: Red Black Tree

1
Red Black Tree
2
Outline
• Red-Black Trees
• Motivation
• Definition
• Operation

3
Motivation
• Binary Search Trees should be balanced.
• AVL Trees need 2 passes top-down
insertion/deletion and bottom-up rebalancing
• Need recursive implementation
• Red-Black Trees need 1 pass top-down rebalancing
and insertion/deletion
• Can be implemented iteratively, faster
• Red-Black Trees have slightly weaker balance
restrictions
• Less effort to maintain
• In practice, worst case is similar to AVL Trees

4
Red-Black Trees Definition
• Rules of Red-Black Trees
• Every node is colored either red or black
• The root is black
• If a node is red, its children must be black
• consecutive red nodes are disallowed
• Every path from a node to a null reference must
contain the same number of black nodes
• Convention null nodes are black

5
Red-Black Trees
• The insertion sequence is 10, 85, 15, 70, 20, 60,
30, 50, 65, 80, 90, 40, 5, 55

30
15
70
10
85
60
20
5
50
80
90
65
40
55
6
Red-Black Trees Properties
• Each path must contain the same number of black
nodes. (Rule 4)
• Consecutive red nodes are not allowed. (Rule 3)
• The longest path is at most twice the length of
the shortest path

7
Red-Black Trees Properties
• B total black nodes from root to leaf
• N total all nodes
• H height

All operations guaranteed logarithmic!
8
Insertion
• A new node must be colored red
• Why?
• A new item is always inserted as a leaf in the
tree
• If we color a new item black, then the number of
black nodes from root would be different (violate
property 4)
• If the parent is black, no problem.
• If the parent is red, we create two consecutive
red nodes (violate property 3)
• Thus, we have to do some rotating/recolouring
• Remember convention null nodes are black

9
Single Rotation
• Case after insertion
• Consecutive red (P X)
• Sibling of parent (S) is black
• X is outer node (left-left or right-right)
• X new node
• P parent
• S sibling
• G Grandparent

10
Double Rotation
• Case after insertion
• Consecutive red (P X)
• Sibling of parent (S) is black
• X is inner node (left-right or left)
• X new node
• P parent
• S sibling
• G Grandparent

11
Single Rotation (bottom-up)
• Case after insertion
• Consecutive red
• Sibling of parent is red
• Outer node (left-left or right-right)

But what if Ps parent is red? ? We have to keep
going up the tree all the way to the root ?
12
Top-Down Insertion
• The solution prevent S from ever being red!
• Starting from the root (searching for insertion
point)
• Never allow 2 red siblings
• If we see a node X with 2 red children, do a
colour flip.

13
Color Flip
• Maintains property 4
• Possible violation of 3 if Xs parent is red!
• Do single or double rotation
• Xs parents sibling can never be red!
• Set the root to black (to maintain property 2)

14
Color Flip (2)
If we do the colour flipping on the way down to
the insertion point, we will never reach a
condition where P S are red!
15
Example Insert 18
30
15
70
10
85
60
20
5
50
80
90
65
55
40
16
Example Insert 2
30
15
70
10
85
60
20
5
50
80
90
65
55
40
17
Example Insert 2
30
15
70
5
85
60
20
10
2
50
80
90
65
55
40
18
Example Insert 45 (Illustration)
30
15
70
5
85
60
20
10
2
50
80
90
65
55
40
19
Example Insert 45 (Top-Down Color Flip)
30
15
70
5
85
60
20
10
2
50
80
90
65
55
40
20
Example Insert 45 (Top-Down Color Flip)
30
15
70
5
85
60
20
10
2
50
80
90
65
55
40
21
Example Insert 45 (Single Rotate)
30
15
70
5
85
60
20
10
2
50
80
90
65
55
40
22
Example Insert 45 (Single Rotate)
30
15
60
50
5
20
70
85
10
2
65
55
40
80
90
23
Red-Black Tree Insertion Exercise
• The insertion sequence is 10, 85, 15, 70, 20, 60,
30, 50, 65, 80, 90, 40, 5, 55

24
Red-Black Tree Deletion
• Deletion in BST only leaf nodes or nodes with
one child are really deleted (Why?)
• If the deleted node is red no problem (all
properties maintained).

Leaf nodes
Single child nodes
25
Top-Down Deletion
• If node to be deleted is black ? violate property
4
• Always ensure that the node to be deleted is red.
• Top-down traversal from root (looking for node to
be deleted)

X visited node P parent S sibling
Idea make sure that X is red!
26
Possible cases
• P is red (inductive invariant)
• X and S are black (result of property 3)
• 2 cases
• 1. Both Xs children (A B) are black
• 2. X has at least one red child (A, B, or both)

27
Case 1 Both Xs children are black
• Depends on children of S (C D)
• Both C D are black simply colour-flip

28
Case 1 Both Xs children are black
• Outer child of S (C) is Red do a single rotation
and recolour

29
Case 1 Both Xs children are black
• Inner child of S (C) is Red do a double rotation
and recolour

30
Case 2 One/Both of Xs children is red
• Recurse down to Xs child
• If we land on a red node, fine.
• If we land on a black node, rotate sibling and
parent

S
P
P
D
S
X
C
X
B
A
D
C
B
A
31
Summary
• Red-Black trees use color as balancing
information instead of height in AVL trees.
• An insertion may cause a local perturbation (two
consecutive red nodes)
• The pertubation is either
• resolved locally (rotations), or
• propagated to a higher level in the tree by
recoloring (color flip)
• O(1) for a rotation or color flip
• At most one restructuring per insertion.
• O(log n) color flips
• Total time O(log n)

32
Further Reading
• http//telaga.cs.ui.ac.id/WebKuliah/IKI10100/resou
rces/animation/data-structure/redblack/redblack.ht
ml (mirror from http//www.ece.uc.edu/franco/C321
/html/RedBlack/redblack.html)
• Chapter 19.5
About PowerShow.com