Top Banner
Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design Jiraporn Pooksook Naresuan University
57

Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Jul 19, 2020

Download

Documents

dariahiddleston
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: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Ch11: Heap and Heap Sort

305233, 305234 Algorithm Analysis and Design

Jiraporn PooksookNaresuan University

Page 2: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

What is the (binary) heap?

• The (binary) heap data structure is an array object that can be viewed as a nearly complete binary tree. Each node of the tree corresponds to an element of the array that stores the value in the node.the node.

• An array A that represents a heap is an object with two attributes:– length[A] is the number of elements in the array

– heap-size[A] is the number of elements in the heap stored within array A.

Page 3: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: heap

16

1

14

2

10

3

4 5 6 7

Root is A[1]

parent(i)=

left(i)= 2i

2/i

8 7 9 3

2

8

4

9

1

10

16 14 10 8 7 9 3 2 4 1

right(i)= 2i +1

Page 4: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Binary Heap

• There are two kinds of binary heaps:

• Max-heaps

– For every node i other than the root, A[parent(i)] ≥ A[i]A[parent(i)] ≥ A[i]

• Min-heaps

– For every node i other than the root, A[parent(i)] ≤ A[i]

Page 5: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-heap

16

1

14

2

10

3

4 5 6 7

8 7 9 3

2

8

4

9

1

10

16 14 10 8 7 9 3 2 4 1

Height = lg n

Page 6: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Max-Heapify(A,i)l = left(i)r = right(i)if l ≤ heap-size[A] and A[l] > A[i]

then largest = lelse largest = ielse largest = i

if r ≤ heap-size[A] and A[r] > A[largest]then largest = r

If largest != ithen exchanged A[i] and A[largest]

Max-Heapify(A,largest)

Page 7: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-Heapify(A,2)

16

1

4

2

10

3

4 5 6 7

4 is not ≥ its children and 14 > 4. Then

we swap them

14 7 9 3

2

8

8

9

1

10

16 4 10 14 7 9 3 2 8 1

A[i] largest

Page 8: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-Heapify(A,2)

16

1

14

2

10

3

4 5 6 7

4 7 9 3

2

8

8

9

1

10

16 14 10 4 7 9 3 2 8 1

Page 9: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-Heapify(A,4)

16

1

14

2

10

3

4 5 6 7

4 is not ≥ its children and

8 > 4. Then we swap them

4 7 9 3

2

8

8

9

1

10

16 14 10 4 7 9 3 2 8 1

A[i] largest

Page 10: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-Heapify(A,4)

16

1

14

2

10

3

4 5 6 7

8 7 9 3

2

8

4

9

1

10

16 14 10 8 7 9 3 2 4 1

Page 11: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Analyze: Running time of Max-Heapify(A,i)

16

1

14

2

10

3

4 5 6 7

8 7 9 3

2

8

4

9

1

10The children’s

