Heap Data Structure The heap data structure is an array object that can be viewed as a nearly complete binary tree.
Heap  1 2 3 4 5 6 7 8 9 10 (a) 16 2 9 8 3 10 7 14 4 1
Heap (b) 1 4 2 3 9 7 8 10 14 16 5 4 3 2 1 10 9 8 7 6
Heap <ul><li>There are two kinds of binary heaps </li></ul><ul><li>max-heaps </li></ul><ul><li>min heaps </li></ul>
Maintaining the Heap Property The function of MAX-HEAPIFY is to let the value at A[i] “float down” in the max-heap so that...
Maintaining the Heap Property MAX-HEAPIFY(A, i) l   LEFT(i) r    RIGHT(i) if l <= heap-size[A] and A[l] >A [i] then larg...
Maintaining the Heap Property If r<= heap-size[A] and A[r] > A[largest] then largest    r If largest  ≠ i then exchange A...
Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (a) 16 2 9 14 3 10 7 4 8 1
Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (b) 16 2 9 4 3 10 7 14 8 1
Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (c) 16 2 9 6 3 10 7 14 4 1
Building a Heap BUILD-MAX-HEAP(A) heap-size[A]    length[A] for i    length[A]/2 downto 1 do MAX-HEAPIFY(A, i)
Building a Heap 3 2 16 1 10 4 7 8 14 9 A
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (a) 4 14 9 2 10 3 16 1 8 7
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (b) 4 14 9 2 10 3 16 1 8 7
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (c) 4 2 9 14 10 3 16 1 8 7
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (d) 4 2 9 14 3 10 16 1 8 7
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (e) 4 2 9 14 3 10 7 16 8 1
Building a Heap 1 2 3 4 5 6 7 8 9 10 (f) 16 2 9 8 3 10 7 14 4 1
Heapsort Algorithm HEAPSORT(A) BUILD-MAX-HEAP(A) for I    length[A] downto 2  do exchange A[1]       A[i] heap-size[A] ...
Heapsort Algorithm (a) 16 2 9 8 3 10 7 14 4 1
Heapsort Algorithm (b) i 14 2 9 4 3 10 7 8 1 16
Heapsort Algorithm (c) i 10 2 1 4 3 9 7 8 16 14
Heapsort Algorithm (d) i 9 1 4 2 3 7 8 16 14 10
Heapsort Algorithm (e) i 8 1 4 3 2 7 16 14 10 19
Heapsort Algorithm (f) i 7 1 3 2 4 16 14 10 9 8
Heapsort Algorithm (g) i 4 1 3 2 16 14 10 9 8 7
Heapsort Algorithm (h) i 3 1 2 16 14 10 9 8 7 4
Heapsort Algorithm (i) i 2 1 16 14 10 9 8 7 4 3
Heapsort Algorithm (j) i 1 2 16 14 10 9 8 7 4 3
Heapsort Algorithm k 3 4 7 2 9 1 16 14 10 8 A
Upcoming SlideShare
Loading in …5
×

chapter - 6.ppt

5,007 views
4,591 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,007
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
180
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

chapter - 6.ppt

  1. 1. Heap Data Structure The heap data structure is an array object that can be viewed as a nearly complete binary tree.
  2. 2. Heap 1 2 3 4 5 6 7 8 9 10 (a) 16 2 9 8 3 10 7 14 4 1
  3. 3. Heap (b) 1 4 2 3 9 7 8 10 14 16 5 4 3 2 1 10 9 8 7 6
  4. 4. Heap <ul><li>There are two kinds of binary heaps </li></ul><ul><li>max-heaps </li></ul><ul><li>min heaps </li></ul>
  5. 5. Maintaining the Heap Property The function of MAX-HEAPIFY is to let the value at A[i] “float down” in the max-heap so that the sub-tree rooted at index i becomes a max-heap
  6. 6. Maintaining the Heap Property MAX-HEAPIFY(A, i) l  LEFT(i) r  RIGHT(i) if l <= heap-size[A] and A[l] >A [i] then largest  l else largest  i
  7. 7. Maintaining the Heap Property If r<= heap-size[A] and A[r] > A[largest] then largest  r If largest ≠ i then exchange A[i]  A[largest] MAX-HEAPIFY(A, largest)
  8. 8. Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (a) 16 2 9 14 3 10 7 4 8 1
  9. 9. Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (b) 16 2 9 4 3 10 7 14 8 1
  10. 10. Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (c) 16 2 9 6 3 10 7 14 4 1
  11. 11. Building a Heap BUILD-MAX-HEAP(A) heap-size[A]  length[A] for i  length[A]/2 downto 1 do MAX-HEAPIFY(A, i)
  12. 12. Building a Heap 3 2 16 1 10 4 7 8 14 9 A
  13. 13. Building a Heap 1 2 3 4 5 6 7 8 9 10 i (a) 4 14 9 2 10 3 16 1 8 7
  14. 14. Building a Heap 1 2 3 4 5 6 7 8 9 10 i (b) 4 14 9 2 10 3 16 1 8 7
  15. 15. Building a Heap 1 2 3 4 5 6 7 8 9 10 i (c) 4 2 9 14 10 3 16 1 8 7
  16. 16. Building a Heap 1 2 3 4 5 6 7 8 9 10 i (d) 4 2 9 14 3 10 16 1 8 7
  17. 17. Building a Heap 1 2 3 4 5 6 7 8 9 10 i (e) 4 2 9 14 3 10 7 16 8 1
  18. 18. Building a Heap 1 2 3 4 5 6 7 8 9 10 (f) 16 2 9 8 3 10 7 14 4 1
  19. 19. Heapsort Algorithm HEAPSORT(A) BUILD-MAX-HEAP(A) for I  length[A] downto 2 do exchange A[1]   A[i] heap-size[A]  heap-size[A] -1 MAX-HEAPIFY(A,1)
  20. 20. Heapsort Algorithm (a) 16 2 9 8 3 10 7 14 4 1
  21. 21. Heapsort Algorithm (b) i 14 2 9 4 3 10 7 8 1 16
  22. 22. Heapsort Algorithm (c) i 10 2 1 4 3 9 7 8 16 14
  23. 23. Heapsort Algorithm (d) i 9 1 4 2 3 7 8 16 14 10
  24. 24. Heapsort Algorithm (e) i 8 1 4 3 2 7 16 14 10 19
  25. 25. Heapsort Algorithm (f) i 7 1 3 2 4 16 14 10 9 8
  26. 26. Heapsort Algorithm (g) i 4 1 3 2 16 14 10 9 8 7
  27. 27. Heapsort Algorithm (h) i 3 1 2 16 14 10 9 8 7 4
  28. 28. Heapsort Algorithm (i) i 2 1 16 14 10 9 8 7 4 3
  29. 29. Heapsort Algorithm (j) i 1 2 16 14 10 9 8 7 4 3
  30. 30. Heapsort Algorithm k 3 4 7 2 9 1 16 14 10 8 A

×