You are on page 1of 4

Red-Black Trees (RBT)

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.

Red-Black Tree Rotations


Algorithms to restore RBT property to tree after Tree-Insert and Tree-Delete. Assumptions: Right child y of x is not NIL.
Left-Rotate(T, x) 1. y right[x] 2. right[x] left[y] 3. parent[left[y]] x 4. parent[y] parent[x] 5. if parent[x] = NIL then root[T] y 6. else if x = left[parent[x]] then left[parent[x]] y 7. else right[parent[x]] y 8. left[y] x 9. parent[x] y

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)

RBT Insert - Fixup


RB-Insert-Fixup(T, z) 1. while z root and color[parent[z]] = RED do 2. if parent[z] is a left child then Goes from 3. y right[parent[parent[z]]] inserted node z 4. if color[y] = RED then up until property 5. color[parent[z]] BLACK that no red node 6. color[y] BLACK has red child is 7. color[parent[parent[z]]] RED restored. 8. z parent[parent[z]] 9. else 10. if z = right[parent[z]] then 11. z parent[z] 12. LEFT_ROTATE(t, z) 13. color[parent[z]] BLACK 14. color[parent[parent[z]] RED 15. RIGHT-ROTATE(T,parent[parent[z]]) 16. else (parent[z] is a right child, do lines 3-14 with right left) 17. color[root[T]] BLACK

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.

RBT Insert - Fixup


RB-Insert-Fixup(T, z) while color[parent[z]] = RED do /* Assume z's parent is left child */ y = right sibling of parent[z] Case 1: y is RED color y and [parent[z]] BLACK color[parent[parent[z]]] RED move z up to point to parent[parent[z]] Case 2: y is BLACK if z is right child move z to point to parent[z] LEFT_ROTATE(T z) /* now z is left child */ Case 3: if z is left child color parent[z] BLACK color parent[parent[z]] RED RIGHT-ROTATE(T, parent[parent[z]]) Finish by coloring root BLACK
7

RBT Insert 24 Fixup Example


20

Insert node with key = 24


18 17 19 22 21

Color of parent[24] = RED y


25 24

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

RBT Insert 18 Fixup Example


21

RBT Insert 18 Fixup Example


21 16 15 19 18 20

Insert node z with key = 18


16 15 19 18 20 22 25

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.

Loop ends because RB property restored. DONE


10

RBT Insert 18 Fixup Example


22

RBT Insert 18 Fixup Example


21 16 15 19 18 20 24

Insert node z with key = 18


16 15 19 18 20 22 24 25

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

RBT Insert 18 Fixup Example


20

z
15

16 19 18 22

21 24

y
25

Loop ends because RB property restored. DONE

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.

RBT Delete - Fixup


RB-Delete-Fixup(T, x) while x != root and x is BLACK do /*assume x is left child*/ w x's right sibling If case 1 encountered, recolor, Case 1: w is RED rotate, go to case 2 & quit or go to either case 3 and/or case 4 color w BLACK color parent[x] RED LEFT-ROTATE(T, parent[x]) w x's new right sibling Case 2: both w's children are BLACK color w RED If case 2 encountered, keep moving x up tree, recoloring. x parent[x] /* note: if we came to Case 2 after Case 1, we're DONE because after case 1, parent[x] = x is RED */
15 16

RBT Delete - Fixup


Now at least one of w's children is RED Case 3: if w's right child is BLACK color left child of w BLACK color w RED RIGHT-ROTATE(T, w) w x's new sibling Case 4: if w's right child is RED color w same as its parent color parent[x] BLACK color w's right child BLACK LEFT-ROTATE (T, parent[x]) x root color x BLACK If case 3 encountered, recolor , rotate, and go to case 4.

RBT Delete 22 Fixup Example


20

Delete node with key = 22


18 17 19 22 21

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]

If case 4 encountered, recolor, rotate, and quit.


17

20 18 19 25 21

Since y is RED, no black heights have changed. DONE

17

18

RBT Delete 22 Fixup Example


21 16 15 19 20 22

RBT Delete 24 Fixup Example


23 16 15 20 19 21 24

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

RBT Delete 24 Fixup Example


21

RBT Delete 24 Fixup Example


16

w is set to left sibling of x


21 25 21

w
15 14

16 20 19 21

25

Color of w is RED, so it is set to BLACK and color of parent[x] is set to RED

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

This is followed by RIGHT-ROTATE(T, parent[x])

14

w
19

20 21

16 15

x
25

14
21

w
19

20 21

22

You might also like