Sunteți pe pagina 1din 44

Red-Black Trees

Presentation by Aasim Ali (04-9174)


Red-Black Trees
• Definition: A Red-Black Tree (shortened as RB Tree or
RBT) is a Binary Search Tree (BST) where:
1. Every node is either RED or BLACK.
2. The root is BLACK
3. Every leaf (sentinel node) is BLACK.
4. If a node is RED, then both of its children are BLACK.
5. For each node, all paths from the node to descendant leaves contain
the same number of BLACK nodes.

• Definition: The BLACK-height of a node, x, in a Red-Black


Tree is the number of BLACK nodes on any path from x to
any leaf (sentinel node), not counting x.

• Definition: The BLACK-height of a Red-Black Tree is the


BLACK-height of Root node.
A few RBT visual examples

A valid Red-Black Tree

Black-Height of:
Black-Height of tree = 2
r=2 x=2
(since r is the root)
y=1 z=1
Contd.
A few RBT visual examples

A valid Red-Black Tree of Black-Height 3

Black-Height of:
x=2 y=1 z=1
Contd.
A few RBT visual examples

A valid Red-Black Tree of Black-Height 3

Black-Height of:
x=3 y=2 z=1
Contd.
A few RBT visual examples
ot
s a re n
f n ode e
a l l lea at sam
e n ce to be t
H ired e igh
q u h
re
i n a ry)
(ord

It is also a valid Red-Black Tree, because:


 No RED Child has RED parent
 Black-Height from any node to all its successor
leaf nodes is same
Theorems Related to RB Trees

Theorem 1:
Any Red-Black Tree with root x has
at least n = 2bh(x) – 1 internal nodes
where bh(x) is the black height of node x.

Proof:
By induction:
when h(x) = 0: 2bh(x) – 1 = 20 – 1 = 1 – 1 = 0
when h(x) > 0: (2bh(x)-1 – 1) + (2bh(x)-1 – 1) + 1 = 2bh(x) – 1
Contd.
Theorems Related to RB Trees

Theorem 2:
In a red-black tree, at least half the nodes on any path from the root to
a leaf must be black.

Proof:
If there is a red node on the path then there must be a corresponding
black node.
Contd.
Theorems Related to RB Trees

Theorem 3:
In a red-black tree, no path from any node, x, to a leaf is more than twice
as long as any other path from x to any other leaf.

Proof:
• Every path from a node to any leaf contains the same number of black
nodes [definition]
• At least ½ the nodes on any such path are black [Theorem 2]
• Therefore, there can be no more than twice as many nodes on any path
from x to a leaf as on any other path.
• Hence, the length of every path is no more than twice in length of any
other path from x to any other leaf
Contd.
Theorems Related to RB Trees

Theorem 4:
A red-black tree with n internal nodes has height h <= 2 lg(n + 1).

Proof:
Let h be the height of the red-black tree with rooted at x.

bh(x) >= h/2 [Theorem 2]


n >= 2bh(x) – 1 [Theorem 1]
Therefore
n >= 2 h/2 – 1
n + 1 >= 2h/2
lg(n + 1) >= h/2
2 lg(n + 1) >= h
Operations on Red-Black Tree

Search
Insertion
Deletion
Search

• Search works exactly as in ordinary Binary Search Tree (BST)

• Since h <= 2 lg (n + 1) hence search takes O (lg n) even in worst


case
(where h is height of the tree, and n is total number of nodes)
Mechanism for Insertion and Deletion

• Basic mechanism for Insertion & Deletion: same as in BST

• Ensuring the validity of Red Black (RB) Properties may require certain
adjustments for fixing of any violations

• Fixing adjustments include:


– Change of colors
– Rotations: Left rotation and Right rotation

• Two approaches are used for fixing:


– Top-Down:
Adjustments during the pre-insert/pre-delete downward traversal
(while moving from the root towards insertion/deletion point)
– Bottom-Up:
Post-insert/post-delete adjustments during the upward traversal
(while moving from the point of insertion/deletion towards root)

Only Bottom-Up cases are discussed further in this presentation


Possible Rotations in Red Black Fixing

Types of Rotations: (a) Left Rotation, (b) Right Rotation


Rotation is O(1) operation: simply requires update of fixed number of pointers

Left Rotation: Rotate R around P Right Rotation: Rotate L around P


G G G

P P P

L R L RL R

L
L L
R L
R R
R L
L
R
L L
R
R
R
L
L L
R R
L R
R

Original Structure
<BACK>
Red-Black Trees

Bottom-Up Insertion
Bottom–Up Insertion

• Insert node as in BST


• Color the Node RED
• Two Red-Black properties may be violated?
1. Every node is Red or Black
2. Root is Black
3. Sentinel nodes are Black
4. If node is Red, both children must be Black
5. Every path from node to descendant leaf must contain the same
number of Blacks
Cases of Bottom-Up Insertion

• Insert node; Color it RED; X is pointer to it

• Cases

0: X is the root
(Possibility: inserting 1st node, propagated effect of fixing)

1: Both parent and uncle are RED


(Only this case may need recursive fixing)

Two further cases when Parent is RED, and uncle is BLACK:


2: (zig-zag) –X and its parent are opposite type (left/right) children
3: (zig-zig) –X and its parent are both left or both right children
Terminology for Insertion
• The node causing violation is pointed to by X (being the
new node in the start of Insertion Fixing)
• The parent of X is P
• The uncle of X is U
• The grandparent of X is G
• The Sibling of X is S

Red Node Black Node Sentinel Node


Bottom-Up Insertion
Case 0

X X

Case 0 – X is root (Trivial case)


 Just change the color of X to Black
(This is the only case that increases Black-Height of the tree)
Bottom-Up Insertion
Case 1

Case 1 – Both P and U are RED X


Just Recolor and move up:
 Color parent and uncle BLACK
G
 Color grandparent RED P
 Point X to grandparent U
 Check for violation in new situation
(it may invoke any of four cases) X
X and P can be either child;
it does not affect the strategy
Bottom-Up Insertion
Case 2

Case 2 – Zig-Zag: P is RED; U is BLACK;


P & X are opposite children G
Double Rotate & Recolor:
X P is left
 Left rotate X around P U
 Right rotate X around G
S X isright
 Swap colors of G and X

This simulation shows Case 2 when X is right child and P is left child
Symmetric handling is needed when X is left child and P is right child
Bottom-Up Insertion
Case 3

Case 3 – Zig-Zig : P is RED; U is BLACK;


P & X are same direction children G
Single Rotate and Recolor:
 Right rotate P around G P U
 Swap colors of P and G
S
X

This simulation shows Case 3 when X and P both are left children
Symmetric handling is needed when X and P both are right children
Asymptotic Cost of Insertion

• O(lg n) to descend to insertion point


• O(1) to do insertion
• O(lg n) to ascend and readjust == worst case only for
insert-fix case 1

• Total: O(log n)
Examples of Insertion

Let’s Insert: 80
96, 97, 99
60 88

45 75 84 95

18 55 70 77 82 86 92 97
98

10 25 50 65 72 96
96 98
97
5 15 20 30

Insert 96 as
Insert 97 RED: No Needs
as RED: fixing fixing
needed
case 2 (zig-zag)
Contd.
Examples of Insertion

Let’s Insert: 80
96, 97, 99
60 88

45 75 84 95

18 55 70 77 82 86 92 97 Case 1

10 25 50 65 72
96 98

Case 1
99
5 15 20 30

Insert 99 as RED: Needs fixing


Contd.
Examples of Insertion

Let’s Insert: 80
3, 56, 57
60 88
18 45 75 84 95

18 45
55 65 77 82 86 92 97
Case 3 10 25 50 96 98
(zig-zig)

99
5 15 20 30
3 Case 1 Insert 3 as RED: Needs fixing
Contd.
Examples of Insertion

Let’s Insert: 80 Case 0


3, 56, 57
60 88
18 Case 1 75 84 95
10 45 65 77 82 86 92 97

5 15 25 55 Case 1 96 98
50 56 99
3
20 30 57 Case 1

Insert 5657
Insert as as
RED: NoNeeds
RED: fixingfixing
needed
Red-Black Trees

Bottom-Up Deletion
(starting with BST delete)
Ordinary BST Delete

1. Leaf: just delete it.

3. Has just one child: replace it with that child

5. Has two children then Delete-by-Copy:


copy the value of in-order predecessor in the node
(selected for deletion) then delete the in-order
predecessor (bringing it to case 1 or 2 above)
Terminology for Deletion

• U was physically deleted Black node


• V takes U’s place, thus has an extra unit of blackness
• P is the parent of V
• S is the sibling of V
• + indicates extra blackness:
P+ means P with extra blackness
• Sentinel nodes are not shown

Black Node Red or Black, and don’t care

Red Node Sentinel Node


RB Properties Verification

Let’s analyze the effect of simple BST Delete on RBT


Properties

3. If U is RED?
Not a problem – no RB properties violated

5. If U is BLACK?
(a) U is not root: black-height of some paths needs fixing
(b) U is root: even then some verification is needed
Sibling of a non-root BLACK node is guaranteed to exist
Cases of Bottom-Up Deletion
• Cases (caused by deletion of a BLACK node)
1: Sibling of deleted node is RED (which implies: the Sibling has at least
one BLACK child, and the parent of deleted node is BLACK)

Three more cases when Sibling is BLACK


2: Both children (internal or external) of Sibling are BLACK
3: Deleted node is Left child and only the left child of Sibling is RED
4: Deleted node is Left child and at least right child of Sibling is RED

If Deleted node is Right child then symmetric strategy for 3 and 4 is used.
Fixing the problem

• Think of V as having an “extra” unit of BLACKNESS:


– It may be absorbed into the tree (by a RED node), or
– It may propagated up to the root and out of the tree

• There are four cases if V is a left child; and four symmetric


cases if V is a right child

• V as left child is illustrated in the following slides


Bottom-Up Deletion
Case 1

• V’s sibling, S, is Red


– Rotate S around P and recolor S & P
• NOT a terminal case – One of the other cases will now apply
• All other cases apply when S is Black
P

U S
V
Case 1 Diagram

P P
Rotate

V+ S V+ S

S
Recolor
P

V+
Bottom-Up Deletion
Case 2
• V’s sibling, S, is black and has two black children
– Recolor S to be Red
– P absorbs V’s extra blackness
• If P is Red, we’re done
• If P is Black, it now has extra blackness and
problem has been propagated up the tree

U S

V
Case 2 diagram

Recolor and absorb


P P+

V+ S V S

Either extra black absorbed by P or


P now has extra blackness
Bottom-Up Deletion
Case 3
• S is Black,
S’s right child is Black and
S’s left child is Red
– Rotate
}
S’s left child around S
– Swap color of S and S’s left child
– Now in case 4 P

