Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Red-Black Trees

• “Balanced” binary search trees guarantee an


O(log n) running time
Analysis of Algorithms • Red-black-tree
CS 435/610 – Binary search tree with an additional attribute for its
nodes: color which can be red or black
– Constrains the way nodes can be colored on any path
Red-Black Trees from the root to a leaf:
Instructor: Adrian Ionescu
Ensures that no path is more than twice, as long as, any other
path  the tree is balanced

Slide material due in part to G. Bebis


2

1 2

Example: RED-BLACK-TREE Red-Black-Trees Properties


(**Satisfy the binary search tree property**)
26

17 41
1. Every node is either red or black
NIL NIL
30 47 2. The root is black
3. Every leaf (NIL) is black
NIL 38 NIL 50
4. If a node is red, then both its children are black
NIL NIL NIL NIL
• No two consecutive red nodes on a simple path
• For convenience we use a sentinel NIL[T] to represent all
from the root to a leaf
the NIL nodes at the leafs
– NIL[T] has the same fields as an ordinary node 5. For each node, all paths from that node to descendant
– Color[NIL[T]] = BLACK leaves contain the same number of black nodes
– The other fields may be set to arbitrary values
3 4

3 4

Most important property of


Black-Height of a Node
Red-Black-Trees
h=4
26 bh = 2

h=1 h=3 A red-black tree with n internal nodes


17 41 bh = 2
bh = 1
has height at most 2log(n + 1)
NIL NIL h=2 h=2
30 bh = 1 47 bh = 1
h=1

38
bh = 1
50
h=1 • Need to prove two claims first …
NIL NIL bh = 1

NIL NIL NIL NIL

• Height of a node: the number of edges in the longest


path to a leaf
• Black-height of a node x: bh(x) is the number of
black nodes (including NIL) on the path from x to a leaf,
not counting x
5 6

5 6

1
Claim 1 Claim 2
• Any node x with height h(x) has bh(x) ≥ h(x)/2 • The subtree rooted at any node x contains
• Proof
– By property 4, at most h/2 red nodes on the path from the
at least 2bh(x) - 1 internal nodes
node to a leaf
h=4
– Hence at least h/2 are black bh = 2
26
h=4 h=1 h=3
26 bh = 2 bh = 1 17 41 bh = 2

h=1 h=3 h=2


NIL NIL h=2
bh = 1 17 41 bh = 2 30 bh = 1 47 bh = 1
h=1
h=2 bh = 1
NIL NIL h=2 h=1
30 bh = 1 47 bh = 1 NIL 38 NIL 50 bh = 1
h=1
bh = 1
h=1 NIL NIL NIL NIL
NIL 38 NIL 50 bh = 1

NIL NIL NIL NIL 7 8

7 8

Claim 2 (cont’d) Claim 2 (cont’d)


Proof: By induction on h[x] Inductive step:
• Prove it for h[x]=h
Basis: h[x] = 0 
• Let bh(x) = b, then any child y of x has:
x is a leaf (NIL[T])  – bh (y) = b (if the child is red), or
– bh (y) = b - 1 (if the child is black)
bh(x) = 0  x
NIL
# of internal nodes: 20 -1=0 x
bh = 2

26 bh = 2
y1 y2
bh = 1 17 41
Inductive Hypothesis: assume it is true for
NIL NIL 30 47
h[x]=h-1
NIL 38 NIL 50

9 NIL NIL 10

9 10

Claim 2 (cont’d) Height of Red-Black-Trees (cont’d)


• Using inductive hypothesis, the Lemma: A red-black tree with n internal nodes has
number of internal nodes for each
x h height at most 2log(n + 1). height(root) = h root
child of x is at least: Proof: bh(root) = b

2bh(x) - 1 - 1 l r h-1 n ≥ 2b - 1 ≥ 2h/2 - 1 l r


