DSA Ch9 Heaps
DSA Ch9 Heaps
Chapter 9
Heap Definition
Heaps Heap Structure
Basic Heap
Data Structures and Algorithms Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Applications
Selection Algorithms
Priority Queues
9.1
Heaps
Outcomes
Dr. Rang Nguyen
Heap Applications
Selection Algorithms
Priority Queues
9.2
Heaps
Outcomes
Dr. Rang Nguyen
Heap Structure
heap structures.
Basic Heap
• L.O.8.4 - Develop recursive implementations for Algorithms
ReheapUp
methods supplied for the following structures: list, tree, ReheapDown
Heap Applications
Selection Algorithms
Priority Queues
9.3
Heaps
Contents
Dr. Rang Nguyen
1 Heap Definition
2 Heap Structure
3 Basic Heap Algorithms
ReheapUp
Heap Definition
ReheapDown
Heap Structure
4 Heap Data Structure Basic Heap
Algorithms
5 Heap Algorithms ReheapUp
ReheapDown
Priority Queues
9.4
Heaps
Heap Definition
Heap Structure
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.5
Heaps
Heap Definition
Dr. Rang Nguyen
Definition
A heap (max-heap) is a binary tree structure with the
following properties:
1 The tree is complete or nearly complete.
2 The key value of each node is greater than or equal to Heap Definition
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
Heap Structure
key value in each of its descendents.
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
Heap Definition
Heap Structure
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.8
Heaps
Heap trees
Dr. Rang Nguyen
Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.9
Heaps
Invalid Heaps
Dr. Rang Nguyen
Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.10
Heaps
Heap Definition
Heap Structure
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.11
Heaps
ReheapUp
Dr. Rang Nguyen
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.12
Heaps
ReheapDown
Dr. Rang Nguyen
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.13
Heaps
Heap Definition
Heap Structure
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.14
Heaps
Properties of Heaps
Dr. Rang Nguyen
Basic Heap
Heap Applications
Selection Algorithms
Priority Queues
9.15
Heaps
Heap in arrays
Dr. Rang Nguyen
Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
(Source: Data Structures - A Pseudocode Approach with C++)
9.16
Heaps
Heap Data Structure
Dr. Rang Nguyen
Heap Structure
2 The parent of a node located at index i is located at
Basic Heap
b(i − 1)/2c. Algorithms
ReheapUp
3 Given the index for a left child, j, its right sibling, if ReheapDown
Heap Data
any, is found at j + 1. Conversely, given the index for a Structure
right child, k, its left sibling, which must exist, is found Heap Algorithms
at k − 1. ReheapUp
ReheapDown
Build a Heap
4 Given the size, N , of a complete heap, the location of Insert a Node
Heap Applications
5 Given the location of the first leaf element, the location Selection Algorithms
Priority Queues
of the last nonleaf element is 1 less.
9.17
Heaps
Heap Definition
Heap Structure
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.18
Heaps
ReheapUp Algorithm
Dr. Rang Nguyen
Basic Heap
Algorithms
9.19
Heaps
ReheapUp Algorithm
Dr. Rang Nguyen
Heap Structure
then Basic Heap
swap(position, parent) Algorithms
ReheapUp
Heap Data
end Structure
Heap Algorithms
end ReheapUp
ReheapDown
9.20
Heaps
ReheapDown Algorithm
Dr. Rang Nguyen
Algorithm reheapDown(ref heap <array>,
val position <integer>, val lastPosition
<integer>)
Reestablishes heap by moving data in Heap Definition
position down to its correct location. Heap Structure
Basic Heap
Algorithms
Pre: All data in the subtree of position ReheapUp
ReheapDown
Heap Applications
Selection Algorithms
rightChild = position * 2 + 2
if leftChild <= lastPosition then
if (rightChild <= lastPosition) AND
(heap[rightChild].key > heap[leftChild].key
Heap Definition
then
Heap Structure
largeChild = rightChild
Basic Heap
else Algorithms
return
End reheapDown 9.22
Heaps
Build a Heap
Dr. Rang Nguyen
• We begin by dividing the array into two parts, the left Heap Definition
Heap Structure
being a heap and the right being data to be inserted
Basic Heap
into the heap. Note the "wall" between the first and Algorithms
Heap Data
• At the beginning the root (the first node) is the only Structure
Heap Algorithms
node in the heap and the rest of the array are data to ReheapUp
be inserted. ReheapDown
Build a Heap
Insert a Node
Heap Applications
to insert the next element into the heap and moves the Selection Algorithms
wall separating the elements one position to the right. Priority Queues
9.23
Heaps
Build a Heap
Dr. Rang Nguyen
Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
Heap Data
Structure
walker = 1 Heap Algorithms
End buildHeap
9.25
Heaps
Insert a Node into a Heap
Dr. Rang Nguyen
Heap Structure
Basic Heap
• We find it immediately after the last node Algorithms
ReheapUp
Heap Data
Structure
Heap Applications
Selection Algorithms
Priority Queues
9.26
Heaps
Insert a Node into a Heap
Dr. Rang Nguyen
Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
Basic Heap
last is reference parameter to last node in Algorithms
ReheapUp
heap ReheapDown
Heap Data
data contains data to be inserted Structure
Heap Algorithms
ReheapUp
ReheapDown
Post: data have been inserted into heap. Build a Heap
Insert a Node
Delete a Node
full
9.28
Heaps
Insert a Node into a Heap
Dr. Rang Nguyen
Basic Heap
last = last + 1 Algorithms
ReheapUp
Heap Data
reheapUp(heap, last) Structure
Heap Algorithms
return true ReheapUp
ReheapDown
Heap Applications
Selection Algorithms
Priority Queues
9.29
Heaps
Delete a Node from a Heap
Dr. Rang Nguyen
Basic Heap
Heap Algorithms
• To reestablish the heap, we move the data ReheapUp
ReheapDown
9.30
Heaps
Delete a Node from a Heap
Dr. Rang Nguyen
Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
Basic Heap
Algorithms
Pre: heap is a valid heap structure ReheapUp
ReheapDown
data ReheapDown
Build a Heap
Insert a Node
Delete a Node
9.32
Heaps
Delete a Node from a Heap
Dr. Rang Nguyen
Heap Applications
Selection Algorithms
Priority Queues
9.33
Heaps
Complexity of Binary Heap Operations
Dr. Rang Nguyen
• ReheapUp: O(log2 n)
Heap Definition
Basic Heap
Algorithms
Heap Data
Structure
• Insert a Node into a Heap: O(log2 n) Heap Algorithms
ReheapUp
ReheapDown
Heap Applications
Selection Algorithms
Priority Queues
9.34
Heaps
Heap Definition
Heap Structure
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.35
Heaps
Heap Applications
Dr. Rang Nguyen
Heap Data
Structure
Heap Applications
Selection Algorithms
Priority Queues
9.36
Heaps
Selection Algorithms
Dr. Rang Nguyen
Problem
Determining the k th element in an unsorted list.
Two solutions:
1 Sort the list and select the element at location k. The Heap Definition
Basic Heap
Algorithms
2 Create a heap and delete k − 1 elements from the heap, ReheapUp
Heap Data
is O(n log2 n). Structure
Heap Algorithms
ReheapUp
ReheapDown
Rather than simply discarding the elements at the top of the Build a Heap
Insert a Node
heap, a better solution would be to place the deleted element Delete a Node
at the end of the heap and reduce the heap size by 1. Heap Applications
Selection Algorithms
Priority Queues
After the k th
element has been processed, the temporarily
removed elements can then be inserted into the heap. 9.37
Heaps
Selection Algorithms
Dr. Rang Nguyen
Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
Heap Structure
Basic Heap
Pre: heap is an array implementation of a Algorithms
ReheapUp
heap ReheapDown
Heap Data
k is the ordinal of the element desired Structure
Heap Algorithms
last is reference parameter to last element ReheapUp
ReheapDown
Build a Heap
Insert a Node
Heap Applications
Selection Algorithms
Priority Queues
9.39
Heaps
Selection Algorithms
Dr. Rang Nguyen
9.40
Heaps
Selection Algorithms
Dr. Rang Nguyen
Basic Heap
while last < originalSize do Algorithms
ReheapUp
Heap Data
reheapUp(heap, last) Structure
Heap Algorithms
end ReheapUp
ReheapDown
Heap Applications
Selection Algorithms
Priority Queues
9.41
Heaps
Priority Queues
Dr. Rang Nguyen
The heap is an excellent structure to use for a priority queue.
Example
Assume that we have a priority queue with three priorities:
high (3), medium (2), and low (1).
Heap Definition
Of the first five customers who arrive, the second and the
Heap Structure
fifth are high-priority customers, the third is medium priority,
Basic Heap
and the first and the fourth are low priority. Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
9.42
Heaps
Priority Queues
Dr. Rang Nguyen
The customers are served according to their priority and
within equal priorities, according to their arrival. Thus we see
that customer 2 (3998) is served first, followed by customer
5 (3995), customer 3 (2997), customer 1 (1999), and
customer 4 (1996). Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
Heap Applications
Selection Algorithms
Priority Queues
(Source: Data Structures - A Pseudocode Approach with C++)
9.43
Heaps
Priority Queues
Dr. Rang Nguyen
Heap Definition
Heap Structure
Basic Heap
Algorithms
ReheapUp
ReheapDown
Heap Data
Structure
Heap Algorithms
ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node
(Source: Data Structures - A Pseudocode Approach with C++)
Heap Applications
Selection Algorithms
Priority Queues
9.44