Professional Documents
Culture Documents
RedBlack
RedBlack
1 2
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
38
bh = 1
50
h=1 • Need to prove two claims first …
NIL NIL bh = 1
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
7 8
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
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
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
• 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
21 22
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
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
35 36
35 36