0% found this document useful (0 votes)
23 views44 pages

DSA Ch9 Heaps

Uploaded by

Stanly Mark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views44 pages

DSA Ch9 Heaps

Uploaded by

Stanly Mark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Heaps

Dr. Rang Nguyen

Chapter 9
Heap Definition
Heaps Heap Structure

Basic Heap
Data Structures and Algorithms Algorithms
ReheapUp
ReheapDown

Heap Data
Structure

Dr. Rang Nguyen Heap Algorithms


ReheapUp
Faculty of Computer Science and Engineering ReheapDown
Build a Heap
University of Technology, VNU-HCM Insert a Node
Delete a Node

Heap Applications
Selection Algorithms
Priority Queues

9.1
Heaps
Outcomes
Dr. Rang Nguyen

• L.O.4.1 - List some applications of Heap.


Heap Definition
• L.O.4.2 - Depict heap structure and relate it to array. Heap Structure

• L.O.4.3 - List necessary methods supplied for heap Basic Heap


Algorithms
structure, and describe them using pseudocode. ReheapUp
ReheapDown

• L.O.4.4 - Depict the working steps of methods that Heap Data


Structure
maintain the characteristics of heap structure for the
Heap Algorithms
cases of adding/removing elements to/from heap. ReheapUp
ReheapDown
Build a Heap
Insert a Node
Delete a Node

Heap Applications
Selection Algorithms
Priority Queues

9.2
Heaps
Outcomes
Dr. Rang Nguyen

• L.O.4.5 - Implement heap using C/C++.


• L.O.4.6 - Analyze the complexity and develop
experiment (program) to evaluate methods supplied for Heap Definition

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, searching, and graphs. Heap Data


Structure
• L.O.1.2 - Analyze algorithms and use Big-O notation to Heap Algorithms
characterize the computational complexity of algorithms ReheapUp
ReheapDown

composed by using the following control structures: Build a Heap


Insert a Node

sequence, branching, and iteration (not recursion). Delete a Node

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

ReheapUp Heap Data


Structure
ReheapDown
Heap Algorithms
Build a Heap ReheapUp

Insert a Node ReheapDown


Build a Heap

Delete a Node Insert a Node


Delete a Node

6 Heap Applications Heap Applications


Selection Algorithms
Selection Algorithms Priority Queues

Priority Queues
9.4
Heaps

Dr. Rang Nguyen

Heap Definition

Heap Structure

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

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

the key value in each of its descendents. 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.6
Heaps
Heap Definition
Dr. Rang Nguyen
Definition
A min-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 less than or equal to the Heap Definition

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

(Source: Data Structures - A Pseudocode Approach with C++)


9.7
Heaps

Dr. Rang Nguyen

Heap Definition

Heap Structure

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.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

(Source: Data Structures - A Pseudocode Approach with C++)

9.10
Heaps

Dr. Rang Nguyen

Heap Definition

Heap Structure

Basic Heap Algorithms 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.11
Heaps
ReheapUp
Dr. Rang Nguyen

The reheapUp operation repairs a "broken" heap by floating


the last element up the tree until it is in its correct location
in the heap.
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.12
Heaps
ReheapDown
Dr. Rang Nguyen

The reheapDown operation repairs a "broken" heap by


pushing the root down the tree until it is in its correct
location in the heap.
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.13
Heaps

Dr. Rang Nguyen

Heap Definition

Heap Structure

Heap Data 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.14
Heaps
Properties of Heaps
Dr. Rang Nguyen

• A complete or nearly complete binary tree.


• If the height is h, the number of nodes N Heap Definition

is between 2h−1 and 2h − 1. Heap Structure

Basic Heap

• Complete tree: N = 2h − 1 when last level Algorithms


ReheapUp
ReheapDown

is full. Heap Data


Structure
• Nearly complete: All nodes in the last level Heap Algorithms
ReheapUp

are on the left. ReheapDown


Build a Heap
Insert a Node

→ Heap can be represented in an array. Delete a Node

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

The relationship between a node and its children is fixed and


can be calculated:
1 For a node located at index i, its children are found at
• Left child: 2i + 1
• Right child: 2i + 2 Heap Definition

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

the first leaf is bN/2c. Delete 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

Dr. Rang Nguyen

Heap Definition

Heap Structure

Heap Algorithms 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.18
Heaps
ReheapUp Algorithm
Dr. Rang Nguyen

Algorithm reheapUp(ref heap <array>,


val position <integer>)
Reestablishes heap by moving data in Heap Definition

position up to its correct location. Heap Structure

