Red-Black Tree Insertion Start with binary search insertion, coloring the new node red.14 NILl Insert 18NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl Recoloring & Rebalancing Right after insertion:9 NILl NILl NILl violation! NILl NILl NILl Properties.
Download ReportTranscript Red-Black Tree Insertion Start with binary search insertion, coloring the new node red.14 NILl Insert 18NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl Recoloring & Rebalancing Right after insertion:9 NILl NILl NILl violation! NILl NILl NILl Properties.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red. 12 12 14 9 NILl Insert 18 9 NILl NILl 13 NILl 14 NILl 15 NILl NILl 13 NILl NILl 15 NILl NILl 18 NILl NILl Recoloring & Rebalancing Right after insertion: 12 9 NILl 14 NILl 13 NILl 15 violation! NILl NILl 18 NILl Properties 1, 2, 3, 5? Property 4? NILl Not affected. How to fix? Recoloring – Case 1 Look at the color of uncle y – red. 12 12 14 9 NILl 9 y NILl 13 NILl NILl 14 NILl 15 NILl NILl NILl 13 18 x NILl 15 NILl NILl 18 NILl Color parent and uncle black and grandparent red. Done! (if violation exists, propagate coloring upwards) NILl NILl Case 1 Propagate upwards (if c’s parent is red). Uncle y is red. c new x y c Recoloring no flip of color to red if c is root d a x b D A B a O(1) C E d b A Any path entering subtree A, B, C, D, or E will pass through the same number B of black nodes to reach a leaf. D C #black nodes on a path from node c (inclusive) to a leaf is unchanged. E Property 5 maintained! Recoloring – Case 2 Uncle y is black. 12 12 Rot 2 9 Rot 1 Rotation 1 15 15 9 y NILl NILl 5 14 14 18 18 7 x NILl NILl NIL NILl l 7 NILl NILl NILl NILlNewly NILl NILl NILl NILl NILl NILl inserted 5 12 7 15 Rotation 2 Recoloring 5 NILl 9 NILlNILl NILl NILl 14 12 18 NILl NILl 7 NILl 5 NILl 15 9 NILlNILl NILl NILl 14 18 NILl NILl NILl Case 2 Uncle y of x is black. And x is a left (right) child while its parent is a right (left) child. c c y y d a x d b b D C A B c a x x E b recolor b and c E D a C B Case 3 d A E C D B A Terminate here! No propagation. Recoloring – Case 3 Uncle y is black. 12 12 NILl 5 NILl recoloring 14 9 15 y NILl 15 NILl 9 NILl Newly inserted 5 18 NILl x NILl NILl NILl NILl 14 18 NILl NILl NILl NILl Case 3 Uncle y is black. Both x and its parent are left children or both are right children. b c recolor b and c y b C B a d x a A x D E A c B d C D E Terminate here! Termination from Case 1 Case 1 changes into case 2 or 3, terminating after ≤ 2 rotations. Or case 1 all the way (repeatedly upward propagations). Terminates when either x = root or parent is black. root b x a No action! Running Time of RB-Insert The height of a red-black tree on n nodes is 2lg (n+1). Binary search tree insertion takes O(lg n). Case 1 may repeat O(lg n) times (the pointer x moves up the tree). Case 2 or 3 terminates RB-Insert after at most two tree rotations. RB-Insert runs in O(lg n) time. Another Example of Insertion 20 10 70 5 NILl NILl NILl NILl 40 NILl NILl 75 60 NILl 30 25 80 50 15 45 35 NILl NILl NILl 55 NILl NILl NILl 65 NILl NILl NILl 85 NILl NILl Insert 37 Case 1: uncle y is red. 20 Propagate violation upward by recoloring. 10 70 5 NILl NILl NILl Properties 1, 2, 3, 5 satisfied but property 4 violated. y NILl NILl 40 NILl 75 60 NILl 30 25 80 50 15 45 35 NILl NILl NILl 55 65 NILl NILl NILl 37 x NILl NILl NILl NILl Binary tree insertion. Color it red. 85 NILl NILl Case 1 again since uncle y is red. 20 10 70 5 NILl NILl NILl NILl 40 NILl NILl 75 y 60 NILl x 30 25 80 50 15 45 35 NILl NILl NILl 55 NILl NILl NILl 37 NILl NILl 85 NILl NILl 65 NILl NILl Propagate violation upward by recoloring. Case 2: uncle y is black; x is a left child; its parent a right child. 70 x 5 NILl NILl 40 NILl NILl 75 60 NILl 30 25 80 50 15 NILl z y 10 NILl 20 45 35 NILl NILl NILl 55 NILl NILl NILl 37 NILl 85 NILl 65 NILl NILl First, right rotation. NILl NILl Case 3: uncle y is black and x is a right child and so is its parent. z 20 y 10 5 NILl NILl 50 40 15 NILl NILl 25 NILl x 30 45 NILl 35 NILl NILl NILl 70 37 NILl NILl 60 55 65 80 75 85 NILl NILl NILl NILl NILl NILl NILl Next, rotate around z. NILl y 50 z 70 20 10 5 NILl NILl NILl x 40 15 30 NILl 25 60 45 35 55 80 65 75 85 NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl 37 NILl Recolor y 50 z 70 20 10 5 NILl NILl NILl x 40 15 30 NILl 25 60 45 35 55 80 65 75 85 NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl NILl 37 NILl Done.