Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either...

62
Chapter 11 Heap

Transcript of Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either...

Page 1: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

Chapter 11

Heap

Page 2: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

Overview

● The heap is a special type of binary tree.● It may be used either as a priority queue or as a

tool for sorting.

Page 3: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

Learning Objectives

● Learn how the heap can play the role of a priority queue.

● Describe the structure and ordering properties of the heap.

● Study the characteristic heap operations, and analyze their running time.

● Understand the public interface of a heap class.● Design a heap-based priority scheduler.

Page 4: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

Learning Objectives

● Develop a priority scheduling package in Java that uses the heap class.

● Implement the heap class using an array list as the storage component for the heap entries.

● Appreciate the software engineering issues that inform the design of an updatable heap.

Page 5: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.1 Heap as Priority Queue

● In a priority queue, the heap acts as a data structure in which the entries have different priorities of removal. The entry with the highest priority is the one that will

be removed next.● A FIFO queue may be considered a special

case of a priority queue, in which the priority of an entry is the time of its arrival in the queue, and the earlier the arrival time, the higher the priority.

Page 6: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.1 Heap as Priority Queue

● The emergency room in a hospital is a quintessential priority queue.

● Scheduling different processes in an operating system. Processes arrive at different points of time, and

take different amounts of time to finish executing. The operating system needs to ensure that all

processes get fair treatment in the amount of CPU time they are allocated. No single process should hog the CPU nor should any process starve for CPU time.

Page 7: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.1 Heap as Priority Queue

● A crucial difference between the ER example and the CPU scheduling application The priority of this patient must be increased

according to the severity of his or her condition. An ER-like situation is best represented by a priority

queue model in which the priority of an entry may change while it is in the queue.

● We will focus on heaps that do not allow for the priority of an existing entry to be changed.

Page 8: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.2 Heap Properties

Page 9: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.2 Heap Properties

● Remember, a complete binary tree is one in which every level but the last must have the maximum number of nodes possible at that level. The last level may have fewer than the maximum

possible nodes, but they should be arranged from left to right without any empty spots.

● A complete binary tree is called the heap structure property.

● The relative ordering among the keys is called the heap ordering property.

Page 10: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.2 Heap Properties

Page 11: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.2.1 Max and Min Heaps

● A max heap The maximum key is at the top of the heap.

Page 12: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.2.1 Max and Min Heaps

● A priority queue can be implemented either as a max heap or a min heap, according to whether a greater key indicates greater priority (max heap) or smaller priority (min heap).

Page 13: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3 Heap Operations

● The insert operation inserts a new entry in the heap.

● The delete operation that removes the entry at the front of the priority queue.

Page 14: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3.1 Insert

● Inserting a new key in a heap must ensure that after insertion, both the heap structure and the heap ordering properties are satisfied. Insert the new key so that the heap structure

property is satisfied, i.e. the new tree after insertion is also complete.

Make sure that the heap ordering property is satisfied by sifting up the newly inserted key.

Page 15: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3.1 Insert

Page 16: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3.1 Insert

● Sifting up

Page 17: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3.2 Delete

● Deletion removes the entry at the top of the heap.

● This leaves a vacant spot at the root, and the heap has to be restored.

Page 18: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3.2 Delete

● The key k that is extracted from the last node and written into the root is moved as far down as needed.

● The children of k are first compared with each other to determine the larger key. If k is smaller, it is exchanged with the larger key.

● This process continues until either the larger of the keys of k’s children is less than or equal to k, or k reaches a leaf node.

Page 19: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3.2 Delete

Page 20: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3.3 Running Time Alanysis

● Worst Case Assume that the last level contains the full

compliment of nodes. h = log( n + 1 ) - 1

Page 21: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.3.3 Running Time Analysis

● Insert Worst case: the new key may be sifted all the way

up to the root. O(log n)

● Delete Worst case: the key may be sifted all the way down

to a leaf node. O(log n)

Page 22: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.4 A Heap Class

Page 23: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.4 A Heap Class

Page 24: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.4 A Heap Class

● The heap accepts items of any type with the restriction that the type implements the compareTo method of the Comparable interface.

● first returns the top of the heap, and every subsequent call to next returns the item that would appear next in a level-order traversal going top to bottom.

Page 25: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.5 Priority Scheduling with Heap

● A processor and a queue of schedulable processes that are waiting for their turn at the processor.

● The processes are given relative priorities of execution so that the process with the highest priority in the process queue is the first one to be executed when the processor is free.

Page 26: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.5 Priority Scheduling with Heap

● The process with the highest priority in the heap is sent to the CPU for execution at time t.

Page 27: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.5.2 A Scheduling Package using Heap

Page 28: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.5.2 A Scheduling Package using Heap

● If two processors have the same priority value, the earlier arrival time gets higher priority.

Page 29: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.6 Sorting with the Heap Class

● Imagine the priority queue operates in two distinct phases. "outlet" is shut off (build phase)

● entries are allowed to come in but not allowed to exit. "inlet" is shut off (sort phase)

● no entry is allowed in, and all the resident entries are let out one by one based on priority.

● The entries have been sorted according to priority order.