Basic Heap
Algorithms

Pre: All data in the heap above this ReheapUp


ReheapDown

position satisfy key value order of a Heap Data


Structure

heap, except the data in position Heap Algorithms


ReheapUp
ReheapDown
Build a Heap

Post: Data in position has been moved Insert a Node


Delete a Node

up to its correct location. Heap Applications


Selection Algorithms
Priority Queues

9.19
Heaps
ReheapUp Algorithm
Dr. Rang Nguyen

if position > 0 then


parent = (position-1)/2
if heap[position].key > heap[parent].key Heap Definition

Heap Structure
then Basic Heap
swap(position, parent) Algorithms
ReheapUp

reheapUp(heap, parent) ReheapDown

Heap Data
end Structure

Heap Algorithms
end ReheapUp
ReheapDown

return Build a Heap


Insert a Node
Delete a Node

End reheapUp Heap Applications


Selection Algorithms
Priority Queues

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

satisfy key value order of a heap, except Heap Data


Structure

the data in position Heap Algorithms


ReheapUp

lastPosition is an index to the last ReheapDown


Build a Heap

element in heap Insert a Node


Delete a Node

Heap Applications
Selection Algorithms

Post: Data in position has been moved Priority Queues

down to its correct location. 9.21


Heaps
ReheapDown Algorithm
leftChild = position * 2 + 1 Dr. Rang Nguyen

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

largeChild = leftChild ReheapUp


ReheapDown

end Heap Data


Structure
if heap[largeChild].key > heap[position].key
Heap Algorithms
then ReheapUp

swap(largeChild, position) ReheapDown


Build a Heap

reheapDown(heap, largeChild, lastPosition) Insert a Node


Delete a Node

end Heap Applications


Selection Algorithms
end Priority Queues

return
End reheapDown 9.22
Heaps
Build a Heap
Dr. Rang Nguyen

• Given a filled array of elements in random order, to


build the heap we need to rearrange the data so that
each node in the heap is greater than its children.

• 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

second parts. ReheapUp


ReheapDown

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

• Each iteration of the insertion algorithm uses reheap up Delete 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

(Source: Data Structures - A Pseudocode Approach with C++)


9.24
Heaps
Build a Heap
Dr. Rang Nguyen
Algorithm buildHeap(ref heap <array>, val size
<integer>)
Given an array, rearrange data so that they form a
heap.
Heap Definition
Pre: heap is array containing data in nonheap order Heap Structure
size is number of elements in array Basic Heap
Algorithms
ReheapUp
Post: array is now a heap. ReheapDown

Heap Data
Structure
walker = 1 Heap Algorithms

while walker < size do ReheapUp


ReheapDown
Build a Heap

reheapUp(heap, walker) Insert a Node


Delete a Node

walker = walker + 1 Heap Applications

end Selection Algorithms


Priority Queues

End buildHeap
9.25
Heaps
Insert a Node into a Heap
Dr. Rang Nguyen

• To insert a node, we need to locate the


first empty leaf in the array. Heap Definition

Heap Structure

Basic Heap
• We find it immediately after the last node Algorithms
ReheapUp

in the tree, which is given as a parameter. ReheapDown

Heap Data
Structure

• To insert a node, we move the new data to Heap Algorithms


ReheapUp
ReheapDown

the first empty leaf and reheap up. Build a Heap


Insert a Node
Delete a Node

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

(Source: Data Structures - A Pseudocode Approach with C++)


9.27
Heaps
Insert a Node into a Heap
Dr. Rang Nguyen

Algorithm insertHeap(ref heap <array>,


ref last <integer>, val data <dataType>)
Inserts data into heap.
Heap Definition

Pre: heap is a valid heap structure Heap Structure

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

Return true if successful; false if array Heap Applications


Selection Algorithms
Priority Queues

full
9.28
Heaps
Insert a Node into a Heap
Dr. Rang Nguyen

if heap full then


return false Heap Definition

end Heap Structure

Basic Heap
last = last + 1 Algorithms
ReheapUp

heap[last] = data ReheapDown

Heap Data
reheapUp(heap, last) Structure

Heap Algorithms
return true ReheapUp
ReheapDown

End insertHeap Build a Heap


Insert a Node
Delete a Node

Heap Applications
Selection Algorithms
Priority Queues

9.29
Heaps
Delete a Node from a Heap
Dr. Rang Nguyen

• When deleting a node from a heap, the


most common and meaningful logic is to
Heap Definition
delete the root. Heap Structure

Basic Heap

• After it has been deleted, the heap is thus Algorithms


ReheapUp
ReheapDown

