Presentation is loading. Please wait.

Presentation is loading. Please wait.

Red-Black Tree Insertion Start with binary search insertion, coloring the new node red. 1315 NIL l Insert 18 NIL l 14 12 9 NIL l 1315 NIL l 9 12 14 18.

Similar presentations


Presentation on theme: "Red-Black Tree Insertion Start with binary search insertion, coloring the new node red. 1315 NIL l Insert 18 NIL l 14 12 9 NIL l 1315 NIL l 9 12 14 18."— Presentation transcript:

1 Red-Black Tree Insertion Start with binary search insertion, coloring the new node red. 1315 NIL l Insert 18 NIL l 14 12 9 NIL l 1315 NIL l 9 12 14 18

2 Recoloring & Rebalancing NIL l 12 1315 NIL l Properties 1, 2, 3, 5? Property 4? violation! Not affected. How to fix? Right after insertion: 18 149

3 Recoloring – Case 1 1315 NIL l 18 NIL l x y Look at the color of uncle y – red. Color parent and uncle black and grandparent red. Done! (if violation exists, propagate coloring upwards) NIL l 14 1315 18 NIL l 9 12 9 14

4 Case 1 Uncle y is red. a A B ED C Recoloring y new x Propagate upwards (if c’s parent is red). x O(1) #black nodes on a path from node c (inclusive) to a leaf is unchanged. Any path entering subtree A, B, C, D, or E will pass through the same number of black nodes to reach a leaf. Property 5 maintained! no flip of color to red if c is root d c b c A BC DE b ad

5 Recoloring – Case 2 NIL l 5 x y Uncle y is black. NIL l Newly inserted Rot 1 Rot 2 Rotation 1 7 1418 NIL l 12 9 15 5 7 NIL l 1418 NIL l 12 9 15 NIL l

6 Rotation 2 Recoloring NIL l 7 5 14 NIL l 18 NIL l 9 15 12 NIL l 7 5 9 14 NIL l 18 NIL l 15 12

7 Case 2 Uncle y of x is black. d c b BC A DE x y recolor b and c Terminate here! No propagation. And x is a left (right) child while its parent is a right (left) child. Case 3 a y x d a b AB CDE c x a DE BCA d b c

8 Recoloring – Case 3 15 NIL l 15 51418 NIL l 5 x y Uncle y is black. Newly inserted NIL l recoloring 18 NIL l 14 12 9 NIL l 9 12

9 Case 3 Uncle y is black. a b AB CED y x recolor b and c Terminate here! Both x and its parent are left children or both are right children. d c c D a E BC d A x b

10 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. b a x root No action!

11 Running Time of RB-Insert The height of a red-black tree on n nodes is 2lg (n+1). RB-Insert runs in O(lg n) time. 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.

12 20 10 5 15 50 65 30 80 6040 3525 70 NIL l 55 NIL l Another Example of Insertion NIL l 45 7585

13 10 15 65 30 NIL l 55 NIL l Insert 37 Binary tree insertion. Color it red. Properties 1, 2, 3, 5 satisfied but property 4 violated. x y Case 1: uncle y is red. Propagate violation upward by recoloring. 85 80 70 20 5 45 25 50 4060 75 NIL l 37 35

14 20 10 5 15 50 65 30 6040 3525 70 NIL l 55 NIL l 37 NIL l x y Case 1 again since uncle y is red. Propagate violation upward by recoloring. NIL l 45 75 80 85

15 20 5 15 50 30 3525 NIL l 55 NIL l 37 NIL l Case 2: uncle y is black; x is a left child; its parent a right child. x y z First, right rotation. 85 NIL l 40 7010 60 80 65 NIL l 45 75

16 20 10 5 15 30 85 40 25 70 NIL l 55 NIL l 37 NIL l y x z Case 3: uncle y is black and x is a right child and so is its parent. Next, rotate around z. 60 6575 NIL l 50 NIL l 35 45 NIL l 80

17 50 4530 70 NIL l 37 NIL l y x z Recolor 10 15 5 NIL l 25 NIL l 35 55 60 6575 85 80 NIL l 40 20

18 50 4530 70 NIL l 37 NIL l y x z Done. 35 25 80 85 NIL l 15 40 5 60 655575 NIL l 10 20 NIL l


Download ppt "Red-Black Tree Insertion Start with binary search insertion, coloring the new node red. 1315 NIL l Insert 18 NIL l 14 12 9 NIL l 1315 NIL l 9 12 14 18."

Similar presentations


Ads by Google