subtrees each have size at most 2n/3.

)1()3/2()( nTnT

Page 12: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Analyze: Running time of Max-Heapify(A,i)

• We have

• Determine which case of the master theorem applies:

• We have a=1, b=3/2, f(n)= 1

1)3/2()( nTnT

• We have a=1, b=3/2, f(n)= 1

• Thus we have

• Since we can apply case 2 of the master theorem and conclude that the solution is

101loglog 2/3 nnn ab

)1()()( 0 nnf

)(lg)lg()( log nnnnT ab

ข้อสงัเกต 1 = n0 , case 2

Page 13: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Build-Max-Heap(A)

heap-size[A] = length[A]for i = downto 1

do Max-Heapify(A,i) 2/][Alength

Page 14: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

4

1

1

2

3

3

4 5 6 7

i

2 16 9 10

14

8

8

9

7

10

4 1 3 2 16 9 10 14 8 7

Page 15: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

4

1

1

2

3

3

4 5 6 7

i

2 16 9 10

14

8

8

9

7

10

4 1 3 2 16 9 10 14 8 7

Page 16: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

4

1

1

2

3

3

4 5 6 7

14 16 9 10

2

8

8

9

7

10

4 1 3 14 16 9 10 2 8 7

Page 17: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

4

1

1

2

3

3

4 5 6 7

i

14 16 9 10

2

8

8

9

7

10

4 1 3 14 16 9 10 2 8 7

Page 18: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

4

1

1

2

10

3

4 5 6 7

14 16 9 3

2

8

8

9

7

10

4 1 10 14 16 9 3 2 8 7

Page 19: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

4

1

1

2

10

3

4 5 6 7

i

14 16 9 3

2

8

8

9

7

10

4 1 10 14 16 9 3 2 8 7

Page 20: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

4

1

16

2

10

3

4 5 6 7

14 7 9 3

2

8

8

9

1

10

4 16 10 14 7 9 3 2 8 1

Page 21: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

4

1

16

2

10

3

4 5 6 7

i

14 7 9 3

2

8

8

9

1

10

4 16 10 14 7 9 3 2 8 1

Page 22: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Build-Max-Heap(A)

16

1

14

2

10

3

4 5 6 7

8 7 9 3

2

8

4

9

1

10

16 14 10 8 7 9 3 2 4 1

Page 23: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Analyze: Build-Max-Heap(A)heap-size[A] = length[A]for i = downto 1

do Max-Heapify(A,i)

2/][Alength

loop invariant = at the start of each iteration of the for loop of lines 2-3, each node

i+1, i+2,…,n is the root of a max-heap.

Initialization: Before running loop 1 , i = . Each node +1, +2, …, n is a leaf and is the root of a trivial max-heap. (True!!)

Maintenance:if children of node i are numbered higher than I, they are both roots of max-heaps.

The condition required for the call Max-Heapify(A,i) to make node I a max-heap root.Decrementing in the for loop update reestablishes the loop invariant for the next loop. (True!!)

Termination: at termination, i =0. each node 1,2,….,n Is the root of a max-heap. Node 1 is. (True!!)

i+1, i+2,…,n is the root of a max-heap.

2/n 2/n 2/n

Page 24: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Analyze running time: Build-Max-Heap(A)

heap-size[A] = length[A]for i = downto 1

do Max-Heapify(A,i) 2/][Alength

Times1n/2+1n/2.O(lg n)

T(n) = O(n lg n)

Tight Analysis: an n-element heap has height = and at most nodes of any height h.The time required by Max-Heapify when called on a node of height h is O(h). Thus running time can be bounded as O(n)

nlg

12/ hn

Page 25: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Heapsort(A)

Build-Max-Heap(A)for i = length[A] downto 2

do exchange A[1] and A[i]heap-size[A] = heap-size[A] -1heap-size[A] = heap-size[A] -1Max-Heapify(A,1)

Page 26: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

16

1

14

2

10

3

4 5 6 7

8 7 9 3

2

8

4

9

1

10

16 14 10 8 7 9 3 2 4 1

i

Page 27: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

1

1

14

2

10

3

4 5 6 7

8 7 9 3

2

8

4

9

16

10

1 14 10 8 7 9 3 2 4 16

Page 28: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

14

1

1

2

10

3

4 5 6 7

8 7 9 3

2

8

4

9

16

10

14 1 10 8 7 9 3 2 4 16

Page 29: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

14

1

8

2

10

3

4 5 6 7

1 7 9 3

2

8

4

9

16

10

14 8 10 1 7 9 3 2 4 16

Page 30: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

14

1

8

2

10

3

4 5 6 7

4 7 9 3

2

8

1

9

16

10

14 8 10 4 7 9 3 2 1 16

Page 31: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

14

1

8

2

10

3

4 5 6 7

4 7 9 3

2

8

1

9

16

10

14 8 10 4 7 9 3 2 1 16

i

Page 32: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

10

1

8

2

9

3

4 5 6 7

4 7 1 3

2

8

14

9

16

10

10 8 9 4 7 1 3 2 14 16

Page 33: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

10

1

8

2

9

3

4 5 6 7

4 7 1 3

2

8

14

9

16

10

10 8 9 4 7 1 3 2 14 16

i

Page 34: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

9

1

8

2

3

3

4 5 6 7

4 7 1 2

10

8

14

9

16

10

9 8 3 4 7 1 2 10 14 16

Page 35: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

9

1

8

2

3

3

4 5 6 7

i

4 7 1 2

10

8

14

9

16

10

9 8 3 4 7 1 2 10 14 16

Page 36: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

8

1

7

2

3

3

4 5 6 7

4 2 1 9

10

8

14

9

16

10

8 7 3 4 7 1 9 10 14 16

Page 37: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

8

1

7

2

3

3

4 5 6 7

i

4 2 1 9

10

8

14

9

16

10

8 7 3 4 7 1 9 10 14 16

Page 38: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

7

1

4

2

3

3

4 5 6 7

1 2 8 9

10

8

14

9

16

10

7 4 3 1 2 8 9 10 14 16

Page 39: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

7

1

4

2

3

3

4 5 6 7

i

1 2 8 9

10

8

14

9

16

10

7 4 3 1 2 8 9 10 14 16

Page 40: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

4

1

2

2

3

3

4 5 6 7

1 7 8 9

10

8

14

9

16

10

4 2 3 1 7 8 9 10 14 16

Page 41: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

4

1

2

2

3

3

4 5 6 7

i

1 7 8 9

10

8

14

9

16

10

4 2 3 1 7 8 9 10 14 16

Page 42: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

3

1

2

2

1

3

4 5 6 7

4 7 8 9

10

8

14

9

16

10

3 2 1 4 7 8 9 10 14 16

Page 43: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

3

1

2

2

1

3

4 5 6 7

i

4 7 8 9

10

8

14

9

16

10

3 2 1 4 7 8 9 10 14 16

Page 44: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

2

1

1

2

3

3

4 5 6 7

4 7 8 9

10

8

14

9

16

10

2 1 3 4 7 8 9 10 14 16

Page 45: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

2

1

1

2

3

3

4 5 6 7

i

4 7 8 9

10

8

14

9

16

10

2 1 3 4 7 8 9 10 14 16

Page 46: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Heapsort(A)

1

1

2

2

3

3

4 5 6 7

4 7 8 9

10

8

14

9

16

10

1 2 3 4 7 8 9 10 14 16

Page 47: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Analyze: Heapsort(A)

Build-Max-Heap(A)for i = length[A] downto 2

do exchange A[1] and A[i]heap-size[A] = heap-size[A] -1

TimesO(n)nn-1n-1heap-size[A] = heap-size[A] -1

Max-Heapify(A,1)

n-1n-1 . O(lg n)

T(n) = O(n lg n)

Page 48: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Practice: Heapsort

8 17 12 15 92 16 11 52 41

Page 49: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Priority Queues

• A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key.

• There are two kinds of priority queues:• A max priority queue supports these operations:

– Insert(S,x) -> inserts the element x into the set S.– Maximum(S) -> returns the element of S with the largest key.– Maximum(S) -> returns the element of S with the largest key.– Extract-Max(S) -> removes and returns the element of S with

the largest key.– Increase-Key(S,x,k) -> increases the value of element x’s key to

the new value k which is assumed to be as large as x’s current key value.

• A min priority queue supports these operations:– Insert(S,x) , Minimum(S) , Extract-Min(S), Decrease-Key(S,x,k)

Page 50: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Max-Priority Queue

Pseudo-code: Heap-Maximum(A)return A[1]

Running time = O(1)

Page 51: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Max-Priority Queue

Pseudo-code: Heap-Extract-Max(A)if heap-size[A] < 1

then error “heap underflow”

max=A[1]max=A[1]A[1] = A[heap-size[A] ]heap-size[A] = heap-size[A] – 1Max-Heapify(A,1)return max

Running time = O(lg n)

Page 52: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Max-Priority Queue

Pseudo-code: Heap-Increase-Key(A,I,Key)if key < A[i]

then error “new key is smaller than current key”

A[i] = keyA[i] = keywhile i > 1 and A[parent(i)] < A[i]

do exchange A[i] and A[parent(i)]i = parent(i)

Running time = O(lg n)

Page 53: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Max-Priority Queue

Pseudo-code: Max-Heap-Insert(A,key)heap-size(A) = heap-size[A]+1A[heap-size[A]] = Heap-Increase-Key(A, heap-size[A], key)

Running time = O(lg n)

Page 54: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-Priority Queue: Heap-Increase-Key

16

1

14

2

10

3

4 5 6 7

8 7 9 3

2

8

4

9

1

10

i

Page 55: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-Priority Queue: Heap-Increase-Key

16

1

14

2

10

3

4 5 6 7

8 7 9 3

2

8

15

9

1

10

15>4

Page 56: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-Priority Queue: Heap-Increase-Key

16

1

14

2

10

3

4 5 6 7

15 7 9 3

2

8

8

9

1

10

Page 57: Ch11: Heap and Heap Sort - Naresuan University · Ch11: Heap and Heap Sort 305233, 305234 Algorithm Analysis and Design JirapornPooksook NaresuanUniversity. What is the (binary) heap?

Example: Max-Priority Queue: Heap-Increase-Key

16

1

15

2

10

3

4 5 6 7

14 7 9 3

2

8

8

9

1

10