U S

V
Case 3 Diagrams

P
P
V+ S
V+
P
S
Rotate V+ S
Recolor
Bottom-Up Deletion
Case 4
• S is black,
S’s RIGHT child is RED
(Left child either color)
– Rotate S around P
}
– Swap colors of S and P, and color S’s Right child Black

• This is the terminal case P


It does not cause recursive call
U S

V
Case 4 diagrams

P P
Rotate
S V+ S
V+

P
Recolor

V
Asymptotic Cost of Deletion

• O(lg n) to descend to deletion point


• O(1) to do deletion
• O(lg n) to ascend and readjust == worst case only for
delete-fixing case 1

• Total: O(log n)
Examples of Deletion

Let’s Delete: 80
20, 25, 30
60 88

45 75 84 95

18 55 70 77 82 86 92 97
30 50
10 25 65 72

5 15 20 30

Delete
Delete 20
25 30 (RED): No
(BLACK): fixing
Needs
(BLACK): needed
trivial
Fixing Casefixing
4
Extra
Extra blackness
blackness of of U=25
U=30 is be
will absorbed by by
absorbed V=30
P=18
References

• Cormen, Leiserson, Rivest, Stein. Introduction to


Algorithms, 2nd Ed. May 2001. May 2001.

• The following URL has a tutorial on Red-Black Trees


[Julienne Walker and The EC Team]: Collection of
Tutorials on Data Structures
http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.a

S-ar putea să vă placă și