1 / 48

AVL and Red-Black Trees: Balancing and Insertion/Deletion

Learn about balancing techniques for AVL and Red-Black Trees, as well as insertion and deletion operations.

devinn
Download Presentation

AVL and Red-Black Trees: Balancing and Insertion/Deletion

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. CS3424 AVL Trees Red-Black Trees

  2. Trees • As stated before, trees are great ways of holding hierarchical data • Insert, Search, Delete ~ O(lgN) • But only if they’re balanced! • So let’s discuss how to assure balance

  3. Rotations x y Left-Rotate(x)   y x Right-Rotate(y)    

  4. AVL Trees (1962) • Uses height property to maintain balance • Height of left & right differ by at most 1 • Uses rotations to restore balance after insertions and deletions

  5. Balancing in AVL Trees • Balance is the difference between the heights of the left and right subtrees • This should range between -1 and 1 • Maintain this balance upon insert and delete • Balance = HR - HL

  6. Left-Left Imbalance 0 Right-Rotate(X) -2 X Y -1 0 Y X      

  7. Left-Right Imbalance -2 -2 X X = +1 +1 Y Y -1      

  8. Left-Right Imbalance (cont) -2 -2 X X = +1 +1 Y Y +1      

  9. Left-Right Imbalance ( left) -2 X +1 Y -1   

  10. Left-Right Imbalance ( left) Left-Rotate(Y) -2 -2 X X +1 -2 Y  -1 0   Y   

  11. Left-Right Imbalance ( left) Right-Rotate(X) -2 0 X  -2 0 +1  X Y 0  Y   

  12. Left-Right Imbalance ( right) -2 X +1 Y +1   

  13. Left-Right Imbalance ( right) Left-Rotate(Y) -2 -2 X X +1 -1 Y  +1 -1   Y   

  14. Left-Right Imbalance ( right) Right-Rotate(X) -2 0  X 0 -1 -1 X  Y -1 Y    

  15. Red-Black Trees (1972/1978) • BST + notion of color (RED / BLACK) • Every node is either red or black • Root is black • Every leaf (NIL) is black • If node red, both children are black • For each node, path to NIL children contains same number of black nodes (black-height)

  16. Possibilities in Growing a Red-Black Tree

  17. Painting a Tree to beRed-Black

  18. Painting a Tree to beRed-Black Root is black

  19. Painting a Tree to beRed-Black Must be black bh=1 bh=1

  20. Painting a Tree to beRed-Black Invalid!

  21. Painting a Tree to beRed-Black Another try!

  22. Painting a Tree to beRed-Black Must be black

  23. Painting a Tree to beRed-Black Must be red

  24. Painting a Tree to beRed-Black bh = 2 or 3 You can finish this with ease!

  25. Inserting & Deleting in Red-Black Trees • Insert similar to binary search trees • “Search” left & right until finding NIL • But now, we need to worry about coloring • Requires possible rotations & “cleanup” • Deleting similar to BSTs as well • Search left & right until finding item • If we removed a black node, “cleanup”

  26. Red-Black Insert • Insert node (z) • z.Color = red • This can cause two possible problems: • Root must be black (initial insert violates this) • Two red nodes cannot be adjacent (this is violated if parent of z is red) • Cleanup

  27. Red-Black Cleanup • y = z’s “uncle” • Three cases: • y is red • y is black and z is a right child • y is black and z is a left child Not mutually exclusive

  28. Case 1 – z’s Uncle is red • y.Color = black • z.Parent.Color = black • z.Parent.Parent.Color = red • z = z.Parent.Parent • Repeat cleanup

  29. Case 1 Visualized 11 2 14 1 7 15 5 8 y y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent repeat cleanup 4 z New Node

  30. Case 1 Visualized 11 2 14 1 7 15 5 8 y y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent repeat cleanup 4 z New Node

  31. Case 1 Visualized 11 2 14 1 7 15 5 8 y y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent repeat cleanup 4 z New Node

  32. Case 1 Visualized 11 2 14 1 7 15 5 8 y y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent repeat cleanup 4 z New Node

  33. Case 1 Visualized 11 2 14 y 1 7 15 z 5 8 y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent repeat cleanup 4 New Node

  34. Case 2 – z’s Uncle is black & z is a right child • z = z.Parent • Left-Rotate(T, z) • Do Case 3 • Note that Case 2 is a subset of Case 3

  35. Case 2 Visualized 11 2 14 y 1 7 15 z 5 8 z = z.Parent Left-Rotate(T,z) Do Case 3 4

  36. Case 2 Visualized 11 2 14 y z 1 7 15 5 8 z = z.Parent Left-Rotate(T,z) Do Case 3 4

  37. Case 2 Visualized 11 7 14 y 2 8 15 z 1 5 z = z.Parent Left-Rotate(T,z) Do Case 3 4

  38. Case 3 – z’s Uncle is black & z is a left child • z.Parent.Color = black • z.Parent.Parent.Color = red • Right-Rotate(T, z.Parent.Parent)

  39. Case 3 Visualized 11 7 14 y 2 8 15 z 1 5 z.Parent.Color = black z.Parent.Parent.Color = red Right-Rotate(T, z.Parent.Parent) 4

  40. Case 3 Visualized 11 7 14 y 2 8 15 z 1 5 z.Parent.Color = black z.Parent.Parent.Color = red Right-Rotate(T, z.Parent.Parent) 4

  41. Case 3 Visualized 11 7 14 y 2 8 15 z 1 5 z.Parent.Color = black z.Parent.Parent.Color = red Right-Rotate(T, z.Parent.Parent) 4

  42. Case 3 Visualized 7 2 11 z 8 14 1 5 y 15 4 z.Parent.Color = black z.Parent.Parent.Color = red Right-Rotate(T, z.Parent.Parent)

  43. Analysis of Red-Black Cleanup C New z C A D A D y   B   B   z     Case 1 , , , , and  are all black-rooted and all have same black-height (symmetric L/R on A & B)

  44. Analysis of Red-Black Cleanup C C  y  y A B   B A z z  Case 3    Case 2 , , , and  are all black-rooted and all have same black-height ( is black or this would be case 1) Case 2

  45. Analysis of Red-Black Cleanup C B  y B A C z   A   z  y   Case 3 , , , and  are all black-rooted and all have same black-height

  46. Questions aboutRed-Black Insert • What is the running time? • Why are there only at most 2 rotations in the cleanup? • How many times can the cleanuprepeat (i.e. how many times can we have case 1 in a row)?

  47. Red-Black Delete • Delete as before • But if you take away a black node: • Black height is not “balanced” • Can generate adjacent red nodes • Must perform cleanup • 4 cases • “Beyond the scope of this course…”

  48. FIN

More Related