left without a root. Heap Data


Structure

Heap Algorithms
• To reestablish the heap, we move the data ReheapUp
ReheapDown

in the last heap node to the root and Build a Heap


Insert a Node
Delete a Node

reheap down. Heap Applications


Selection Algorithms
Priority Queues

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

(Source: Data Structures - A Pseudocode Approach with C++) 9.31


Heaps
Delete a Node from a Heap
Algorithm deleteHeap(ref heap <array>, Dr. Rang Nguyen

ref last <integer>, ref dataOut


<dataType>)
Deletes root of heap and passes data back
Heap Definition
to caller. Heap Structure

Basic Heap
Algorithms
Pre: heap is a valid heap structure ReheapUp
ReheapDown

last is reference parameter to last node Heap Data


Structure
dataOut is reference parameter for output Heap Algorithms
ReheapUp

data ReheapDown
Build a Heap
Insert a Node
Delete a Node

Post: root deleted and heap rebuilt Heap Applications


Selection Algorithms

root data placed in dataOut Priority Queues

9.32
Heaps
Delete a Node from a Heap
Dr. Rang Nguyen

if heap empty then


return false
Heap Definition
end Heap Structure

dataOut = heap[0] Basic Heap


Algorithms
heap[0] = heap[last] ReheapUp
ReheapDown

last = last - 1 Heap Data


Structure

reheapDown(heap, 0, last) Heap Algorithms


ReheapUp

return true ReheapDown


Build a Heap

End deleteHeap Insert a Node


Delete a Node

Heap Applications
Selection Algorithms
Priority Queues

9.33
Heaps
Complexity of Binary Heap Operations
Dr. Rang Nguyen

• ReheapUp: O(log2 n)
Heap Definition

• ReheapDown: O(log2 n) Heap Structure

Basic Heap
Algorithms

• Build a Heap: O(n log2 n) ReheapUp


ReheapDown

Heap Data
Structure
• Insert a Node into a Heap: O(log2 n) Heap Algorithms
ReheapUp
ReheapDown

• Delete a Node from a Heap: O(log2 n) Build a Heap


Insert a Node
Delete a Node

Heap Applications
Selection Algorithms
Priority Queues

9.34
Heaps

Dr. Rang Nguyen

Heap Definition

Heap Structure

Heap Applications 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.35
Heaps
Heap Applications
Dr. Rang Nguyen

Three common applications of heaps are:


Heap Definition
1 selection algorithms,
Heap Structure

2 priority queues, Basic Heap


Algorithms
ReheapUp

3 and sorting. ReheapDown

Heap Data
Structure

We discuss heap sorting in Chapter 10 and Heap Algorithms


ReheapUp

selection algorithms and priority queues here. ReheapDown


Build a Heap
Insert a Node
Delete a Node

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

complexity of a simple sorting algorithm is O(n2 ). Heap Structure

Basic Heap
Algorithms
2 Create a heap and delete k − 1 elements from the heap, ReheapUp

leaving the desired element at the top. The complexity ReheapDown

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

(Source: Data Structures - A Pseudocode Approach with C++)


9.38
Heaps
Selection Algorithms
Dr. Rang Nguyen

Algorithm selectK(ref heap <array>, ref k


<integer>, ref last <integer>)
Select the k-th largest element from a list. Heap Definition

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

Post: k-th largest value returned Delete a Node

Heap Applications
Selection Algorithms
Priority Queues

9.39
Heaps
Selection Algorithms
Dr. Rang Nguyen

if k > last + 1 then


return 0
end Heap Definition
i=1 Heap Structure

originalSize = last + 1 Basic Heap


Algorithms

while i < k do ReheapUp


ReheapDown

temp = heap[0] Heap Data


Structure

deleteHeap(heap, last, dataOut) Heap Algorithms


ReheapUp

heap[last + 1] = temp ReheapDown


Build a Heap
Insert a Node

i=i+1 Delete a Node

end Heap Applications


Selection Algorithms
Priority Queues

9.40
Heaps
Selection Algorithms
Dr. Rang Nguyen

// Desired element is now at top of heap


holdOut = heap[0]
Heap Definition

// Reconstruct heap Heap Structure

Basic Heap
while last < originalSize do Algorithms
ReheapUp

last = last + 1 ReheapDown

Heap Data
reheapUp(heap, last) Structure

Heap Algorithms
end ReheapUp
ReheapDown

return holdOut Build a Heap


Insert a Node

End selectK Delete a Node

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

(Source: Data Structures - A Pseudocode Approach with C++)

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

You might also like