color | key | left | right | p |
Red-black properties:
Lemma: A red-black tree with n internal nodes has height at most 2⋅log(n+1)
Definiton: The black-height of a node x, denoted bh(x), is the number of black nodes on any simple path from, but not including, x down to a leaf.
Proof outline:
Does the search tree property continue to be satisfied after a rotation?
Can a rotation change the height of the tree?
Left-Rotate (T, x)
1 y = x.right // set x
2 x.right = y.left // turn y's subtree into x's right subtree
3 if y.left ≠ T.nil
4 y.left.p = x
5 y.p = x.p // link x's parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x // put x on y's left
12 x.p = y
Tree-Insert (T, z)
1 y = NIL
2 x = T.root
3 while x ≠ NIL
4 y = x
5 if z.key ≤ x.key
6 x = x.left
7 else x = x.right
8 z.p = y
9 if y == NIL
10 T.root = z
11 elseif z.key < y.key
12 y.left = z
13 else y.right = z
RB-Insert (T, z) 1 y = T.nil 2 x = T.root 3 while x ≠ T.nil 4 y = x 5 if z.key ≤ x.key 6 x = x.left 7 else x = x.right 8 z.p = y 9 if y == T.nil 10 T.root = z 11 elseif z.key < y.key 12 y.left = z 13 else y.right = z 14 z.left = T.nil 15 z.right = T.nil 16 z.color = RED 17 RB-Insert-Fixup(T, z) |
Tree-Insert (T, z)
1 y = NIL
2 x = T.root
3 while x ≠ NIL
4 y = x
5 if z.key ≤ x.key
6 x = x.left
7 else x = x.right
8 z.p = y
9 if y == NIL
10 T.root = z
11 elseif z.key < y.key
12 y.left = z
13 else y.right = z
|
RB-Insert-Fixup (T, z) 1 while z.p.color == RED 2 if z.p == z.p.p.left 3 y = z.p.p.right 4 if y.color == RED 5 z.p.color = BLACK // case 1 6 y.color = BLACK // case 1 7 z.p.p.color = RED // case 1 8 z = z.p.p // case 1 9 else if z == z.p.right 10 z = z.p // case 2 11 Left-Rotate(T, z) // case 2 12 z.p.color = BLACK // case 3 13 z.p.p.color = RED // case 3 14 Right-Rotate(T, z.p.p) // case 3 15 else (same as then clause with “right” and “left” exchanged) 16 T.root.color = BLACK
Following invariants are satisfied:
Transplant (T, dst, src)
1 if dst.p == NIL
2 T.root = src
3 elseif dst == dst.p.left
4 dst.p.left = src
5 else dst.p.right = src
6 if src ≠ NIL
7 src.p = dst.p
|
Tree-Delete (T, z) 1 if z.left == NIL 2 Transplant(T, z, z.right) 3 elseif z.right == NIL 4 Transplant(T, z, z.left) 5 else y = Tree-Minimum(z.right) 6 if y.p ≠ z 7 Transplant(T, y, y.right) 8 y.right = z.right 9 y.right.p = y 10 Transplant(T, z, y) 11 y.left = z.left 12 y.left.p = y |
RB-Transplant (T, dst, src)
1 if dst.p == T.nil
2 T.root = src
3 elseif dst == dst.p.left
4 dst.p.left = src
5 else dst.p.right = src
7 src.p = dst.p
|
Transplant (T, dst, src)
1 if dst.p == NIL
2 T.root = src
3 elseif dst == dst.p.left
4 dst.p.left = src
5 else dst.p.right = src
6 if src ≠ NIL
7 src.p = dst.p
|
RB-Delete (T, z) 1 y = z 2 y-original-color = y.color 3 if z.left == T.nil 4 x = z.right 5 RB-Transplant(T, z, z.right) 6 elseif z.right == T.nil 7 x = z.left 8 RB-Translplant(T, z, z.left) 9 else y = Tree-Minimum(z.right) 10 y-original-color = y.color 11 x = r.right 12 if y.p == z 13 x.p = y 14 else RB-Transplant(T, y, y.right) 15 y.right = z.right 16 y.right.p = y 17 RB-Transplant(T, z, y) 18 y.left = z.left 19 y.left.p = y 20 y.color = z.color 21 if y-original-color == BLACK 22 RB-Delete-Fixup(T, x) |
Tree-Delete (T, z) 1 if z.left == NIL 2 Transplant(T, z, z.right) 3 elseif z.right == NIL 4 Transplant(T, z, z.left) 5 else y = Tree-Minimum(z.right) 6 if y.p ≠ z 7 Transplant(T, y, y.right) 8 y.right = z.right 9 y.right.p = y 10 Transplant(T, z, y) 11 y.left = z.left 12 y.left.p = y |
RB-Delete-Fixup (T, x)
1 while x ≠ T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
4 if w.color == RED
5-8 // CASE 1
9 if w.left.color == BLACK and w.right.color == BLACK
10-11 // CASE 2
12 else if w.right.color == BLACK
13-16 // CASE 3
17-21 // CASE 4
22 else (same as then clause with “right” and “left” exchanged)
23 x.color = BLACK
x's sibling w is red
x's sibling w is black and both of w's children are black
x's sibling w is black, w's left child is red, and w's right child is black
x's sibling w is black, w's left right is red
Where might you use interval trees?