1 / 30

AVL Trees

AVL Trees. Balanced Binary Search Trees (not covered in book, but related to pp 165-169). AVL Trees. First-invented self-balancing binary search tree Named after its two inventors, G.M. Adelson-Velsky and E.M. Landis,

mark-wood
Download Presentation

AVL 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. AVL Trees Balanced Binary Search Trees(not covered in book, but related to pp 165-169)

  2. AVL Trees • First-invented self-balancing binary search tree • Named after its two inventors, • G.M. Adelson-Velsky and • E.M. Landis, • published it in their 1962 paper "An algorithm for the organization of information."

  3. AVL Properties • First, its a binary search tree... • L <= P and P <= R P L R

  4. Question? • Is this a binary tree search tree? 50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68

  5. Min Heap P <= L and P <= R Max Heap P >= L and P >= R BTW, Binary Heap Properties P L R

  6. AVL Properties • An AVL tree is a balanced binary tree • To understand balance we need to understand the notion of Tree Height 55 Height 2 32 71 Height 1 64 86 Height 0

  7. AVL Properties • By default, nodes with no children have a height of Height of 0. 55 Height 2 Height 0 32 71 Height 1 Height 0 64 86 Height 0

  8. AVL Properties • But, we must also understand the concept of Sub-trees Height = max(L.height, R.height) + 1 sub-tree L has a height of 0 sub-tree R has a height of 1 55 Height 2 Height 0 32 71 Height 1 Height 0 64 86 Height 0

  9. AVL Properties • Also empty sub-trees have a Height of -1 Height = max(L.height, R.height) + 1 44 Height = 2 = max(0, 1) + 1 58 Height = 1 = max(-1, 0) + 1 91 Height = 0 = max(-1,-1) + 1

  10. AVL Properties • Anyway, the AVL Balance Property is as follows... • For ALL nodes, the Height of the Left and Right Sub-trees can only differ by 1. P A Node L R

  11. AVL Properties • Wouldn’t this be a better Balance property? • For ALL nodes, the Height of the Left and Right Sub-trees must be equal! P A Node L R

  12. 1 3 7 15 31 0 AVL Properties • For ALL nodes, the Height of the Left and Right Sub-trees must be equal!

  13. 1 3 7 15 31 0 AVL Properties • For ALL nodes, the Height of the Left and Right Sub-trees must be equal! • Strict Balance Conditions are too restrictive.

  14. Question? • Is this an AVL Tree? 50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68

  15. Question? • Is this an AVL Tree? 50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 30 34 43 49 52 54 58 68

  16. Question? • Is this an AVL Tree? 50 4 29 3 70 3 2 12 2 35 56 2 77 1 5 1 17 1 32 1 46 1 53 1 60 1 71 79 3 9 15 21 30 34 43 49 52 54 58 68

  17. Question? • Is this an AVL Tree? 50 29 70 12 35 56 77 5 17 32 79 3

  18. Question? • Is this an AVL Tree? 50 4 29 3 70 2 1 12 2 35 56 0 77 1 5 1 17 0 32 0 79 0 3 0

  19. Question? • Is this an AVL Tree? +1 50 4 +1 -1 29 3 70 2 +1 +1 0 -1 1 12 2 35 56 0 77 1 +1 0 0 0 5 1 17 0 32 0 79 0 0 3 0

  20. Question? • Is this an AVL Tree? 50 29 70 12 35 56 77 5 17 46 53 60 71 79 3 9 15 21 49 52 54 58 68

  21. Question? • No 50 29 70 12 35 56 77 5 17 46 53 60 71 79 3 9 15 21 49 52 54 58 68

  22. Question? • Did this fix the problem? 50 29 70 12 46 56 77 5 17 35 49 53 60 71 79 3 9 15 21 52 54 58 68

  23. Question? 50 • Is this an AVL Tree? 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68 69

  24. 50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68 69 0 Question? • Is this an AVL Tree? +2 -1 -1 -1 0

  25. 50 29 70 12 35 56 77 5 17 32 46 53 60 71 79 3 9 15 21 28 34 43 51 52 54 58 68 69 Question? • Is this an AVL Tree? +2 -1 -1 -1 0

  26. Question? 50 • Is this an AVL Tree? +2 29 -1 60 12 35 56 77 70 -1 5 17 32 46 53 71 79 -1 3 9 15 21 28 34 43 51 52 54 58 68 0 69

  27. Question? 50 • Is this an AVL Tree? 0 29 60 12 35 56 70 5 17 32 46 53 58 68 77 3 9 15 21 28 34 43 51 52 54 69 71 79

  28. Correcting Imbalance • After every insertion • Check to see if an imbalance was created. • All you have to do backtrack up the tree • If you find an imbalance, correct it. • As long as the original tree is an AVL tree, there are only 4 types of imbalances that can occur.

  29. Imbalances • Left-Left (Single Rotation) • Right-Right(Single Rotation) • Left-Right (Double Rotation) • Right-Left(Double Rotation)

  30. Illustration • http://www.eli.sdsu.edu/courses/fall96/cs660/notes/avl/avl.html

More Related