Top Banner
Heap Data Structure The heap data structure is an array object that can be viewed as a nearly complete binary tree.
30
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: chapter - 6.ppt

Heap Data Structure

The heap data structure is an array object

that can be viewed as a nearly complete

binary tree.

Page 2: chapter - 6.ppt

Heap

16

2

98

3

10

7

14

4 1

1

2 3

45 6 7

8 9 10

(a)

Page 3: chapter - 6.ppt

Heap

1 2 3 4 5 6 7 8 9 10

16 14 10 8 7 9 3 2 4 1

(b)

Page 4: chapter - 6.ppt

Heap

There are two kinds of binary heaps

• max-heaps

• min heaps

Page 5: chapter - 6.ppt

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

Page 6: chapter - 6.ppt

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

Page 7: chapter - 6.ppt

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)

Page 8: chapter - 6.ppt

Maintaining the Heap Property

16

2

914

3

10

7

4

8 1

1

2 3

45 6 7

8 9 10

i

(a)

Page 9: chapter - 6.ppt

Maintaining the Heap Property

16

2

94

3

10

7

14

8 1

1

2 3

45 6 7

8 9 10

i

(b)

Page 10: chapter - 6.ppt

Maintaining the Heap Property

16

2

96

3

10

7

14

4 1

1

2 3

45 6 7

8 9 10i

(c)

Page 11: chapter - 6.ppt

Building a Heap

BUILD-MAX-HEAP(A)

heap-size[A] length[A]

for i length[A]/2 downto 1

do MAX-HEAPIFY(A, i)

Page 12: chapter - 6.ppt

Building a Heap

A 4 1 3 2 16 9 10 14 8 7

Page 13: chapter - 6.ppt

Building a Heap

4

14

92

10

3

16

1

8 7

1

2 3

45 6 7

8 9 10

i

(a)

Page 14: chapter - 6.ppt

Building a Heap

4

14

92

10

3

16

1

8 7

1

2 3

45 6 7

8 9 10

i

(b)

Page 15: chapter - 6.ppt

Building a Heap

4

2

914

10

3

16

1

8 7

1

2 3

45 6 7

8 9 10

i

(c)

Page 16: chapter - 6.ppt

Building a Heap

4

2

914

3

10

16

1

8 7

1

2 3

45 6 7

8 9 10

i

(d)

Page 17: chapter - 6.ppt

Building a Heap

4

2

914

3

10

7

16

8 1

1

2 3

45 6 7

8 9 10

i

(e)

Page 18: chapter - 6.ppt

Building a Heap

16

2

98

3

10

7

14

4 1

1

2 3

45 6 7

8 9 10

(f)

Page 19: chapter - 6.ppt

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)

Page 20: chapter - 6.ppt

Heapsort Algorithm

16

2

98

3

10

7

14

4 1

(a)

Page 21: chapter - 6.ppt

Heapsort Algorithm

14

2

94

3

10

7

8

1

(b)

16i

Page 22: chapter - 6.ppt

Heapsort Algorithm

10

2

14

3

9

7

8

(c)

16

i

14

Page 23: chapter - 6.ppt

Heapsort Algorithm

9

14

2

3

7

8

(d)

16

i

1410

Page 24: chapter - 6.ppt

Heapsort Algorithm

8

14

3

2

7

(e)

16

i

1410

19

Page 25: chapter - 6.ppt

Heapsort Algorithm

7

1

3

2

4

(f)

16

i

1410

98

Page 26: chapter - 6.ppt

Heapsort Algorithm

4

1

32

(g)

16

i

1410

987

Page 27: chapter - 6.ppt

Heapsort Algorithm

3

12

(h)

16

i

1410

9874

Page 28: chapter - 6.ppt

Heapsort Algorithm

2

1

(i)

16

i

1410

9874

3

Page 29: chapter - 6.ppt

Heapsort Algorithm

1

2

(j)

16

i

1410

9874

3

Page 30: chapter - 6.ppt

Heapsort Algorithm

A 1 2 3 4 7 8 9 10 14 16

k