Red Red: - Black Trees (RBT) - Black Properties
Red Red: - Black Trees (RBT) - Black Properties
Red Red: - Black Trees (RBT) - Black Properties
A BST can implement any of the basic dynamic-set operations in O(h) time. These operations are O(lgn) if tree is balanced. Red-Black tree: BST in which each node is colored red or black. Constraints on the coloring of nodes ensure that no root to leaf path is more than twice as long as any other, so tree is approximately balanced. Each RBT node contains fields left, right, parent, color, and key. 1) 2) 3) 4) 5)
Red-Black Properties
Red-Black tree properties: Every node is either red or black. The root is black. Every leaf contains NIL and is black. If a node is red, then both its children are black. For each node x in a RBT, all paths in sub-trees rooted at x contain the same number of black nodes.
2 20 2 18 1 17
1
1 22 1 21 1 25
1 19
Every node has a black-height, bh(x) (for all NIL leaves, bh(x) = 0). For root x, bh(x) = bh(T).
2
Red-Black Properties
Lemma: A RBT with n internal nodes has height at most 2lg(n+1). Proof: In class. This means that the dynamic-set operations for BSTs Search, Minimum, Maximum, Successor, and Predecessor can be implemented as-is on RBTs, and since they take O(h) time on a BST, they take O(lgn) time on an RBT. We will see that the operations Tree-Insert and Tree-Delete can also be done in O(lgn) time on RBTs. However, modifications must be made to ensure that the RBT property is maintained.
x LR(T,x) RR(T,y) y x y
RBT Insert
RB-Insert(T, z) 1. y NIL 2. x root[T] 3. while x NIL do 4. y x 5. if key[z] < key[x] 6. x left[x] 7. else x right[x] 8. parent[z] y 9. if y = NIL then 10. root[T] z 11. else if key[z] < key [y] then 12. left[y] z 13. else right[y] z 14. RB-Insert-Fixup(T,z)
Input is a RBT T and a node z such that left[z] = right[z] = NIL and color[z] = RED Inserting z in T: will not violate property 5 by changing the black-height of any node in the tree. may violate properties 2 and/or 4.
Parent of z is a right child so y is set to left child of parent[parent[z]] Color of y is RED so y and ys sibling are set to BLACK. Color of parent of parent of z is set to RED and z is set to point to that node. Loop ends because RB property restored. DONE
8
20 18 17 19 22 21
z y
25 24
Color of parent[z] = RED Parent of z is a left child so y is set to right child of parent[parent[z]], NIL
RIGHT-ROTATE(T, parent[parent[z]]
22 25
z
21 16 22 20 19 25
y = NIL (black)
16 15 18 19
21 22 25 20
15
18
y = NIL (black)
Color of y is BLACK and z is a left child (go directly to case 3) . Color z's parent BLACK and z's grandparent RED.
Color of parent[z] = RED y is set to right child of parent[parent[z]]. Color of y is RED so y and ys sibling are set to BLACK. Color of parent of parent of z is set to RED.
15
y
25
z moves up to parent[parent[z]] (node 20) and y moves to the right child of the parent[parent[z]] (node 24) z is right child, so z moves up to parent[z] (node 16) and {Case 2} LEFT-ROTATE(T, z) occurs. In {Case 3}, parent[z] = BLACK and parent[parent[z]] = RED RIGHT-ROTATE(T, parent[parent[z]])
z
22 21
z
21 16 24 20 19 22 25
20
24 22
y
25
15
16 19 18
y
11
18
12
z
15
16 19 18 22
21 24
y
25
13
RB-Delete(T, z) 1. if either child of z is NIL then 2. yz 3. else y TREE-SUCCESSOR(z) 4. if left[y] NIL then 5. x left[y] 6. else x right[y] 7. parent[x] parent[y] 8. if parent[x] = NIL then root[T] x 9. else 10. if y is a left child then 11. left child of parent[y] x 12. else right child of parent[y] x 13. if y z then 14. key[z] key[y] (copy ys data to z) 15. if color[y] = BLACK then 16. RB-DELETE-FIXUP (T,x) 17. return y
RBT Delete
Input is an RBT and node to delete. Minor modification of TREE-DELETE Complications arise when deleted node y is spliced out and color of y was BLACK, in line 15, since property 5 must be restored. Node x is the former child of y. If color[x] = RED, change it to BLACK.
14
RBT Delete
Note: If the node spliced out (y) is red, no "fixup" is needed. Assume color[y] = BLACK Think of node x as carrying an "extra black". IDEA: Push extra black up tree until - reaching a RED node that can be colored BLACK or - reaching the root, in which case the extra black can be discarded (because the root must be black) - then use rotations and recolorings to fix property 4 violations.
z
25
y is set to successor of z y x = NIL x is set to right child of y (NIL) Since y != z, key[z] = key[y]
20 18 19 25 21
17
18
z=y
25
Delete node with key = 22 y is set to z x RB-Delete-Fixup is called because color[y] =BLACK While loop is never executed because color[x] = RED. color[x] is set to BLACK at end and RB property is restored.
14
z=y
25
Delete node with key = 24 y is set to z x RB-Delete-Fixup is called because color[y] =BLACK Since x is BLACK and not equal to the root, w is set to left sibling of x.
21 16 15 19 20 25
21
w
15 14
16 20 19 21
25
19
20
w
15 14
16 20 19 21
25
15 14
w
19 16
20
Since the color of both w's children is BLACK, w is set to RED and x is set to parent[x] Now x is RED, stopping the while loop. Last line of RB-Delete-Fixup sets color x to BLACK. DONE
21
21
15 25
21
x
25
w
15 14
16 20 19 21
14
w
19
20 21
16 15
x
25
14
21
w
19
20 21
22