number n
• The subtree rooted at x contains at of internal since b  h/2
nodes
least:
• Add 1 to both sides and then take logs:
(2bh(x) - 1 – 1) + (2bh(x) - 1 – 1) + 1 = bh(l)≥bh(x)-1
n + 1 ≥ 2b ≥ 2h/2
2 · (2bh(x) - 1 - 1) + 1 = bh(r)≥bh(x)-1 log(n + 1) ≥ h/2 
2bh(x) - 1 internal nodes h ≤ 2 log(n + 1)
11 12

11 12

2
Operations on Red-Black-Trees INSERT
INSERT: what color to make the new node?
• The non-modifying binary-search-tree operations
• Red? Let’s insert 35!
MINIMUM, MAXIMUM, SUCCESSOR, – Property 4 is violated: if a node is red, then both its
children are black
PREDECESSOR, and SEARCH run in O(h) time
• Black? Let’s insert 14!
– They take O(log n) time on red-black trees – Property 5 is violated: all paths from a node to its leaves
contain the same number of black nodes
• What about TREE-INSERT and TREE-DELETE? 26

– They will still run in O(log n) 17 41

– We have to guarantee that the modified tree will still be


30 47
a red-black tree
38 50
13 14

13 14

26
DELETE 17 41
Rotations
DELETE: what color was the 30 47 • Operations for re-structuring the tree after insert
node that was removed? Black? 38 50 and delete operations on red-black trees
1. Every node is either red or black OK! • Rotations take a red-black-tree and a node within
2. The root is black Not OK! If removing
the root and the child the tree and:
3. Every leaf (NIL) is black OK!
that replaces it is red – Together with some node re-coloring they help restore
4. If a node is red, then both its children are black
the red-black-tree property
Not OK! Could change the Not OK! Could create – Change some of the pointer structure
black heights of some nodes two red nodes in a row – Do not change the binary-search tree property
5. For each node, all paths from the node to descendant
• Two types of rotations:
leaves contain the same number of black nodes
– Left & right rotations
15 16

15 16

Left Rotations Example: LEFT-ROTATE


• 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
17 18

17 18

3
LEFT-ROTATE(T, x) Right Rotations
1. y ← right[x] ►Set y • Assumptions for a right rotation on a node x:
2. right[x] ← left[y] ► y’s left subtree becomes x’s right subtree – The left child of y (x) is not NIL
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]] • Idea:
9. then left[p[x]] ← y – Pivots around the link from y to x
10. else right[p[x]] ← y – Makes x the new root of the subtree
11. left[y] ← x ► Put x on y’s left – y becomes x’s right child
12. p[x] ← y ► y becomes x’s parent – x’s right child becomes y’s left child
19 20

19 20

Insertion RB Properties Affected by Insert


1. Every node is either red or black OK!
• Goal:
2. The root is black If z is the root
– Insert a new node z into a red-black-tree  not OK
3. Every leaf (NIL) is black OK!
• Idea: 4. If a node is red, then both its children are black
– Insert node z into the tree as for an ordinary binary If p(z) is red  not OK
z and p(z) are both red
search tree OK!
5. For each node, all paths
– Color the node red
26
from the node to descendant
– Restore the red-black-tree properties 17 41
leaves contain the same number
• Use an auxiliary procedure RB-INSERT-FIXUP 38 47
of black nodes
50
21 22

21 22

RB-INSERT-FIXUP – Case 1 RB-INSERT-FIXUP – Case 1


z’s “uncle” (y) is red z’s “uncle” (y) is red
Idea: (z is a right child) Idea: (z is a left child)
• p[p[z]] (z’s grandparent) must • p[p[z]] (z’s grandparent) must be
be black: z and p[z] are both red black: z and p[z] are both red
• Color p[z] black • color p[z]  black
• Color y black • color y  black
• Color p[p[z]] red • color p[p[z]]  red
• z = p[p[z]] • z = p[p[z]]
– Push the “red” violation up the tree – Push the “red” violation up the tree
23 24

23 24

