1 / 78

A Heap Is Efficiently Represented As An Array

9. 8. 7. 6. 7. 2. 6. 5. 1. A Heap Is Efficiently Represented As An Array. 9. 8. 7. 6. 7. 2. 6. 5. 1. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 1. 9. 2. 3. 8. 7. 4. 7. 5. 6. 6. 7. 2. 6. 5. 1. 8. 9. Moving Up And Down A Heap. 9. 8. 7. 6. 7. 2. 6. 5. 1.

geib
Télécharger la présentation

A Heap Is Efficiently Represented As An Array

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. 9 8 7 6 7 2 6 5 1 A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10

  2. 1 9 2 3 8 7 4 7 5 6 6 7 2 6 5 1 8 9 Moving Up And Down A Heap

  3. 9 8 7 6 7 2 6 5 1 7 Inserting An Element Into A Max Heap Complete binary tree with 10 nodes.

  4. 9 Inserting An Element Into A Max Heap 8 7 6 7 2 6 New element is 5. 5 1 7 5

  5. 9 Inserting An Element Into A Max Heap 8 7 6 2 6 7 New element is 20. 5 1 7 7

  6. 9 Inserting An Element Into A Max Heap 8 7 6 2 6 New element is 20. 5 1 7 7 7

  7. 9 Inserting An Element Into A Max Heap 7 6 8 2 6 New element is 20. 5 1 7 7 7

  8. 20 Inserting An Element Into A Max Heap 9 7 6 8 2 6 New element is 20. 5 1 7 7 7

  9. 20 Inserting An Element Into A Max Heap 9 7 6 8 2 6 Complete binary tree with 11 nodes. 5 1 7 7 7

  10. 20 Inserting An Element Into A Max Heap 9 7 6 8 2 6 New element is 15. 5 1 7 7 7

  11. 20 Inserting An Element Into A Max Heap 9 7 6 2 6 New element is 15. 8 5 1 7 7 8 7

  12. 20 Inserting An Element Into A Max Heap 7 15 6 9 2 6 New element is 15. 8 5 1 7 7 8 7

  13. 20 7 15 6 9 2 6 8 5 1 7 7 8 7 Complexity of Inserting Complexity is O(log n), where n is heap size.

  14. 20 7 15 6 9 2 6 8 5 1 7 7 8 7 Deleting the Max Element Max element is in the root.

  15. Deleting the Max Element 7 15 6 9 2 6 After max element is removed. 8 5 1 7 7 8 7

  16. Deleting the Max Element 7 15 6 9 2 6 Heap with 10 nodes. 8 5 1 7 7 8 7 Reinsert 8 into the heap.

  17. Deleting the Max Element 7 15 6 9 2 6 Reinsert 8 into the heap. 5 1 7 7 7

  18. 15 Deleting the Max Element 7 6 9 2 6 Reinsert 8 into the heap. 5 1 7 7 7

  19. 15 Deleting the Max Element 9 7 6 2 6 8 Reinsert 8 into the heap. 5 1 7 7 7

  20. 15 Deleting the Max Element 9 7 6 2 6 8 Max element is 15. 5 1 7 7 7

  21. Deleting the Max Element 9 7 6 2 6 8 After max element is removed. 5 1 7 7 7

  22. Deleting the Max Element 9 7 6 2 6 8 Heap with 9 nodes. 5 1 7 7 7

  23. Deleting the Max Element 9 7 6 2 6 8 Reinsert 7. 5 1

  24. 9 Deleting the Max Element 7 6 2 6 8 Reinsert 7. 5 1

  25. 9 Deleting the Max Element 8 7 6 7 2 6 Reinsert 7. 5 1

  26. 9 8 7 6 7 2 6 5 1 Complexity of Remove Max Element Complexity is O(log n).

  27. 1 3 2 4 5 6 7 11 8 9 7 7 8 10 Initializing a Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

  28. 1 Initializing a Max Heap 3 2 4 5 6 7 Start at rightmost array position that has a child. 11 8 9 7 7 8 10 Index is n/2.

  29. 1 Initializing a Max Heap 3 2 4 11 6 7 Move to next lower array position. 5 8 9 7 7 8 10

  30. 1 Initializing a Max Heap 3 2 4 11 6 7 5 8 9 7 7 8 10

  31. 1 Initializing a Max Heap 3 2 9 11 6 7 5 8 4 7 7 8 10

  32. 1 Initializing a Max Heap 3 2 9 11 6 7 5 8 4 7 7 8 10

  33. 1 Initializing a Max Heap 7 2 9 11 6 3 5 8 4 7 7 8 10

  34. 1 Initializing a Max Heap 7 2 9 11 6 3 5 8 4 7 7 8 10

  35. 1 Initializing a Max Heap 7 11 9 6 3 5 8 4 7 7 8 10 Find a home for 2.

  36. 1 Initializing a Max Heap 7 11 9 10 6 3 8 4 8 7 7 5 Find a home for 2.

  37. 1 Initializing a Max Heap 7 11 9 10 6 3 8 4 7 7 8 5 2 Done, move to next lower array position.

  38. 1 Initializing a Max Heap 7 11 9 10 6 3 2 8 4 7 7 8 5 Find home for 1.

  39. 11 Initializing a Max Heap 7 9 10 6 3 2 8 4 7 7 8 5 Find home for 1.

  40. 11 Initializing a Max Heap 10 7 9 6 3 2 8 4 7 7 8 5 Find home for 1.

  41. 11 Initializing a Max Heap 10 7 9 5 6 3 2 8 4 7 7 8 Find home for 1.

  42. 11 Initializing a Max Heap 10 7 9 5 6 3 2 8 4 7 7 8 1 Done.

  43. 11 Time Complexity 7 9 8 5 6 3 1 4 7 7 8 2 10 Height of heap = h. Number of subtrees with root at level j is <= 2 j-1. Time for each subtree is O(h-j+1).

  44. Time for level j subtrees is <= 2j-1(h-j+1) = t(j). Total time is t(1) + t(2) + … + t(h-1) = O(n). Complexity

  45. Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity. Can meld two leftist tree priority queues in O(log n) time.

  46. Extended Binary Trees Start with any binary tree and add an external node wherever there is an empty subtree. Result is an extended binary tree.

  47. A Binary Tree

  48. An Extended Binary Tree number of external nodes is n+1

  49. The Function s() For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x.

  50. s() Values Example

More Related