CMPS 2200 Fall 2017 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk 9/13/17 CMPS 2200 Intro. to Algorithms 1
Dynamic Set A dynamic set, or dictionary, is a data structure which supports operations Insert Delete Find Using balanced binary search trees we can implement a dictionary data structure such that each operation takes O(log n) time. 3 7 10 8 15 18 12 17 22 9/13/17 CMPS 2200 Intro. to Algorithms 2
Search Trees A binary search tree is a binary tree. Each node stores a key. The tree fulfills the binary search tree property: For every node x holds: y x, for all y in the subtree left of x x < y, for all y in the subtree right of x x 3 7 18 x x 10 22 8 15 12 17 9/13/17 CMPS 2200 Intro. to Algorithms 3
Search Trees 7 3 18 Different variants of search trees: 1 12 10 22 Balanced search trees (guarantee height of O(log n) for n elements) 8 15 12 17 k-ary search trees (such as B-trees, 2-3-4-trees) 6 2 7 8 10 25 12 15 21 30 45 11 14 20 23 24 27 40 50 Search trees that store keys only in leaves, and store copies of 1 keys as split-values in internal nodes 6 12 17 26 41 43 59 6 8 12 14 26 35 41 42 9/13/17 CMPS 2200 Intro. to Algorithms 4 8 17 1 14 35 43 42 59 61
Balanced search trees Balanced search tree: A search-tree data structure for which a height of O(log n) is guaranteed when implementing a dynamic set of n items. Examples: AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees 9/13/17 CMPS 2200 Intro. to Algorithms 5
Red-black trees This data structure requires an extra onebit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root is black. 3. The leaves (null s) are black. 4. If a node is red, then both its children are black. 5. All simple paths from any node x, excluding x, to a descendant leaf have the same number of black nodes = black-height(x). 9/13/17 CMPS 2200 Intro. to Algorithms 6
Example of a red-black tree 7 3 18 null null 10 22 h = 4 8 11 null 26 null null null null null null 1. Every node is either red or black. 9/13/17 CMPS 2200 Intro. to Algorithms 7
Example of a red-black tree 7 3 18 null null 10 22 h = 4 8 11 null 26 null null null null null null 2., 3. The root and leaves (null s) are black. 9/13/17 CMPS 2200 Intro. to Algorithms 8
Example of a red-black tree 7 3 18 null null 10 22 h = 4 8 11 null 26 null null null null null null 4. If a node is red, then both its children are black. 9/13/17 CMPS 2200 Intro. to Algorithms 9
Example of a red-black tree 7 bh = 2 3 18 bh = 2 null null bh = 1 10 22 h = 4 bh = 1 8 11 null 26 bh = 0 null null null null null null 5. All simple paths from any node x, excluding x, to a descendant leaf have the same number of black nodes = black-height(x). 9/13/17 CMPS 2200 Intro. to Algorithms 10
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 11
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 12
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 13
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 14
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 15
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes h into their black parents. This process produces a tree in which each node has 2, 3, or 4 children. The 2-3-4 tree has uniform depth h of leaves. 9/13/17 CMPS 2200 Intro. to Algorithms 16
Proof (continued) We have h h/2, since at most half the vertices on any path are red. # leaves in T = # leaves in T # leaves in T = n+1 (fact about binary trees with exactly 2 children per internal node) # leaves in T 2 h (fact about binary trees; T can only have more) n + 1 2 h' log(n + 1) h' h/2 h 2 log(n + 1). 9/13/17 CMPS 2200 Intro. to Algorithms 17 T T h h
Query operations Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR all run in O(log n) time on a red-black tree with n nodes. 3 7 10 18 22 8 11 26 9/13/17 CMPS 2200 Intro. to Algorithms 18
Modifying operations The operations INSERT and DELETE cause modifications to the red-black tree: 1. the operation itself, 2. color changes, 3. restructuring the links of the tree via rotations. 9/13/17 CMPS 2200 Intro. to Algorithms 19
Rotations B RIGHT-ROTATE(B) A A LEFT-ROTATE(A) B Rotations maintain the inorder ordering of keys: a, b, c a A b B c. Rotations maintain the binary search tree property Can be applied to any BST, not just red-black trees A rotation can be performed in O(1) time. 9/13/17 CMPS 2200 Intro. to Algorithms 20
B RIGHT-ROTATE(B) A Rotation Example A LEFT-ROTATE(A) B 31 RIGHT-ROTATE(20) 31 9 41 9 41 6 20 39 42 6 16 39 42 2 8 16 22 2 8 15 20 11 15 17 In-order traversal: 2 6 8 9 11 15 16 17 20 22 31 39 41 42 In-order(v){ if v==null return; In-order(v.left); print(v.key+ ); In-order(v.right); } 11 17 22 In-order traversal: 2 6 8 9 11 15 16 17 20 22 31 39 41 42 Maintains sorted order of keys, and can reduce height 9/13/17 CMPS 2200 Intro. to Algorithms 21
Red-black trees This data structure requires an extra onebit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root is black. 3. The leaves (null s) are black. 4. If a node is red, then both its children are black. 5. All simple paths from any node x, excluding x, to a descendant leaf have the same number of black nodes = black-height(x). 9/13/17 CMPS 2200 Intro. to Algorithms 22
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. 3 7 10 18 22 8 11 26 15 9/13/17 CMPS 2200 Intro. to Algorithms 23
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the 10 violation up the tree. 8 11 7 18 15 22 26 9/13/17 CMPS 2200 Intro. to Algorithms 24
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the 10 violation up the tree. 8 11 7 18 15 22 26 9/13/17 CMPS 2200 Intro. to Algorithms 25
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). 7 10 8 11 18 15 22 26 9/13/17 CMPS 2200 Intro. to Algorithms 26
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). 7 8 10 11 15 18 22 26 9/13/17 CMPS 2200 Intro. to Algorithms 27
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) 9/13/17 CMPS 2200 Intro. to Algorithms 28 7 8 10 11 15 18 22 26
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) 8 11 9/13/17 CMPS 2200 Intro. to Algorithms 29 3 7 10 15 18 22 26
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. 7 Recolor, moving the 3 violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) and recolor. 8 11 9/13/17 CMPS 2200 Intro. to Algorithms 30 10 15 18 22 26
Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. 7 Recolor, moving the 3 violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) and recolor. 8 11 9/13/17 CMPS 2200 Intro. to Algorithms 31 10 15 18 22 26
Pseudocode RB-INSERT(T, x) TREE-INSERT(T, x) color[x] RED only RB property 4 can be violated while x root[t] and color[p[x]] = RED do if p[x] = left[p[p[x]] then y right[p[p[x]] y = aunt/uncle of x if color[y] = RED then Case 1 else if x = right[p[x]] then Case 2 Case 2 falls into Case 3 Case 3 else then clause with left and right swapped color[root[t]] BLACK 9/13/17 CMPS 2200 Intro. to Algorithms 32
Graphical notation Let All denote a subtree with a black root. s have the same black-height. 9/13/17 CMPS 2200 Intro. to Algorithms 33
Case 1 A x B C D y Recolor A B C Continue new x D (Or, A s children are swapped.) p[x] = left[p[p[x]] y right[p[p[x]] color[y] = RED Push C s black onto A and D, and recurse, since C s parent may be red. 9/13/17 CMPS 2200 Intro. to Algorithms 34
Case 2 A x B C y LEFT-ROTATE(A) x A B C y p[x] = left[p[p[x]] y right[p[p[x]] color[y] = BLACK x = right[p[x]] Transform to Case 3. 9/13/17 CMPS 2200 Intro. to Algorithms 35
Case 3 x A B C RIGHT-ROTATE(C) y (and recolor) A B C p[x] = left[p[p[x]] y right[p[p[x]] color[y] = BLACK x = left[p[x]] Done! No more violations of RB property 4 are possible. 9/13/17 CMPS 2200 Intro. to Algorithms 36
Analysis Go up the tree performing Case 1, which only recolors nodes. If Case 2 or Case 3 occurs, perform 1 or 2 rotations, and terminate. Running time: O(log n) with O(1) rotations. RB-DELETE same asymptotic running time and number of rotations as RB-INSERT. 9/13/17 CMPS 2200 Intro. to Algorithms 37
Pseudocode (part II) else then clause with left and right swapped p[x] = right[p[p[x]] then y left[p[p[x]] y = aunt/uncle of x if color[y] = RED then Case 1 else if x = left[p[x]] then Case 2 Case 2 falls into Case 3 Case 3 color[root[t]] BLACK 9/13/17 CMPS 2200 Intro. to Algorithms 38
Case 1 D y C A x B Recolor D y C Continue new x A x B (Or, A s children are swapped.) p[x] = right[p[p[x]] y left[p[p[x]] color[y] = RED Push C s black onto A and D, and recurse, since C s parent may be red. 9/13/17 CMPS 2200 Intro. to Algorithms 39
Case 2 y C x B A RIGHT-ROTATE(A) y C B x A p[x] = right[p[p[x]] y left[p[p[x]] color[y] = BLACK x = left[p[x]] Transform to Case 3. 9/13/17 CMPS 2200 Intro. to Algorithms 40
Case 3 C y B x LEFT-ROTATE(C) (and recolor) C B A A p[x] = right[p[p[x]] y left[p[p[x]] color[y] = BLACK x = right[p[x]] Done! No more violations of RB property 4 are possible. 9/13/17 CMPS 2200 Intro. to Algorithms 41