4
RB-INSERT-FIXUP – Case 3 RB-INSERT-FIXUP – Case 2
Case 3: Idea: Case 2:
• z’s “uncle” (y) is black • color p[z]  black • z’s “uncle” (y) is black
• z is a left child • color p[p[z]]  red • z is a right child
• RIGHT-ROTATE(T, p[p[z]]) Idea:
• No longer have 2 reds in a row • z  p[z]
• p[z] is now black • LEFT-ROTATE(T, z)
 now z is a left child, and both z and p[z] are red  case 3
Case 3
Case 2 Case 3

25 26

25 26

RB-INSERT-FIXUP(T, z) Example
1. while color[p[z]] = RED The while loop repeats only when Insert 4 Case 1 Case 2
case1 is executed: O(log n) times 11 11
2. do if p[z] = left[p[p[z]]] 2 14 2 14 y
Set the value of x’s “uncle”
3. then y ← right[p[p[z]]] z
1 7 15 1 7 15
4. if color[y] = RED
5 8 y 5 8
5. then Case1 z and p[z] are both red z and p[z] are both red
z 4
6. else if z = right[p[z]] z’s uncle y is red 4 z’s uncle y is black
z is a right child
7. then Case2 11 7
z
8. Case3 7 14 y
Case 3
2 11
9. else (same as then clause with “right” z
2 8 15 1 5 8 14
and “left” exchanged)
1 5 z and p[z] are red 4 15
We just inserted the root, or
10. color[root[T]] ← BLACK z’s uncle y is black
The red violation reached z is a left child
27 4 28
the root

27 28

26
RB-INSERT(T, z) RB-INSERT(T, z)
26

17 41 17 41

30 47 30 47
9. if y = NIL
38 50 The tree was empty: 38 50
1. y ← NIL • Initialize nodes x and y 10. then root[T] ← z set the new node to be the root
• Throughout the algorithm y points
2. x ← root[T] to the parent of x 11. else if key[z] < key[y]
Otherwise, set z to be the left or
3. while x  NIL 12. then left[y] ← z right child of y, depending on
whether the inserted node is
4. do y ← x • Go down the tree until 13. else right[y] ← z smaller or larger than y’s key
reaching a leaf
5. if key[z] < key[x] 14. left[z] ← NIL
• At that point y is the
6. then x ← left[x] parent of the node to be 15. right[z] ← NIL Set the fields of the newly added node
inserted
7. else x ← right[x] 16. color[z] ← RED
Fix any inconsistencies that could have
8. p[z] ← y • Sets the parent of z to be y 17. RB-INSERT-FIXUP(T, z) been introduced by adding this new red
node
29 30

29 30

5
Analysis of RB-INSERT Red-Black Trees - Summary
• Operations on red-black-trees:
• Inserting the new element into the tree O(log n)
– SEARCH O(h)
• RB-INSERT-FIXUP – PREDECESSOR O(h)
– SUCCESOR O(h)
– The while loop repeats only if CASE 1 is executed – MINIMUM O(h)
– MAXIMUM O(h)
– The number of times the while loop can be executed
– INSERT O(h)
is O(log n) – DELETE O(h)

• Total running time of RB-INSERT: O(log n) • Red-black-trees guarantee that the height of the
tree will be O(log n)
31 32

31 32

Problems Problems
• What is the ratio between the longest path and • What red-black tree property is violated in the tree
the shortest path in a red-black tree? below? How would you restore the red-black tree
property in this case?
– Property violated: if a node is red, both its children are black
- The shortest path is at least bh(root) – Fixup: color 7 black, 11 red, then right-rotate around 11

- The longest path is equal to h(root)


- We know that h(root)≤2bh(root)
7
z
- Therefore, the ratio is ≤2 2 11

1 5 8 14

4 15

33 34

33 34

Problems 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?
• When we insert a node into a red-black tree, we
– a: increases by 1 initially set the color of the new node to red. Why
– b: stays the same didn’t we choose to set the color to black?
– c: decreases by 1

• (Exercise 13.4-7, page 330) Would inserting a


new node to a red-black tree and then
immediately deleting it, change the tree?

35 36

35 36

You might also like