chapter - 6.ppt
Upcoming SlideShare
Loading in...5
×
 

chapter - 6.ppt

on

  • 3,659 views

 

Statistics

Views

Total Views
3,659
Views on SlideShare
3,657
Embed Views
2

Actions

Likes
0
Downloads
79
Comments
0

2 Embeds 2

http://www.slideshare.net 1
http://coderwall.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

chapter - 6.ppt chapter - 6.ppt Presentation Transcript

  • 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
    • There are two kinds of binary heaps
    • max-heaps
    • min heaps
  • 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
  • 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
  • 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)
  • 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]  heap-size[A] -1 MAX-HEAPIFY(A,1)
  • 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