AVL Trees: Balanced Data Structures

cse 373 n.w
1 / 91
Embed
Share

Explore the concept of AVL trees for balanced data structures. Discover how AVL trees implement balance, maintain properties, and support operations like insertion and deletion. Learn about the importance of preserving balance throughout the tree to optimize efficiency and performance in data manipulation.

  • AVL Trees
  • Data Structures
  • Balancing
  • Tree Operations
  • Optimization

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSE 373 OCTOBER 13TH AVL TREES

  2. ASSORTED MINUTIAE P1 scripts run on Sunday Apologies for part 1 script failures You will receive the part 1 grade for your part 2 code Given opportunity next week to get points back Leave team member as comment on canvas

  3. TODAYS LECTURE AVL Trees Balance Implementation

  4. REVIEW AVL Trees

  5. REVIEW AVL Trees BST trees with AVL property

  6. REVIEW AVL Trees BST trees with AVL property Abs(height(left) height(right)) <= 1

  7. REVIEW AVL Trees BST trees with AVL property Abs(height(left) height(right)) <= 1 Heights of subtrees can differ by at most one

  8. REVIEW AVL Trees BST trees with AVL property Abs(height(left) height(right)) <= 1 Heights of subtrees can differ by at most one This property must be preserved throughout the tree

  9. AVL OPERATIONS Since AVL trees are also BST trees, they should support the same functionality Insert(key k, value v) Find(key k): Same as BST! Delete(key k): For insert, we should maintain AVL property as we build

  10. AVL OPERATIONS Insert(key k, value v): Insert the key value pair into the dictionary Verify that balance is maintained If not, correct the tree How do we correct the tree?

  11. AVL INSERT 6 Start with the single root

  12. AVL INSERT 6 7 Add 7 to the tree

  13. AVL INSERT 6 7 Add 7 to the tree. Is balance preserved?

  14. AVL INSERT 1 6 7 0 Add 7 to the tree. Is balance preserved? Yes

  15. AVL INSERT 6 7 9 Add 9 to the tree

  16. AVL INSERT 6 7 9 Add 9 to the tree. Is balance preserved?

  17. AVL INSERT 2 6 7 1 9 0 Add 9 to the tree. Is balance preserved? No.

  18. AVL INSERT 2 6 7 1 9 0 How do we correct this imbalance?

  19. AVL INSERT 2 6 7 1 9 0 What shape do we want? What then do we have as the root?

  20. AVL INSERT 7 0 0 9 6 0 Since 7 must be the root, we rotate that node into position.

  21. AVL ROTATION To correct this case: B must become the root A B C

  22. AVL ROTATION To correct this case: B must become the root We rotate B to the root position A B C

  23. AVL ROTATION To correct this case: B must become the root We rotate B to the root position A becomes the left child of B A B C B C A

  24. AVL ROTATION To correct this case: B must become the root We rotate B to the root position A becomes the left child of B This is called the left rotation A B C B C A

  25. AVL ROTATION Right rotation C B A B C A

  26. AVL ROTATION Right rotation Symmetric concept C B A B C A

  27. AVL ROTATION Right rotation Symmetric concept B must become the new root C B A B C A

  28. AVL ROTATION These are the single rotations

  29. AVL ROTATION These are the single rotations In general, this rotation occurs when an addition is made to the right-right or left-left grandchild

  30. AVL ROTATION These are the single rotations In general, this rotation occurs when an addition is made to the right-right or left-left grandchild The balance might not be off on the parent! An insert might upset balance up the tree

  31. AVL ROTATION General case Suppose this tree is balanced, {X,Y,Z} all have the same height C B Z X Y

  32. AVL ROTATION General case Suppose this tree is balanced, {X,Y,Z} all have the same height Adding A, puts C out of balance 2 C 1 B Z X Y A

  33. AVL ROTATION General case Suppose this tree is balanced, {X,Y,Z} all have the same height Adding A, puts C out of balance Rotate B up and pass the Y subtree to C 2 C 1 B Z X Y A

  34. AVL ROTATION General case Suppose this tree is balanced, {X,Y,Z} all have the same height Adding A, puts C out of balance Rotate B up and pass the Y subtree to C 0 B 0 C X Z Y A

  35. AVL ROTATION General case Suppose this tree is balanced, {X,Y,Z} all have the same height Adding A, puts C out of balance Rotate B up and pass the Y subtree to C Perform this rotation at the lowest point of imbalance 0 B 0 C X Z Y A

  36. AVL ROTATION These two rotations (right-right and left- left) are symmetric and can be solved the same way

  37. AVL ROTATION These two rotations (right-right and left- left) are symmetric and can be solved the same way Named by the location of the added node relative to the unbalanced node

  38. AVL ROTATION These two rotations (right-right and left- left) are symmetric and can be solved the same way Named by the location of the added node relative to the unbalanced node What are the other two cases?

  39. AVL ROTATION Right left case A C B

  40. AVL ROTATION Right left case Again, A is out of balance A C B

  41. AVL ROTATION Right left case Again, A is out of balance This time, the addition (B) comes between A and C A C B

  42. AVL ROTATION Right left case Again, A is out of balance This time, the addition (B) comes between A and C In this case, the grandchild must become the root. A C B

  43. AVL ROTATION Right left case Again, A is out of balance This time, the addition (B) comes between A and C In this case, the grandchild must become the root. A C B B C A

  44. AVL ROTATION Identifying what should be the new root is key A C B B C A

  45. AVL ROTATION Identifying what should be the new root is key A C Imagine lifting up the root B B C A

  46. AVL ROTATION Identifying what should be the new root is key A C Imagine lifting up the root Where will the children have to go to maintain the search property? B B C A

  47. AVL ROTATION I apologize for what you are about to see

  48. AVL ROTATION This is for your reference later. h+3 h+2 a c h+2 h b h+1 h+1 h+1 a b c h Z h h h h-1 h-1 h-1 U V X X U Z V

  49. AVL ROTATION 7 4 10 3 9 5 12 11 15 2 8 6 14 16 Let s do an example. Insert(13)

  50. AVL ROTATION 7 4 10 3 9 5 12 11 15 2 8 6 14 16 13 Where is the imbalance?

Related


More Related Content