Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time - - PowerPoint PPT Presentation
Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time - - PowerPoint PPT Presentation
Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time per Operation The Red-Black Tree Properties Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. nil nil nil nil nil
The Red-Black Tree Properties
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes. The black-height of a node x is the number of black nodes on a path from, but not including, x to a leaf. The black-height of T is the black-height of its root.
nil nil nil nil nil nil nil nil nil nil nil nil
Corollary: Operations Tree-Minimum, Tree-Maximum, Tree-Predecessor, Tree- Successor, and Find take O(log n) time on a red-black tree of size n. Corollary: A red-black tree with n nodes has height at most 2 log (n + 1).
The Height of a Red-Black Tree with n Nodes
nil nil nil nil nil nil nil nil nil nil nil nil
Lemma: A red-black tree of black-height h contains at least 2h – 1 nodes. Lemma: A red-black tree of black-height h has height at most 2h.
Rotations—The Key to Height Reduction
b d A B C a c e b d A B C a c e Right rotation Left rotation
A Rotation Takes Constant Time
b d A B C a c e b d A B C a c e
Right rotation
d b c a e
A Rotation Takes Constant Time
b d A B C a c e b d A B C a c e
Right rotation
d b c a e
A Rotation Takes Constant Time
b d A B C a c e b d A B C a c e
Right rotation
d b c a e
A Rotation Takes Constant Time
b d A B C a c e b d A B C a c e
Right rotation
d b c a e
A Rotation Takes Constant Time
b d A B C a c e b d A B C a c e
Right rotation
c a e d b
nil nil nil nil nil nil nil nil nil nil nil nil
Insertion into a Red-Black Tree
1 5 13 7 17 18 19 37 25 33 49 Element to insert: 21
nil nil nil nil nil nil nil nil nil nil nil nil nil
Insertion into a Red-Black Tree
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
4
1 5 13 7 17 18 19 37 25 33 49 Element to insert: 21 21
4 4
Insertion into a Red-Black Tree
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
4
Element to insert: 3
4 4
nil nil nil nil nil nil nil nil nil nil nil nil
1 5 13 7 17 18 19 37 25 33 49
nil nil nil nil nil nil nil nil nil nil nil nil nil
Insertion into a Red-Black Tree
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
4
Element to insert: 3
4 4
1 5 13 7 17 18 19 37 25 33 49 3
4 8
No red-red property Goal: Restore no-red-red condition while maintaining the other four. Tools: Recoloring of nodes; rotations
nil nil nil nil nil nil nil nil nil nil nil nil nil
Insertion into a Red-Black Tree
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
4
Element to insert: 3
4 4
1 5 13 7 17 18 19 37 25 33 49 3
4 8
No red-red property Goal: Restore no-red-red condition while maintaining the other four. Tools: Recoloring of nodes; rotations
Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy:
■ Fix the violation of no-red-red property at x ■ This may violate the condition at some ancestor ■ Continue fixing the property at the ancestor (the ancestor becomes x)
Observations:
■ Initially, x has a red parent; hence, x is not the root. ■ If parent[x] is red, it is not the root either;
that is, parent[parent[x]] exists.
■ If parent[x] is black, we are done.
Restoring the No-Red-Red Property
? x parent[x] parent[parent[x]]
Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy:
■ Fix the violation of no-red-red property at x ■ This may violate the condition at some ancestor ■ Continue fixing the property at the ancestor (the ancestor becomes x)
Observations:
■ Initially, x has a red parent; hence, x is not the root. ■ If parent[x] is red, it is not the root either;
that is, parent[parent[x]] exists.
■ If parent[x] is black, we are done.
Assumptions:
■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]
Restoring the No-Red-Red Property
? x parent[x] parent[parent[x]]
Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy:
■ Fix the violation of no-red-red property at x ■ This may violate the condition at some ancestor ■ Continue fixing the property at the ancestor (the ancestor becomes x)
Observations:
■ Initially, x has a red parent; hence, x is not the root. ■ If parent[x] is red, it is not the root either;
that is, parent[parent[x]] exists.
■ If parent[x] is black, we are done.
Assumptions:
■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]
Restoring the No-Red-Red Property
x parent[x] parent[parent[x]]
Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy:
■ Fix the violation of no-red-red property at x ■ This may violate the condition at some ancestor ■ Continue fixing the property at the ancestor (the ancestor becomes x)
Observations:
■ Initially, x has a red parent; hence, x is not the root. ■ If parent[x] is red, it is not the root either;
that is, parent[parent[x]] exists.
■ If parent[x] is black, we are done.
Assumptions:
■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]
Restoring the No-Red-Red Property
x parent[x] parent[parent[x]]
x parent[x] parent[parent[x]]
Case 1
Restoring the No-Red-Red Property
3 cases:
■ Both parent[x] and its
sibling are red
■ parent[x] is red, its sibling
is black, and x is the left child of its parent
■ As Case 2; but x is the
right child of its parent x
Recoloring
Restoring the No-Red-Red Property
3 cases:
■ Both parent[x] and its
sibling are red
■ parent[x] is red, its sibling
is black, and x is the left child of its parent
■ As Case 2; but x is the
right child of its parent x parent[x] parent[parent[x]] x
Recoloring
x parent[x] parent[parent[x]]
Recoloring Right-rotation Case 1 Case 2
Restoring the No-Red-Red Property
3 cases:
■ Both parent[x] and its
sibling are red
■ parent[x] is red, its sibling
is black, and x is the left child of its parent
■ As Case 2; but x is the
right child of its parent x parent[x] parent[parent[x]] x
Recoloring
x parent[x] parent[parent[x]]
Recoloring + Right-rotation Case 2 Case 1
Restoring the No-Red-Red Property
3 cases:
■ Both parent[x] and its
sibling are red
■ parent[x] is red, its sibling
is black, and x is the left child of its parent
■ As Case 2; but x is the
right child of its parent x parent[x] parent[parent[x]] x
Recoloring
x parent[x] parent[parent[x]]
Case 2 Recoloring + Right-rotation Left-rotation Case 1 Case 3
Lemma: An insertion into a red-black tree with n nodes takes O(log n) time and performs at most two rotations. Observations:
■ Every case takes constant time ■ Case 1 brings x two steps closer to the root and only recolors nodes (no rotations) ■ In Cases 2 and 3, we perform 1 or 2 rotations; then we are done
Insertion into a Red-Black Tree—Summary
Deletion from a Red-Black Tree
nil nil nil nil nil nil nil nil nil nil nil nil
1 5 13 7 17 18 19 37 25 33 49 Element to delete: 25
Deletion from a Red-Black Tree
1 5 13 7 17 18 19 37 33 49 Element to delete: 25
nil nil nil nil nil nil nil nil nil nil nil
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
4 4 4 4
Deletion from a Red-Black Tree
Element to delete: 18 Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
4 4 4 4
nil nil nil nil nil nil nil nil nil nil nil nil
1 5 13 7 17 18 19 37 25 33 49
Deletion from a Red-Black Tree
1 5 13 7 17 19 37 25 33 49 Element to delete: 18
nil nil nil nil nil nil nil nil nil nil nil
4 4 4 4
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
8
nil nil nil nil nil nil nil nil nil nil nil
Deletion from a Red-Black Tree
1 5 13 7 17 19 37 25 33 49 Element to delete: 18
4 4 4
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes. First step: Declare the child of the removed node “doubly black.”
4 8
nil nil nil nil nil nil nil nil nil nil nil
Deletion from a Red-Black Tree
1 5 13 7 17 19 37 25 33 49 Element to delete: 18
4 4 4
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes. First step: Declare the child of the removed node “doubly black.”
4 4
nil nil nil nil nil nil nil nil nil nil nil
Deletion from a Red-Black Tree
1 5 13 7 17 19 37 25 33 49 Element to delete: 18
4 4 4 4
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
8
First step: Declare the child of the removed node “doubly black.” Color property
nil nil nil nil nil nil nil nil nil nil nil
Deletion from a Red-Black Tree
1 5 13 7 17 19 37 25 33 49 Element to delete: 18
4 4 4 4
Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Every root-to-leaf path contains the same number of black nodes.
8
First step: Declare the child of the removed node “doubly black.” Color property
Restoring the Color Property
The easy cases: Node x is red, root or not Node x is the root and black
x parent[x] w c c'
Case 2
x parent[x] w c
Case 1
Restoring the Color Property
Assume that x is the left child of its parent and that its right sibling w is black. 3 cases, depending on colors
- f children of w:
■ Both are black ■ Right child is red ■ Left child is red, and right
child is black
Recoloring
x c c c'
Recoloring
c c'
Left-rotation
Case 3
x parent[x] w c
Recoloring
Restoring the Color Property
Assume that x is the left child of its parent and that its right sibling w is black. 3 cases, depending on colors
- f children of w: