Professional Documents
Culture Documents
• MinHeap implementation
• Heap Applications:
• Heap Sort
• Heap as a priority queue
1
What is a Binary Heap?
• A binary heap is a complete binary tree with one (or both) of the following
heap order properties:
• MinHeap property: Each node must have a key that is less or equal to
the key of each of its children.
• MaxHeap property: Each node must have a key that is greater or equal
to the key of each of its children.
• A binary heap satisfying the MinHeap property is called a MinHeap.
• A binary heap satisfying the MaxHeap property is called a MaxHeap.
• A binary heap with all keys equal is both a MinHeap and a MaxHeap.
• Recall: A complete binary tree may have missing nodes only on the right
side of the lowest level.
2
MinHeap and non-MinHeap examples
21 16 21 16
24 31 19 68 6 31 19 68
21 16 21 16
24 31 19 68 24 31 19
65 26 32 65 26 32
Violates heap structural property Violates heap structural property
3
MaxHeap and non-MaxHeap examples
65 46 65 46
24 32 23 25 67 32 23 25
70
Not a Heap Not a Heap 30
50 40
21 16
24 31 19 38
19 18 10
15 20 25
2 5 15
Violates heap structural property Violates heap structural property
4
Array Representation of a Binary Heap
• A heap is a dynamic data structure that is represented and
manipulated more efficiently using an array.
• Since a heap is a complete binary tree, its node values can be
stored in an array, without any gaps, in a breadth-first order, where:
Value(node i+1) array[ i ], for i > 0
13
21 16
24 31 19 68
13 21 16 24 31 19 68 65 26 32
0 1 2 3 4 5 6 7 8 9
65 26 32
13
21 16
24 31 19 68
13 21 16 24 31 19 68 65 26 32
65 26 32
0 1 2 3 4 5 6 7 8 9 10
6
Array Representation of a Binary Heap (contd.)
7
MinHeap Implementation
• A binary heap can serve as a priority queue
• Our MinHeap class will implement the following
PriorityQueue interface:
8
MinHeap Implementation (contd.)
buildHeapBottomUp();
}
9
MinHeap enqueue
The pseudo code algorithm for enqueing a key in a MinHeap is:
enqueue(key){
if(the heap is full)
throw an exception ;
insert key at the end of the heap ;
while(key is not in the root node and key < parent(key))
swap(key , parent(key)) ;
}
insert( key){
heapSize = heapSize + 1;
i = heapSize;
A[i] = key; Complexity: O(log n)
while( i > 1 && A[i] < A[i/2] ){
temp = A[i/2];
A[i/2] = A[i];
A[i] = temp;
i = i/2;
}
}
The process of swapping an element with its parent, in order to restore the heap order property is
called percolate up, sift up, or reheapification upward.
13 13
21 16 Insert 18 21 16
24 31 19 68 24 31 19 68
65 26 32 65 26 32 18
Percolate up
13 13
18 16 21 16
Percolate up
24 21 19 68 24 18 19 68
65 26 32 31 65 26 32 31
11
MinHeap enqueue implementation
• To have better efficiency, we avoid repeated swapping
• We create a hole [i.e., an array location] at the end of the heap for the
new key, move the hole upward when needed, and at the end, put the
key into the hole
insert( key){
heapSize = heapSize + 1;
hole = heapSize;
A[hole] = key;
}
12
MinHeap Insertion via hole Example
13 To Insert 18 create a hole 13
at index (heapSize + 1)
21 16 21 16
24 31 19 68 24 31 19 68
65 26 32 65 26 32 18 < 31,
Percolate
hole up
13
13
18 16
21 16
24 21 19 68
24 19 68
13
65 26 32 31
65 26 32 31
16
14
MinHeap dequeue
• The pseudo code algorithm for deleting the root key in a MinHeap
is:
dequeueMin(){
if(Heap is empty) throw an exception ;
extract the key from the root ;
if(root is a leaf node){ delete root ; return; }
copy the key from the last leaf to the root ;
delete last leaf ;
currentNode = root ;
while(currentNode is not a leaf node
and key of curentNode > key of any of its children)
swap currentNode with the smaller child ;
return ;
}
24 21 23 68 24 21 23 68
65 26 32 31 65 26 32
2. Replace by value of last node Percolate down
18 18
Percolate down
21 19 31 19
24 31 23 68 24 21 23 68
65 26 32 65 26 32
16
MinHeap Dequeue via hole (Example)
13 1. Delete min element
24 21 23 68 24 21 23 68
65 26 32 31 65 26 32
31 > 18, Percolate
2. Store 31,value of last node, in a hole down
temp variable
18 18
21 19 19
24 31 23 68 18
24 21 23 68
65 26 32 21 19
65 26 32
18
Deleting an arbitrary key
The algorithm of deleting an arbitrary key from a heap is:
• Copy the key x of the last node to the node containing the deleted key.
• Delete the last node.
• Percolate x down until the heap property is restored.
Example:
19
Changing the priority of a key
There are three possibilities when the priority of a key x is changed:
1. The heap property is not violated.
2. The heap property is violated and x has to be percolated up to restore the heap property.
3. The heap property is violated and x has to be percolated down to restore the heap property.
Example:
20
Building a heap (top down)
• A heap is built top-down by inserting one key at a time in an initially empty heap.
• After each key insertion, if the heap property is violated, it is restored by
percolating the inserted key upward.
The algorithm is:
for(int i=1; i <= heapSize; i++){ Complexity: O(n log n)
read key;
binaryHeap.enqueue(key);
}
Example: Insert the keys 4, 6, 10, 20, and 8 in this order in an originally empty max-heap
21
Converting an array into a Binary heap (top-down)
22
Converting an array into a MinHeap (top down) (Example)
70 29 68 65 32 19 16 13 26 31
70 29 29
29 68 70 68 65 68
65 32 19 16 65 32 19 16 70 32 19 16
13 26 31 13 26 31 13 26 31
16 19 29
32 19 32 29 32 68
70 65 68 29 70 65 68 16 70 65 19 16
13 26 31 13 26 31 13 26 31
13 13
13
16 19 16 19
16 19
26 65 68 29 26 31 68 29
32 65 68 29
70 32 31 70 32 65
70 26 31
13 16 19 26 31 68 29 70 32 65
23
Converting an array into a Binary heap (bottom-up)
13 70 70
26 16 13 16 29 16
29 31 19 68 26 31 19 68 13 31 19 68
65 70 32 65 29 32 65 26 32
13 26 16 29 31 19 68 65 70 32
25
Heap Application: Heap Sort
• A MinHeap or a MaxHeap can be used to implement an efficient
sorting algorithm called Heap Sort.
• The following algorithm uses a MinHeap:
26
Heap Sort with no extra storage
• Observation: after each dequeueMin, the size of heap shrinks by 1
• We can use the last cell just freed up to store the element that was just
deleted
• after the last dequeueMin, the array will contain the elements in
decreasing sorted order
• To sort the elements in the decreasing order, use a min heap
• To sort the elements in the increasing order, use a max heap
Example (Adopted from another course): Max heap after the buildHeap phase for
the input sequence 59,36,58,21,41,97,31,16,26,53
27
Heap Sort with no extra storage
Heap after the first deleteMax operation:
28
Heap Sort with no extra storage
Heap after the second deleteMax operation:
29
Heap Applications: Priority Queue
• A heap can be used as the underlying implementation of a priority queue.
• A priority queue is a data structure in which the items to be inserted have associated
priorities.
• Items are withdrawn from a priority queue in order of their priorities, starting with the highest
priority item first.
• Minimum priority queues treat low-valued keys as high-priority; maximum priority queues
prioritizes high-valued keys
• Priority queues are often used in resource management, simulations, and in the
implementation of some algorithms (e.g., some graph algorithms, some backtracking
algorithms).
• Several data structures can be used to implement priority queues. Below is a comparison of
some: