DS-RedBlackTrees-v1.00
DS-RedBlackTrees-v1.00
2
Example: RED-BLACK-TREE
26
17 41
NIL NIL
30 47
NIL 38 NIL 50
h=1 h=3
bh = 1 17 41 bh = 2
h=1 h=3
bh = 1 17 41 bh = 2
h=4
26 bh = 2
h=1 h=3
bh = 1 17 41 bh = 2
NIL NIL 30 47
NIL 38 NIL 50
NIL NIL
Claim 2 (cont’d)
◼ Using inductive hypothesis, the number of
internal nodes for each child of x is at least: x h
2bh(x) - 1 - 1
◼ The subtree rooted at x contains at least: r
l h-1
(2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1 =
2 · (2bh(x) - 1 - 1) + 1 =
2bh(x) - 1 internal nodes
bh(l)≥bh(x)-1
bh(r)≥bh(x)-1
Height of Red-Black-Trees
(cont’d)
Lemma: A red-black tree with n internal nodes has
height at most 2lg(n + 1).
height(root) = h root
Proof: bh(root) = b
n ≥ 2b – 1 ≥ 2h/2 - 1
number n l r
since b h/2
of internal
nodes
17 41
30 47
38 50
26
DELETE 17 41
30 47
DELETE: what color was the
node that was removed? Black? 38 50
1. Every node is either red or black OK!
2. The root is black Not OK! If removing the root
and the child that replaces it
3. Every leaf (NIL) is black OK!
is red
4. If a node is red, then both its children are black
Not OK! Could change the Not OK! Could create
black heights of some nodes two red nodes in a row
5. For each node, all paths from the node to descendant leaves
contain the same number of black nodes
Rotations
◼ Operations for re-structuring the tree after insert and
delete operations on red-black trees
◼ Rotations take a red-black-tree and a node within the
tree and:
◼ Together with some node re-coloring they help restore the
red-black-tree property
◼ Change some of the pointer structure
◼ Do not change the binary-search tree property
◼ Two types of rotations:
◼ Left & right rotations
Left Rotations
◼ Assumptions for a left rotation on a node x:
◼ The right child of x (y) is not NIL
◼ Idea:
◼ Pivots around the link from x to y
◼ Makes y the new root of the subtree
◼ x becomes y’s left child
◼ y’s left child becomes x’s right child
Example: LEFT-ROTATE
LEFT-ROTATE(T, x)
1. y ← right[x] ►Set y
2. right[x] ← left[y] ► y’s left subtree becomes x’s right subtree
3. if left[y] NIL
4. then p[left[y]] ← x ► Set the parent relation from left[y] to x
5. p[y] ← p[x] ► The parent of x becomes the parent of y
6. if p[x] = NIL
7. then root[T] ← y
8. else if x = left[p[x]]
9. then left[p[x]] ← y
10. else right[p[x]] ← y
11. left[y] ← x ► Put x on y’s left
12. p[x] ← y ► y becomes x’s parent
Right Rotations
◼ Assumptions for a right rotation on a node x:
◼ The left child of y (x) is not NIL
◼ Idea:
◼ Pivots around the link from y to x
◼ Makes x the new root of the subtree
◼ y becomes x’s right child
◼ x’s right child becomes y’s left child
Insertion
◼ Goal:
◼ Insert a new node z into a red-black-tree
◼ Idea:
◼ Insert node z into the tree as for an ordinary
binary search tree
◼ Color the node red
◼ Restore the red-black-tree properties
◼ Use an auxiliary procedure RB-INSERT-FIXUP
RB Properties Affected by
Insert
1. Every node is either red or black OK!
2. The root is black If z is the root
3. Every leaf (NIL) is black OK! not OK
4. If a node is red, then both its children are black
If p(z) is red not OK
OK! z and p(z) are both red
5. For each node, all paths
26
from the node to descendant
17 41
leaves contain the same number
of black nodes 38 47
50
RB-INSERT-FIXUP – Case 1
z’s “uncle” (y) is red
Idea: (z is a right child)
◼ p[p[z]] (z’s grandparent) must be
black: z and p[z] are both red
◼ Color p[z] black
◼ Color y black
◼ Color p[p[z]] red
◼ z = p[p[z]]
◼ Push the “red” violation up the tree
RB-INSERT-FIXUP – Case 1
z’s “uncle” (y) is red
Idea: (z is a left child)
◼ p[p[z]] (z’s grandparent) must be
black: z and p[z] are both red
◼ Color p[z] black
◼ Color y black
◼ Color p[p[z]] red
◼ z = p[p[z]]
◼ Push the “red” violation up the tree
RB-INSERT-FIXUP – Case 3
Idea:
Case 3:
• color p[z] black
◼ z’s “uncle” (y) is black
• color p[p[z]] red
◼ z is a left child
• RIGHT-ROTATE(T, p[p[z]])
• No longer have 2 reds in a row
• p[z] is now black
Case 3
RB-INSERT-FIXUP – Case 2
Case 2:
◼ z’s “uncle” (y) is black
◼ z is a right child
Idea:
◼ z p[z]
◼ LEFT-ROTATE(T, z)
now z is a left child, and both z and p[z] are red case 3
Case 2 Case 3
RB-INSERT-FIXUP(T, z)
The while loop repeats only when
1. while color[p[z]] = RED case1 is executed: O(lgn) times
2. do if p[z] = left[p[p[z]]]
3. then y ← right[p[p[z]]] Set the value of x’s “uncle”
4. if color[y] = RED
5. then Case1
6. else if z = right[p[z]]
7. then Case2
8. Case3
9. else (same as then clause with “right”
and “left” exchanged)
10. color[root[T]] ← BLACK We just inserted the root, or
The red violation reached the
root
Example
Insert 4 Case 1 Case 2
11 11
2 14 2 14 y
1 7 15 1 7 z 15
5 8 y 5 8
z and p[z] are both red z and p[z] are both red
z 4 z’s uncle y is red 4 z’s uncle y is black
z is a right child
11 7
Case 3 z
7 14 y 2 11
z
2 8 15 1 5 8 14
17 41
RB-INSERT(T, z) 30 47
38 50
• Initialize nodes x and y
1. y ← NIL • Throughout the algorithm y points
2. x ← root[T] to the parent of x
3. while x NIL
4. do y ← x • Go down the tree until
reaching a leaf
5. if key[z] < key[x] • At that point y is the
6. then x ← left[x] parent of the node to be
inserted
7. else x ← right[x]
8. p[z] ← y • Sets the parent of z to be y
26
17 41
RB-INSERT(T, z) 30 47
38 50
9. if y = NIL The tree was empty:
10. then root[T] ← z set the new node to be the root
7
z
2 11
1 5 8 14
4 15
Problems
◼ Let a, b, c be arbitrary nodes in subtrees , , in
the tree below. How do the depths of a, b, c change
when a left rotation is performed on node x?
◼ a: increases by 1
◼ b: stays the same
◼ c: decreases by 1
Problems
◼ When we insert a node into a red-black tree, we
initially set the color of the new node to red. Why
didn’t we choose to set the color to black?