1 / 27

Bottom-Up Red-Black Trees

Bottom-Up Red-Black Trees. Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees. Two keys per element. Search tree on one key, priority queue on other. Color flip doesn’t disturb priority queue property.

aman
Download Presentation

Bottom-Up Red-Black Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bottom-Up Red-Black Trees • Top-down red-black trees require O(log n) rotations per insert/delete. • Color flips cheaper than rotations. • Priority search trees. • Two keys per element. • Search tree on one key, priority queue on other. • Color flip doesn’t disturb priority queue property. • Rotation disturbs priority queue property. • O(log n) fix time per rotation => O(log2n) overall time.

  2. Bottom-Up Red-Black Trees • Bottom-up red-black tree properties. • At most 1 rotation per insert/delete. • O(1) amortized complexity to restructure following an insert/delete.

  3. Bottom-Up Insert • New pair is placed in a new node, which is inserted into the red-black tree. • New node color options. • Black node => one root-to-external-node path has an extra black node (black pointer). • Hard to remedy. • Red node => one root-to-external-node path may have two consecutive red nodes (pointers). • May be remedied by color flips and/or a rotation.

  4. gp pp d p c a b Classification Of 2 Red Nodes/Pointers LLb • XYz • X => relationship between gp and pp. • pp left child of gp => X = L. • Y => relationship between pp and p. • p right child of pp => Y = R. • z = b (black) if d = null or a black node. • z = r (red) if d is a red node.

  5. gp pp d gp p c pp d p a b c a b XYr • Color flip. • Move p, pp, and gp up two levels. • Continue rebalancing if necessary.

  6. gp y z pp x z y d p x a b c d c a b LLb • Rotate. • Done! • Same as LL rotation of AVL tree.

  7. gp y z pp x z x d a b c d a p y b c LRb • Rotate. • Done! • Same as LR rotation of AVL tree. • RRb and RLb are symmetric.

  8. Exercise: build the red-black tree • Build a red-black tree from the following key values in sequence: 45, 28, 36, 22, 68, 50, 10, 98, 26, 15, 25, 27, 78

  9. Delete • Delete as for unbalanced binary search tree. • If red node deleted, no rebalancing needed. • If black node deleted, a subtree becomes one black pointer (node) deficient.

  10. 10 7 40 45 3 8 30 60 35 1 20 5 25 Delete A Black Leaf • Delete 8.

  11. 10 7 40 45 3 30 60 35 1 20 5 25 Delete A Black Leaf py y • y is root of deficient subtree. • py is parent of y.

  12. 10 7 40 45 3 8 30 60 35 1 20 5 25 Delete A Black Degree 1 Node py y • Delete 45. • y is root of deficient subtree.

  13. 10 7 40 45 3 8 30 60 35 1 20 5 25 Delete A Black Degree 2 Node • Not possible, degree 2 nodes are never deleted.

  14. 10 7 40 45 3 8 30 py 60 35 1 20 5 25 y Rebalancing Strategy • If y is a red node, make it black.

  15. 10 py 7 40 45 3 8 30 y 60 35 1 20 5 25 Rebalancing Strategy • Now, no subtree is deficient. Done!

  16. y 10 7 40 45 3 8 30 60 35 1 20 5 25 Rebalancing Strategy • y is a black root (there is no py). • Entire tree is deficient. Done!

  17. py v y a b Rebalancing Strategy • y is black but not the root (there is a py). • Xcn • y is right child of py=> X = R. • Pointer to v is black => c = b. • v has 1 red child => n = 1.

  18. py py v y v y a b a b Rb0 (case 1) • Color change. • Now, py is root of deficient subtree. • Continue!

  19. py py v y v y a b a b Rb0 (case 2) • Color change. • Deficiency eliminated. • Done!

  20. v py py v a y b y a b Rb1 (case 1) • LL rotation. • Deficiency eliminated. • Done!

  21. w py py v v y c y a b w a b c Rb1 (case 2) • LR rotation. • Deficiency eliminated. • Done!

  22. w py py v v y c y a b w a b c Rb2 • LR rotation. • Deficiency eliminated. • Done!

  23. py v y w a b c Rr(n) • n = # of red children of vs right child w. Rr(2)

  24. v py py v a y b y a b Rr(0) • LL rotation. • Done!

  25. w py py v v y c y a b w a c b Rr(1) (case 1) • LR rotation. • Deficiency eliminated. • Done!

  26. x py py v v y d y a w w a x b b c c d Rr(1) (case 2) • Rotation. • Deficiency eliminated. • Done!

  27. x py py v v y d y a w w a x b b c c d Rr(2) • Rotation. • Deficiency eliminated. • Done!

More Related