Professional Documents
Culture Documents
Red-Black tree
Recall binary search tree
Key values in the left subtree <= the node value
Key values in the right subtree >= the node value
Operations:
insertion, deletion
Search, maximum, minimum, successor,
predecessor.
O(h), h is the height of the tree.
Red-Black Tree
A red-black tree is a binary search tree with the
following properties:
Red-black properties:
1. Every node is either red or black.
2. The root and leaves (NIL’s) are black.
3. If a node is red, then its parent is black.
4.All simple paths from any node x to a
descendant leaf have the same number of black
nodes = black - height(x).
L PARENT R
I
E KEY G
F H
T COLOR T
Red-Black Tree
2 6
4 9
5 11
7
Height Lemma
A red-black tree with n internal nodes has height ≤ 2 lg(n+1).
Proof:
1) any node with height h has black-height ≥ h/2.
2) the subtree rooted at any node x contains ≥ 2bh(x) – 1 internal
nodes.
Proof by induction on height of x.
2 5
Binary Search Tree
1 3
Insert Into RB Tree
Violation to the RB Tree Characteristics – Because
of the Insertion…The Violation can be on any of the
below RB Tree properties..
Recolor
z
z
Insert Into RB Tree Algorithm
Case 2: ‘z’s Uncle/Aunt ‘y’ is Black and ‘z’ is a Right Child
C
y y
B
z z A
Insert Into RB Tree Algorithm
Case 3: ‘z’s uncle ‘y’ is Black and ‘z’ is a Left Child
z
Analysis of Insert operation in
a RB Tree
O(lg n) time to get through RB-INSERT up to the call of RB-
INSERT-FIXUP.
Within RB-INSERT-FIXUP :
Each iteration takes O(1) time.
Each iteration is either the last one or it moves z up 2 levels.
O(lg n) levels -> O(lg n) time.
Also note that there are at most 2 rotations overall.
40
20 50
15 30
10
One More Example
11 Steps:
BST Insert
2 14 Case 1
Case 2
Case 3
1 7 15
5 8
2 11
1 5
8 14
4 15
Solution
Deletion
Deletion, like insertion, should preserve all the
RB properties.
The properties that may be violated depends on
the color of the deleted node.
» Red – OK. Why?
» Black?
Steps Involved are:
» Do regular BST deletion.
» Fix any violations of RB properties that may result.
Violation of RB Properties
If the node supposed to be deleted “Red-black properties:”
‘y ‘ is black, we could have 1. Every node is either red or
violations of redblack black.
properties:
2. The root is black.
Prop. 1. OK.
3. Every leaf (nil[T]) is black.
Prop. 2. If y is the root and x is red,
then the root has become red. 4. If a node is red, then both
its children are black.
(Hence no two reds in a
Prop. 3. OK.
row on a simple path from
the root to a leaf.)
Prop. 4. Violation if p[y] and x are
both red.
5. For each node, all paths
from the node to
Prop. 5. Any path containing y now descendant leaves contain
has 1 fewer the same number of black
black node. nodes.
Violation of RB Properties
RB Delete Fixup Algorithm
Case -1 “x’s Sibling ‘w’ is RED
1. Take 1 black off x (-> singly black) and off w (-> red).
Move that black to p[x]
2. Do the next iteration with p[x] as the new x.
INFO: If entered this case from case 1, then p[x] was red ->
new x is red & black -> color attribute of new x is RED -> loop
terminates. Then new x is made black in the last line.
Case -3
15
10
5 12
3 7 11 13 Valid and
2 4 6 9 unaffected
Right subtree
Delete 11 from RB Tree
Example
15
10
3 7 11 13
x w Step 1 - Case 2
2 4 6 9
15
10
3 7 11 13
Step 1 - Case 2
2 4 6 9
15
10
3 7 11 13
Step 2 - Case 1
2 4 6 9
15
10
3 7 11 13
Step 2 - Case 1
2 4 6 9
15
Right Rotate 10
3 7 11 13
Step 2 - Case 1
2 4 6 9
15
2 4 7 w 12 x
Step 2 - Case 2
6 9 11 13
15
2 4 7 12
Step 2 - Case 2
6 9 11 13
15
2 4 7 12
Step 3 –
6 9 13 if ‘x’ is Red –
Come out of While Loop
And Recolor ‘x’ to Black
Within RB-DELETE-FIXUP :
Case 2 is the only case in which more iterations
occur.
x moves up 1 level.
Hence, O(lg n) itrations.
Each of cases 1, 3, and 4 has 1 rotation leads to
≤ 3 rotations in all.
Hence, O(lg n) time.