Documente Academic
Documente Profesional
Documente Cultură
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
Black-Height of:
x=2 y=1 z=1
Contd.
A few RBT visual examples
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
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.
Search
Insertion
Deletion
Search
• Ensuring the validity of Red Black (RB) Properties may require certain
adjustments for fixing of any violations
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
• Cases
0: X is the root
(Possibility: inserting 1st node, propagated effect of fixing)
X 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
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
• 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
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
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
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)
If Deleted node is Right child then symmetric strategy for 3 and 4 is used.
Fixing the problem
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
V+ S V S
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
V
Case 4 diagrams
P P
Rotate
S V+ S
V+
P
Recolor
V
Asymptotic Cost of Deletion
• 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