Lec 15 Heap
Lec 15 Heap
Algorithms
(CEN3016)
Dr. Muhammad Umair Khan
Assistant Professor
Department of Computer Engineering
National University of Technology
Heap
• A heap is a type of data structure. One of the interesting things about
heaps is that they allow you to find the largest element in the heap in
O(1) time. (Recall that in certain other data structures, like arrays, this
operation takes O(n) time.)
• Furthermore, extracting the largest element from the heap (i.e. finding
and removing it) takes O(log n) time.
• These properties make heaps very useful for implementing a “priority
queue," which we’ll get to later.
• They also give rise to an O(n log n) sorting algorithm, “heapsort," which
works by repeatedly extracting the largest element until we have
emptied the heap.
Two Special Heaps
A heap is a certain kind of complete binary tree.
►A heap is a kind of tree that offers both insertion and deletion in
O(log2n) time.
►Fast for insertions; not so fast for deletions.
• Max-Heap
• Min-Heap
Max-Heap
When a complete
binary tree is built,
its first node must be
the root.
Heaps
Almost Complete
binary tree.
Almost Complete
binary tree.
Almost Complete
binary tree.
Almost Complete
binary tree.
Almost Complete
binary tree.
Heaps
45
A heap is a certain
kind of complete
binary tree. 35 23
27 21 22 4
19
Each node in a heap
contains a key that
can be compared to
other nodes' keys.
Heaps
45
A heap is a certain
kind of complete
binary tree. 35 23
27 21 22 4
19
The "heap property"
requires that each
node's key is >= the
keys of its children
A Data Structure Heap
• A heap is a nearly complete binary tree which can be easily
implemented on an array.
1
6
2 3
5 3 6 5 3 2 4 1
4 5 6
2 4 1
Nearly complete binary tree
• Every level except bottom is complete.
• On the bottom, nodes are placed as left as possible.
1
1
6
6
2 3
2 3
5 3
5 3
4 5 6
4 5 6
2 4 1
2 1 4
No! Yes!
Adding a Node to a Heap
45
Put the new node in the
next available spot.
35 23
Push the new node
upward, swapping with
its parent until the new 27 21 22 4
node reaches an
acceptable location.
19 42
Adding a Node to a Heap
45
Put the new node in the
next available spot.
35 23
Push the new node
upward, swapping with
its parent until the new 42 21 22 4
node reaches an
acceptable location.
19 27
Adding a Node to a Heap
45
Put the new node in the
next available spot.
42 23
Push the new node
upward, swapping with
its parent until the new 35 21 22 4
node reaches an
acceptable location.
19 27
Adding a Node to a Heap
45
The parent has a key
that is >= new node, or
42 23
The node reaches the
root.
The process of pushing 35 21 22 4
the new node upward
is called
reheapification 19 27
upward.
Removing the Top of a Heap
45
Move the last node onto
the root.
42 23
35 21 22 4
19 27
Removing the Top of a Heap
27
Move the last node onto
the root.
42 23
35 21 22 4
19
Removing the Top of a Heap
27
Move the last node onto
the root.
42 23
Push the out-of-place
node downward,
swapping with its larger 35 21 22 4
child until the new node
reaches an acceptable
location. 19
Removing the Top of a Heap
42
Move the last node onto
the root.
27 23
Push the out-of-place
node downward,
swapping with its larger 35 21 22 4
child until the new node
reaches an acceptable
location. 19
Removing the Top of a Heap
42
Move the last node onto
the root.
35 23
Push the out-of-place
node downward,
swapping with its larger 27 21 22 4
child until the new node
reaches an acceptable
location. 19
Removing the Top of a Heap
42
The children all have
keys <= the out-of-place
node, or 35 23
The node reaches the
leaf. 27 21 22 4
The process of pushing
the new node
downward is called 19
reheapification
downward.
Implementing a Heap
42
We will store the data
from the nodes in a
partially-filled array. 35 23
27 21
An array of data
Implementing a Heap
42
• Data from the root goes
in the first
location of the 35 23
array.
27 21
42
An array of data
Implementing a Heap
42
• Data from the next row
goes in the next two
array locations. 35 23
27 21
42 35 23
An array of data
Implementing a Heap
42
• Data from the next row
goes in the next two
array locations. 35 23
27 21
42 35 23 27 21
An array of data
Implementing a Heap
42
• Data from the next row
goes in the next two
array locations. 35 23
27 21
42 35 23 27 21
An array of data
We don't care what's in
this part of the array.
Important Points about the
Implementation
42
• The links between the tree's
nodes are not actually stored as
pointers, or in any other way. 35 23
• The only way we "know" that
"the array is a tree" is from the 27 21
way we manipulate the data.
42 35 23 27 21
An array of data
Important Points about the
Implementation
42
• If you know the index of a node,
then it is easy to figure out the
indexes of that node's parent 35 23
and children. Formulas are given
27 21
Programming representation: Logical representation:
Parent: (i-1)/2 Parent: (i)/2
Left: 2*i + 1 42 35 23 27 21 Left: 2*i
Right: 2*i + 2 [1] [2] [3] [4] [5] Right: 2*i + 1
Array-based representation of
binary trees (cont.)
• Full or complete trees can be implemented easily using an array-
based representation (elements occupy contiguous array slots)
• "Dummy nodes" are required for trees which are not full or
complete
Summary
void display(){
for(int i=0 ; i<heap_size ; i++){
cout<<" "<<harr[i]<<" ,";
}
cout<<"\b \b";
}
Implementation in C++
int main() {
insertKey_max(3);
/*insertKey_min(3);
insertKey_max(2);
insertKey_min(1);
insertKey_max(1);
insertKey_min(2);
insertKey_max(15);
insertKey_min(15);
insertKey_max(5);
insertKey_min(5);*/
cout<<"Inserted!!";
Implementation in C++
display();
cout<<endl; display();
int temp = delete_key(); return 0;
if(temp == 0) }
{
cout<<"\nHeap is Empty!!"<<endl;
}else{
cout<<temp<<" Deleted!!"<<endl;
}
Max-Heapify
• Max-Heapify(A,i) is a subroutine.
• When it is called, two subtrees rooted at Left(i) and Right(i) are max-
heaps, but A[i] may not satisfy the max-heap property.
• Max-Heapify(A,i) makes the subtree rooted at A[i] become a max-
heap by letting A[i] “float down”.
Building a Max-Heap
1 3
2 16 9 10
14 8 7
Adding a Node to a Heap
4
1 3
2 16 9 10
14 8 7
Adding a Node to a Heap
4
1 3
16 9 10
14
2 8 7
Adding a Node to a Heap
4
1 3
14 16 9 10
2 8 7
Adding a Node to a Heap
4
1 10
14 16 9 3
2 8 7
Adding a Node to a Heap
4
1 10
14 16 9 3
2 8 7
Adding a Node to a Heap
4
16 10
14 1 9 3
2 8 7
Adding a Node to a Heap
4
16 10
14 7 9 3
2 8 1
Adding a Node to a Heap
16
4 10
14 7 9 3
2 8 1
Adding a Node to a Heap
16
14 10
4 7 9 3
2 8 1
Adding a Node to a Heap
16
14 10
7 9 3
8
2 4 1
Heapsort
Heapsort ( A)
Buid - Max - Heap( A);
for i length[ A] downto 2
do begin
exchange A[1] A[i ];
heap - size[ A] heap - size[ A] 1;
Max - Heapify ( A,1);
end - for
Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
Build a max-heap
16
14 10
7 9 3
8
2 4 1
14 10
7 9 3
8
2 4 16
1
14 10
7 9 3
8
2 4 16
8 10
7 9 3
4
2 1 16
14
8 10
7 9 3
4
2 1 16
8 10
7 9 3
4
2 14 16
1
8 10
7 9 3
4
2 14 16
8 9
7 1 3
4
2 14 16
10
8 9
7 1 3
4
2 14 16
8 9
7 1 3
4
10 14 16
2
8 9
7 1 3
4
10 14 16
8 3
7 1 2
4
10 14 16
8 3
7 1 2
4
10 14 16
8 3
7 1 9
4
10 14 16
7 3
4 2 1 9
10 14 16
7 3
4 2 8 9
10 14 16
1, 7, 3, 4, 2, 8, 9, 10, 14,16.
7
4 3
1 2 8 9
10 14 16
2
4 3
1 7 8 9
10 14 16
4
2 3
1 7 8 9
10 14 16
1
2 3
4 7 8 9
10 14 16
3
2 1
4 7 8 9
10 14 16
1
2 3
4 7 8 9
10 14 16
2
1 3
4 7 8 9
10 14 16
1
2 3
4 7 8 9
10 14 16
1, 2, 3, 4, 7, 8, 9, 10, 14,16.
Priority Queues
A priority queue is a data structure for maintaining a set S of elements, each with
an associated value called a key.
We will only consider a max-priority queue.
If we give the key a meaning, such as priority, so that elements with the highest
priority have the highest value of key, then we can use the heap structure to
extract the element with the highest priority.