CMPS 2200 – Fall 2017 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk 9/13/17 1 CMPS 2200 Intro. to Algorithms
Dynamic Set A dynamic set , or dictionary , is a data structure which supports operations • Insert 7 • Delete 3 18 18 • Find 10 10 22 22 8 15 15 12 12 17 17 Using balanced binary search trees we can implement a dictionary data structure such that each operation takes O (log n ) time. 9/13/17 2 CMPS 2200 Intro. to Algorithms
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 • x < y , for all y in the subtree right of x 7 x x 3 18 18 10 10 22 22 8 15 15 12 12 17 17 9/13/17 3 CMPS 2200 Intro. to Algorithms
Search Trees 7 3 18 18 Different variants of search trees: 10 10 22 22 1 12 12 8 15 15 • Balanced search trees (guarantee height of O(log n ) for n elements) 12 12 17 17 10 25 • k -ary search trees (such as B-trees, 6 12 15 21 30 45 2-3-4-trees) 2 11 14 7 8 20 23 24 27 40 50 17 • Search trees that store keys only 8 42 in leaves, and store copies of 1 14 35 43 keys as split-values in 17 43 6 12 26 41 59 1 internal nodes 6 8 12 14 26 35 41 42 59 61 9/13/17 4 CMPS 2200 Intro. to Algorithms
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. • AVL trees • 2-3 trees • 2-3-4 trees Examples: • B-trees • Red-black trees 9/13/17 5 CMPS 2200 Intro. to Algorithms
Red-black trees This data structure requires an extra one- bit 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 6 CMPS 2200 Intro. to Algorithms
Example of a red-black tree 7 3 18 null null h = 4 10 22 null 8 11 26 null null null null null null 1. Every node is either red or black. 9/13/17 7 CMPS 2200 Intro. to Algorithms
Example of a red-black tree 7 3 18 null null h = 4 10 22 null 8 11 26 null null null null null null 2., 3. The root and leaves ( null ’s) are black. 9/13/17 8 CMPS 2200 Intro. to Algorithms
Example of a red-black tree 7 3 18 null null h = 4 10 22 null 8 11 26 null null null null null null 4. If a node is red, then both its children are black. 9/13/17 9 CMPS 2200 Intro. to Algorithms
Example of a red-black tree 7 bh = 2 3 18 bh = 2 null null h = 4 10 22 bh = 1 null 8 11 26 bh = 1 null null null null null null bh = 0 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 10 CMPS 2200 Intro. to Algorithms
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log( n + 1). Proof. I NTUITION : T • Merge red nodes into their black parents. 9/13/17 11 CMPS 2200 Intro. to Algorithms
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log( n + 1). Proof. I NTUITION : T • Merge red nodes into their black parents. 9/13/17 12 CMPS 2200 Intro. to Algorithms
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log( n + 1). Proof. I NTUITION : T • Merge red nodes into their black parents. 9/13/17 13 CMPS 2200 Intro. to Algorithms
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log( n + 1). Proof. I NTUITION : T • Merge red nodes into their black parents. 9/13/17 14 CMPS 2200 Intro. to Algorithms
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log( n + 1). Proof. I NTUITION : T’ • Merge red nodes into their black parents. 9/13/17 15 CMPS 2200 Intro. to Algorithms
Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log( n + 1). Proof. I NTUITION : 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 16 CMPS 2200 Intro. to Algorithms
Proof (continued) • We have h h /2, since at most half T the vertices on any path are red. h • # 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 T’ binary trees; T’ can only have h more) n + 1 2 h' log( n + 1) h' h /2 h 2 log( n + 1). 9/13/17 17 CMPS 2200 Intro. to Algorithms
Query operations Corollary. The queries S EARCH , M IN , M AX , S UCCESSOR , and P REDECESSOR all run in O (log n ) time on a red-black tree with n nodes. 7 3 18 18 10 10 22 22 8 11 11 26 26 9/13/17 18 CMPS 2200 Intro. to Algorithms
Modifying operations The operations I NSERT and D ELETE 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 19 CMPS 2200 Intro. to Algorithms
Rotations R IGHT -R OTATE (B) B A L EFT -R OTATE (A) 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 20 CMPS 2200 Intro. to Algorithms
R IGHT- R OTATE(B) B A Rotation Example L EFT- R OTATE(A) A B R IGHT- R OTATE(20) 31 31 31 31 9 41 41 9 41 41 39 39 42 42 39 39 42 42 6 20 20 6 16 16 2 8 16 16 22 22 2 8 15 15 20 20 In-order(v){ 11 11 17 17 22 22 15 15 17 17 if v==null return; In-order(v.left); 11 11 print(v.key+” ”); In-order(v.right); } In-order traversal: In-order traversal: 2 6 8 9 11 15 16 17 20 22 31 39 41 42 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 21 CMPS 2200 Intro. to Algorithms
Red-black trees This data structure requires an extra one- bit 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 22 CMPS 2200 Intro. to Algorithms
Insertion into a red-black tree I DEA : Insert x in tree. Color x red. Only red- black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 18 18 • Insert x =15. 10 10 22 22 8 11 11 26 26 15 15 9/13/17 23 CMPS 2200 Intro. to Algorithms
Insertion into a red-black tree I DEA : Insert x in tree. Color x red. Only red- black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 18 18 • Insert x =15. • Recolor, moving the 10 10 22 22 violation up the tree. 8 11 11 26 26 15 15 9/13/17 24 CMPS 2200 Intro. to Algorithms
Insertion into a red-black tree I DEA : Insert x in tree. Color x red. Only red- black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 18 18 • Insert x =15. • Recolor, moving the 10 10 22 22 violation up the tree. 8 11 11 26 26 15 15 9/13/17 25 CMPS 2200 Intro. to Algorithms
Insertion into a red-black tree I DEA : Insert x in tree. Color x red. Only red- black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 18 18 • Insert x =15. • Recolor, moving the 10 10 22 22 violation up the tree. 8 11 11 26 26 • R IGHT -R OTATE (18). 15 15 9/13/17 26 CMPS 2200 Intro. to Algorithms
Insertion into a red-black tree I DEA : Insert x in tree. Color x red. Only red- black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 10 10 • Insert x =15. • Recolor, moving the 8 18 18 violation up the tree. 11 11 22 22 • R IGHT -R OTATE (18). 15 15 26 26 9/13/17 27 CMPS 2200 Intro. to Algorithms
Insertion into a red-black tree I DEA : Insert x in tree. Color x red. Only red- black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. 7 Example: 3 10 10 • Insert x =15. • Recolor, moving the 8 18 18 violation up the tree. 11 11 22 22 • R IGHT -R OTATE (18). 15 15 26 26 • L EFT -R OTATE (7) 9/13/17 28 CMPS 2200 Intro. to Algorithms
Recommend
More recommend