Title: Heap
1Chapter 11
2Overview
- The heap is a special type of binary tree.
- It may be used either as a priority queue or as a
tool for sorting.
3Learning 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.
4Learning 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.
511.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.
611.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.
711.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.
811.2 Heap Properties
911.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.
1011.2 Heap Properties
1111.2.1 Max and Min Heaps
- A max heap
- The maximum key is at the top of the heap.
1211.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).
1311.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.
1411.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.
1511.3.1 Insert
1611.3.1 Insert
1711.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.
1811.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 ks children is less than or equal to
k, or k reaches a leaf node.
1911.3.2 Delete
2011.3.3 Running Time Alanysis
- Worst Case
- Assume that the last level contains the full
compliment of nodes. - h log( n 1 ) - 1
2111.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)
2211.4 A Heap Class
2311.4 A Heap Class
2411.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.
2511.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.
2611.5 Priority Scheduling with Heap
- The process with the highest priority in the heap
is sent to the CPU for execution at time t.
2711.5.2 A Scheduling Package using Heap
2811.5.2 A Scheduling Package using Heap
- If two processors have the same priority value,
the earlier arrival time gets higher priority.
2911.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.
3011.6.1 Example Sorting Integers
3111.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.
3211.6.1 Example Sorting Integers
3311.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)
3411.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.
3511.7.1 Array Storage
3611.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.
3711.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).
3811.7.1 Array Storage
- It is possible to implement any binary tree as
an array.
3911.7.1 Array Storage
4011.7.1 Array Storage
4111.7.2 Implementation using ArrayList
4211.7.2 Implementation using ArrayList
4311.7.2 Implementation using ArrayList
4411.7.2 Implementation using ArrayList
4511.7.2 Implementation using ArrayList
4611.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.
4711.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.
4811.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.
4911.8.2 Handles to heap entries
- How is the client informed that the handle of 8
and 5 have changed?
5011.8.3 Shared handle array
- The heap maintains a separate handle array, and
shares this array with the clients
5111.8.3 Shared handle array
5211.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.
5311.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.
5411.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.
5511.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.
5611.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.
5711.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 poppedthis
entry would contain the index of a free position
in the handle array. - The new items handle would be in this position.
- If the free space stack is empty, the new items
handle would be added as a new handle entry at
the end of the handle array.
5811.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 items handle has been stored.
5911.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.
6011.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 symmetricallythe 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.
6111.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.
6211.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.