A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
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.
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.
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.
Reprise: The ADT Heap Interface considered in Chapter 23 Listing 23-6 Adapted from Pearson Education, Inc.
Example Adapted from Pearson Education, Inc.
Possible Implementation of a PQ as Sorted List (ch 11) Figure 11-20 Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes Adapted from Pearson Education, Inc.
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.
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.
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.
The steps in adding 85 to a maxheap Adapted from Pearson Education, Inc.
A revision of the steps to avoid swaps Adapted from Pearson Education, Inc.
An array representation of the steps Adapted from Pearson Education, Inc.
An array representation of the steps Adapted from Pearson Education, Inc.
RemoveMax Adapted from Pearson Education, Inc.
Adding 20, 40, 30, 10, 90, and 70 Adapted from Pearson Education, Inc.
The steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap Adapted from Pearson Education, Inc.
The steps in creating a heap of the entries 20, 40, 30, 10, 90, and 70 by using reheap Adapted from Pearson Education, Inc.
Figure 26-9 A trace of heap sort Adapted from Pearson Education, Inc.
Figure 26-9 A trace of heap sort Adapted from Pearson Education, Inc.
Figure 26-9 A trace of heap sort Adapted from Pearson Education, Inc.
Efficiency Adapted from Pearson Education, Inc.
End Chapter 26 Adapted from Pearson Education, Inc.