A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

30
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Transcript of A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Page 1: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

A Heap Implementation

Chapter 26

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 2: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Contents

• Reprise: The ADT Heap

• Using an Array to Represent a Heap

• Adding an Entry

• Removing the Root

• Creating a Heap

• Heap Sort

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 3: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Objectives

• Use an array to represent a heap

• Add an entry to an array-based heap

• Remove the root of an array-based heap

• Create a heap from given entries

• Sort an array by using a heap sort

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 4: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Reprise: The ADT Heap

• A heap: Complete binary tree Nodes contain Comparable objects

• A maxheap: Object in each node greater than or equal to

the objects in node’s descendants

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 5: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Reprise: The ADT Heap

• Interface considered in Chapter 23

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 6: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Array to Represent a Heap

• Begin by using array to represent complete binary tree. Complete tree is full to its next-to-last level Leaves on last level are filled from left to right

• Locate either children or parent of any node by performing simple computation on node’s number

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 7: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-1 (a) A complete binary tree with its nodes numbered in level order; (b) its representation as an array

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 8: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Array to Represent a Heap

• View code of class MaxHeap,Listing 26-1

• Data fields: Array of Comparable heap entries Index of last entry in the array A constant for default initial capacity of heap

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Note: Code listing filesmust be in same folder

as PowerPoint filesfor links to work

Note: Code listing filesmust be in same folder

as PowerPoint filesfor links to work

Page 9: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-2 The steps in adding 85 to the maxheap in Figure 26-1a

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 10: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-2 The steps in adding 85 to the maxheap in Figure 26-1a

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 11: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-2 The steps in adding 85 to the maxheap in Figure 26-1a

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 12: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-3 A revision of the steps shown in Figure 26-2, to avoid swaps

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 13: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-3 A revision of the steps shown in Figure 26-2, to avoid swaps

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 14: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-4 An array representation of the steps in Figure 26-3

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 15: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-4 An array representation of the steps in Figure 26-3

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 16: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-5 The steps to remove the entry in the root of the maxheap in Figure 26-3d

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 17: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-5 The steps to remove the entry in the root of the maxheap in Figure 26-3d

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 18: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-6 The steps that transform a semiheap into a heap without swaps

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 19: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-6 The steps that transform a semiheap into a heap without swaps

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 20: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-7 The steps in adding 20, 40, 30, 10, 90, and 70 to an initially empty heap

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 21: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-7 The steps in adding 20, 40, 30, 10, 90, and 70 to an initially empty heap

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 22: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-7 The steps in adding 20, 40, 30, 10, 90, and 70 to an initially empty heap

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 23: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-8 the steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 24: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-8 the steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 25: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-8 the steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap

Complexity

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 26: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-9 A trace of heap sort

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 27: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-9 A trace of heap sort

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 28: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Figure 26-9 A trace of heap sort

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 29: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Efficiency

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Page 30: A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

End

Chapter 26

Copyright ©2012 by Pearson Education, Inc. All rights reserved