Page 30: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.6.1 Example: Sorting Integers

Page 31: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.6.1 Example: Sorting Integers

● The build phase is a sequence of n calls to the add method of the Heap class.

● Adding the i-th element takes up to log i time in the worst case.

Page 32: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.6.1 Example: Sorting Integers

● Stirling's formula

Page 33: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.6.1 Example: Sorting Integers

● The sort phase is O(n log n)● O(n log n) + O(n log n) = O(n log n)

Page 34: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7 Heap Class Implementation

● The heap is a special type of binary tree. A complete one.

● The heap entries can be stored in an array.

Page 35: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.1 Array Storage

Page 36: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.1 Array Storage

● Determining the parent and the children of each node For a node of the binary tree at index k, its left and

right children are at indices 2k + 1 and 2k + 2, respectively.

The parent of a node at index k is at index (k – 1)/2.

Page 37: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.1 Array Storage

● For a node at index k, if 2k + 1 is beyond the upper limit of the array, the node is a leaf.

● If a node at index k has a child at 2k + 2, then there must be a child at 2k + 1.

● Finding a parent or a child is O(1).

Page 38: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.1 Array Storage

● It is possible to implement any binary tree as an array.

Page 39: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.1 Array Storage

Page 40: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.1 Array Storage

Page 41: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.2 Implementation using ArrayList

Page 42: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.2 Implementation using ArrayList

Page 43: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.2 Implementation using ArrayList

Page 44: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.2 Implementation using ArrayList

Page 45: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.2 Implementation using ArrayList

Page 46: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.7.2 Implementation using ArrayList

● What happens when a new item is added, and the arrayList is full to capacity? The array will be resized in order to accommodate

the new item. If the heap had n items including the new item, the

resizing is O(n). Resizing should happen very infrequently,

especially if the initial capacity is assigned carefully.

Page 47: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.1 Designing an Updatable Heap

● In order to update an entry, one would first have to locate it in the heap.

● If a heap had n entries, it would take up to O(n) time to find that entry.

Page 48: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.2 Handles to heap entries

● We need a direct pointer to that entry so we avoid searching.

● O(1) to find, and O(log n) to update.

Page 49: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.2 Handles to heap entries

● How is the client informed that the handle of 8 and 5 have changed?

Page 50: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.3 Shared handle array

● The heap maintains a separate handle array, and shares this array with the clients

Page 51: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.3 Shared handle array

Page 52: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.4 Encapsulating handles within heap

● Encapsulating the handle array within the heap insulates the client program space from the heap, while ensuring that handles are always fresh.

● When an item is added, the handle that is returned is an index into the handle array, instead of a location in the heap array list.

Page 53: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.4 Encapsulating handles within heap

● Whenever an item is added to the heap, it is assigned a new handle by growing the handle array.

Page 54: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.4 Encapsulating handles within heap

● The size of the handle array is equal to the number of items ever added to the heap.

● If n items are added to the heap but there are never more than k items in the heap at any time, the original heap (of the Heap class) would have required exactly k units of space.

● Our updatable heap requires n + k units of space.

Page 55: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.5 Recycling free handle space

● We must carefully recycle space in the handle array.

● When an item is deleted from the heap, it no longer needs space in the handle array. The client will never access this item again.

● This space can be marked available so when a new item is added to the heap, it may be reused as a handle to this new item.

Page 56: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.5 Recycling free handle space

● Since we cannot afford to spend time searching in the handle array for such marked, recyclable space, we need to have a separate data structure that will keep track of all free spaces.

● A simple data structure that will achieve this is a list or a stack.

Page 57: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.5 Recycling free handle space

● Whenever an item is to be added to the heap, the free-space stack is first checked to see if it is not empty.

● If so, the top entry of the stack is popped–this entry would contain the index of a free position in the handle array. The new item’s handle would be in this position.

● If the free space stack is empty, the new item’s handle would be added as a new handle entry at the end of the handle array.

Page 58: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.5 Recycling free handle space

● The new item itself is always added at the end of the heap array list.

● The client is passed back the index in the handle array where the new item’s handle has been stored.

Page 59: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.8.5 Recycling free handle space

● An updatable heap will support update of keys in O(log n) time, since every update results in either a sift up or a sift down.

● It would ensure that the amount of space used is O(m) where m is the maximum number of items ever in the heap.

Page 60: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.9 Summary

● A heap is a complete binary tree with the property that the value of the item at any node x is greater than or equal to the values of the items at all the nodes in the subtree rooted at x.

● The above defines a max heap. A min heap is defined symmetrically–the heap ordering property now requires that the value at a node be less than or equal to the values at the nodes in its subtree.

Page 61: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.9 Summary

● A heap may be used as a priority queue. It may also be used to sort a set of values.

● One of the important uses of a priority queue is in scheduling a set of activities in order of their priorities.

● The entries of a heap are stored in an array for maximum effectiveness in space usage.

● The (max) heap operations delete max and insert both take O(log n) time.

Page 62: Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.

11.9 Summary

● The heap data structure does not support a fast search operation.

● The updatable heap support update of keys in O(log n) time, and uses O(m) space, where m is the maximum number of items ever in the heap.