1 / 85

Advanced Trees Part II

Advanced Trees Part II. Briana B. Morrison Adapted from Alan Eugenio & William J. Collins. Topics. AVL Trees Part III 2-3-4 Search Trees Red Black Trees. AVL Tree Definition. AVL trees are balanced.

amina
Télécharger la présentation

Advanced Trees Part II

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. Advanced TreesPart II Briana B. Morrison Adapted from Alan Eugenio & William J. Collins

  2. Topics • AVL Trees • Part III • 2-3-4 Search Trees • Red Black Trees Advanced Trees (Part II)

  3. Advanced Trees (Part II)

  4. Advanced Trees (Part II)

  5. Advanced Trees (Part II)

  6. Advanced Trees (Part II)

  7. AVL Tree Definition • AVL trees are balanced. • An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1. An example of an AVL tree where the heights are shown next to the nodes: Advanced Trees (Part II)

  8. Balance Factor • The balance factor is defined as: • The height of the left subtree minus the height of the right subtree • The tree is balanced if the absolute value of the balance factor is <=1 │ ht(TL) – ht(TR) │ <= 1 Advanced Trees (Part II)

  9. Advanced Trees (Part II)

  10. Advanced Trees (Part II)

  11. 20 HEIGHT OF LEFT SUBTREE = ? HEIGHT OF RIGHT SUBTREE = ? Advanced Trees (Part II)

  12. Advanced Trees (Part II)

  13. NOPE! THE HEIGHT OF THE LEFT SUBTREE IS 1 AND THE HEIGHT OF THE RIGHT SUBTREE IS –1. Advanced Trees (Part II)

  14. Advanced Trees (Part II)

  15. Advanced Trees (Part II)

  16. Advanced Trees (Part II)

  17. NOPE: THE LEFT AND RIGHT SUBTREES ARE NOT AVL TREES, SO THE TREE IS NOT AN AVL TREE. Advanced Trees (Part II)

  18. NOPE! THE HEIGHT OF THE LEFT SUBTREE IS 2 AND THE HEIGHT OF THE RIGHT SUBTREE IS 0. Advanced Trees (Part II)

  19. Advanced Trees (Part II)

  20. Advanced Trees (Part II)

  21. Advanced Trees (Part II)

  22. Advanced Trees (Part II)

  23. Advanced Trees (Part II)

  24. Advanced Trees (Part II)

  25. Advanced Trees (Part II)

  26. Advanced Trees (Part II)

  27. Advanced Trees (Part II)

  28. Advanced Trees (Part II)

  29. Advanced Trees (Part II)

  30. Advanced Trees (Part II)

  31. Advanced Trees (Part II)

  32. Advanced Trees (Part II)

  33. n(2) 3 n(1) 4 Height of an AVL Tree • Fact: The height of an AVL tree storing n keys is O(log n). • Proof: Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h. • We easily see that n(1) = 1 and n(2) = 2 • For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2. • That is, n(h) = 1 + n(h-1) + n(h-2) • Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction), n(h) > 2in(h-2i) • Solving the base case we get: n(h) > 2 h/2-1 • Taking logarithms: h < 2log n(h) +2 • Thus the height of an AVL tree is O(log n) Advanced Trees (Part II)

  34. Advanced Trees (Part II)

  35. Advanced Trees (Part II)

  36. Advanced Trees (Part II)

  37. Advanced Trees (Part II)

  38. Advanced Trees (Part II)

  39. Advanced Trees (Part II)

  40. Advanced Trees (Part II)

  41. Advanced Trees (Part II)

  42. Advanced Trees (Part II)

  43. Advanced Trees (Part II)

  44. Advanced Trees (Part II)

  45. Advanced Trees (Part II)

  46. ITEMS NOT IN THE SUBTREE OF THE ITEM ROTATED ABOUT ARE UNAFFECTED BY THE ROTATION. • A ROTATION TAKES CONSTANT TIME. • BEFORE AND AFTER A ROTATION, THE TREE IS STILL A BINARY SEARCH TREE. • THE CODE FOR A LEFT ROTATION IS SYMMETRIC TO THE CODE FOR A RIGHT ROTATION: SIMPLY SWAP “left” AND “right”. Advanced Trees (Part II)

  47. Now let’s look at some examples of inserting into an AVL tree to get an idea of how all this rotation stuff works: Advanced Trees (Part II)

  48. Example – Insert numbers 1-7 into AVL Advanced Trees (Part II)

  49. Advanced Trees (Part II)

  50. Advanced Trees (Part II)

More Related