Download presentation
Presentation is loading. Please wait.
Published byHadi Budiman Modified over 5 years ago
1
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
2
Contents Reprise: The ADT Heap Using an Array to Represent a Heap
Adding an Entry Removing the Root Creating a Heap Heap Sort Adapted from Pearson Education, Inc.
3
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 Adapted from Pearson Education, Inc.
4
Heaps (ch23) Complete binary tree: nodes contain Comparable objects
Organization Each node contains object no smaller (or no larger) than objects in descendants Maxheap, object in node greater than or equal to descendant objects Minheap, object in node less than or equal to descendant objects Adapted from Pearson Education, Inc.
5
Reprise: The ADT Heap Interface considered in Chapter 23 Listing 23-6
Adapted from Pearson Education, Inc.
6
Example Adapted from Pearson Education, Inc.
7
Possible Implementation of a PQ as Sorted List (ch 11)
Figure Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes Adapted from Pearson Education, Inc.
8
Possible Implementation of a PQ as a Heap (ch 23)
public class PriorityQueue <T extends Comparable<? super T >> implements PriorityQueueInterface < T > { private MaxHeapInterface < T > pq; public PriorityQueue () { pq = new MaxHeap < T > (); } // end default constructor public void add (T newEntry) { pq.add (newEntry); } // end add //Implementations of remove, peek, isEmpty, getSize, and clear are here. } // end PriorityQueue Adapted from Pearson Education, Inc.
9
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 Adapted from Pearson Education, Inc.
10
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 Adapted from Pearson Education, Inc.
11
The steps in adding 85 to a maxheap
Adapted from Pearson Education, Inc.
12
A revision of the steps to avoid swaps
Adapted from Pearson Education, Inc.
13
An array representation of the steps
Adapted from Pearson Education, Inc.
14
An array representation of the steps
Adapted from Pearson Education, Inc.
15
RemoveMax Adapted from Pearson Education, Inc.
16
Adding 20, 40, 30, 10, 90, and 70 Adapted from Pearson Education, Inc.
17
The steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap
Adapted from Pearson Education, Inc.
18
The steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap
Adapted from Pearson Education, Inc.
19
Figure 26-9 A trace of heap sort
Adapted from Pearson Education, Inc.
20
Figure 26-9 A trace of heap sort
Adapted from Pearson Education, Inc.
21
Figure 26-9 A trace of heap sort
Adapted from Pearson Education, Inc.
22
Efficiency Adapted from Pearson Education, Inc.
23
End Chapter 26 Adapted from Pearson Education, Inc.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.