chapter 15 heaps. copyright © 2005 pearson addison-wesley. all rights reserved. 15-2 chapter...
TRANSCRIPT
![Page 1: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/1.jpg)
Chapter 15
Heaps
![Page 2: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/2.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2
Chapter Objectives
• Define a heap abstract data structure
• Demonstrate how a heap can be used to solve problems
• Examine various heap impmentations
• Compare heap implementations
![Page 3: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/3.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-3
Heaps
• A heap is a binary tree with two added properties:– It is a complete tree
– For each node, the node is less than or equal to both the left child and the right child
• This definition described a minheap
![Page 4: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/4.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-4
Heaps
• In addition to the operations inherited from a binary tree, a heap has the following additional operations:– addElement
– removeMin
– findMin
![Page 5: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/5.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-5
FIGURE 15.1 The operations on a heap
![Page 6: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/6.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-6
Listing 15.1
![Page 7: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/7.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-7
FIGURE 15.2 UML description of the HeapADT
![Page 8: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/8.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-8
The addElement Operation
• The addElement method adds a given element to the appropriate location in the heap
• A binary tree is considered complete if all of the leaves are level h or h-1 where h = log2n and n is the number of elements in the tree
![Page 9: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/9.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-9
The addElement Operation
• Since a heap is a complete tree, there is only one correct location for the insertion of a new node– Either the next open position from the left
at level h
– Or the first position in level h+1 if level h is full
![Page 10: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/10.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-10
The addElement Operation
• Once we have located the new node in the proper position, then we must account for the ordering property
• We simply compare the new node to its parent value and swap the values if necessary
• We continue this process up the tree until either the new value is greater than its parent or the new value becomes the root of the heap
![Page 11: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/11.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-11
FIGURE 15.3 Two minheaps containing the same data
![Page 12: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/12.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-12
FIGURE 15.4 Insertion points for a heap
![Page 13: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/13.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-13
FIGURE 15.5 Insertion and reordering in a heap
![Page 14: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/14.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-14
The removeMin Operation
• The removeMin method removes the minimum element from the heap
• The minimum element is always stored at the root
• Thus we have to return the root element and replace it with another element
![Page 15: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/15.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-15
The removeMin Operation
• The replacement element is always the last leaf
• The last leaf is always the last element at level h
![Page 16: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/16.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-16
FIGURE 15.6 Examples of the last leaf in a heap
![Page 17: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/17.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-17
The removeMin Operation
• Once the element stored in the last leaf has been moved to the root, the heap will have to reordered
• This is accomplished by comparing the new root element to the smaller of its children and the swapping them if necessary
• This process is repeated down the tree until the element is either in a leaf or is less than both of its children
![Page 18: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/18.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-18
FIGURE 15.7 Removal and reordering in a heap
![Page 19: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/19.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-19
Using Heaps: Heap Sort
• Given the ordering property of a heap, it is natural to think of using a heap to sort a list of objects
• One approach would be to simply add all of the objects to a heap and then remove them one at a time in ascending order
![Page 20: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/20.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-20
Using Heaps: Heap Sort• Insertion into a heap is O(log n) for any
given node and thus would O(n log n) for n nodes to build the heap
• However, it is also possible to build a heap in place using an array
• Since we know the relative position of each parent and its children in the array, we simply start with the first non-leaf node in the array, compare it to its children and swap if necessary
![Page 21: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/21.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-21
Using Heaps: Heap Sort
• However, it is also possible to build a heap in place using an array
• Since we know the relative position of each parent and its children in the array, we simply start with the first non-leaf node in the array, compare it to its children and swap if necessary
![Page 22: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/22.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-22
Using Heaps: Heap Sort
• We then work backward in the array until we reach the root
• Since at most, this will require us to make two comparisons for each non-leaf node, this approach is O(n) to build the heap
• We will revisit the analysis of HeapSort at the end of the chapter
![Page 23: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/23.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-23
Using Heaps: Priority Queue
• A priority queue is a collection that follows two ordering rules:– Items which have higher priority go first
– Items with the same priority use a first in, first out method to determine their ordering
• A priority queue could be implemented using a list of queues where each queue represents items of a given priority
![Page 24: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/24.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-24
Using Heaps: Priority Queue
• Another solution is to use a minheap
• Sorting the heap by priority accomplishes the first ordering
• However, the first in, first out ordering for items with the same priority has to be manipulated
![Page 25: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/25.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-25
Using Heaps: Priority Queue
• The solution is to create a PriorityQueueNode object that stores the element to be placed on the queue, the priority of the element and the arrival order of the element
• Then we simply define a compareTo method for the PriorityQueueNode class that first compares priority then arrival time
• The PriorityQueue class then extends the Heap class and stores PriorityQueueNodes
![Page 26: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/26.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-26
Listing 15.2
![Page 27: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/27.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-27
Listing 15.2 (cont.)
![Page 28: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/28.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-28
Listing 15.2 (cont.)
![Page 29: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/29.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-29
Listing 15.2 (cont.)
![Page 30: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/30.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-30
Listing 15.3
![Page 31: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/31.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-31
Listing 15.3 (cont.)
![Page 32: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/32.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-32
Listing 15.3 (cont.)
![Page 33: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/33.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-33
Implementing Heaps with Links
• A linked implementation of a minheap would simply be an extension of our LinkedBinaryTree class
• However, since we need each node to have a parent pointer, we will create a HeapNode class to extend our BinaryTreeNode class we used earlier
![Page 34: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/34.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-34
Listing 15.4
![Page 35: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/35.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-35
Implementing Heaps with Links
• The addElement method must accomplish three tasks:– Add the new node at the appropriate
location
– Reorder the heap
– Reset the lastNode pointer to point to the new last node
![Page 36: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/36.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-36
LinkedHeap - the addElement Operation
![Page 37: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/37.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-37
LinkedHeap - the addElement Operation
![Page 38: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/38.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-38
LinkedHeap - the addElement Operation
• The addElement operation makes use of two private methods:– getNextParentAdd that returns a
reference to the node that will be the parent of the new node
– heapifyAdd that reorders the heap after the insertion
![Page 39: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/39.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-39
LinkedHeap - the addElement Operation
![Page 40: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/40.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-40
LinkedHeap - the addElement Operation
![Page 41: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/41.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-41
LinkedHeap - the removeMin Operation
• The removeMin operation must accomplish three tasks:– Replace the element stored in the root
with the element stored in the last leaf
– Reorder the heap if necessary
– Return the original root element
![Page 42: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/42.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-42
LinkedHeap - the removeMin Operation
![Page 43: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/43.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-43
LinkedHeap - the removeMin Operation
![Page 44: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/44.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-44
LinkedHeap - the removeMin Operation
• Like the addElement operation, the removeMin operation makes use of two private methods– getNewLastNode that returns a reference
to the new last node in the heap
– heapifyRemove that reorders the heap after the removal
![Page 45: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/45.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-45
LinkedHeap - the removeMin Operation
![Page 46: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/46.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-46
LinkedHeap - the removeMin Operation
![Page 47: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/47.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-47
LinkedHeap - the removeMin Operation
![Page 48: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/48.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-48
Implementing Heaps with Arrays
• An array implementation of a heap may provide a simpler alternative
• In an array implementation, the location of parent and child can always be calculated
• Given that the root is in postion 0, then for any given node stored in position n of the array, its left child is in position 2n + 1 and its right child is in position 2(n+1)
• This means that its parent is in position (n-1)/2
![Page 49: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/49.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-49
Implementing Heaps with Arrays
• Like the linked version, the addElement operation for an array implementation of a heap must accomplish three tasks:– Add the new node,
– Reorder the heap,
– Increment the count by one
• The ArrayHeap version of this method only requires one private method, heapifyAdd which reorders the heap after the insertion
![Page 50: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/50.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-50
ArrayHeap - the addElement Operation
![Page 51: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/51.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-51
ArrayHeap - the addElement Operation
![Page 52: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/52.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-52
ArrayHeap - the removeMin Operation
• The removeMin operation must accomplish three tasks:– Replace the element stored at the root with the
element stored in the last leaf
– Reorder the heap as necessary
– Return the original root element
• Like the addElement operation, the removeMin operation makes use of a private method, heapifyRemove to reorder the heap
![Page 53: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/53.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-53
ArrayHeap - the removeMin Operation
![Page 54: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/54.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-54
ArrayHeap - the removeMin Operation
![Page 55: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/55.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-55
ArrayHeap - the removeMin Operation
![Page 56: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/56.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-56
Analysis of Heap Implementations
• The addElement operation is O(log n) for both implementations
• The removeMin operation is O(log n) for both implementations
• The findMin operation is O(1) for both implementations
![Page 57: Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-2 Chapter Objectives Define a heap abstract data structure Demonstrate](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cb95503460f94980f73/html5/thumbnails/57.jpg)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 15-57
Analysis of Heap Implementations
• One might conclude then that HeapSort is O(log n) since both adding and removing elements is O(log n)
• Keep in mind that for HeapSort, we must perform both operations for each of n elements
• Thus the time complexity of HeapSort is 2*n*log n or O(n log n)