0% found this document useful (0 votes)
25 views84 pages

Lec 15 Heap

Uploaded by

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

Lec 15 Heap

Uploaded by

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

Data Structures and

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

In a max - heap, every node i other than the


root satisfies the following property :
A[Parent (i )]  A[i ].
Min-Heap

In a min - heap, every node i other than the


root satisfies the following property :
A[Parent (i )]  A[i ].
Heaps
Root

When a complete
binary tree is built,
its first node must be
the root.
Heaps

Almost Complete Left child


binary tree. of the
root

The second node is


always the left child
of the root.
Heaps

Almost Complete Right child


binary tree. of the
root

The third node is


always the right child
of the root.
Heaps

Almost Complete
binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

Almost Complete
binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

Almost Complete
binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

Almost Complete
binary tree.

The next nodes


always fill the next
level from left-to-right.
Heaps

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

 A heap is a complete binary tree, where the entry at each


node is greater than or equal to the entries in its children.
 To add an entry to a heap, place the new entry at the next
available spot, and perform a reheapification upward.
 To remove the biggest entry, move the last node onto the
root, and perform a reheapification downward.
Implementation in C++
#include <iostream>
using namespace std;
#define MAX 5
int heap_size=0;
int harr[MAX];
int parent(int i) { return (i-1)/2; }
// to get index of left child of node at index i
int left(int i) { return (2*i + 1); }
// to get index of right child of node at index i
int right(int i) { return (2*i + 2); }
Implementation in C++
// Inserts a new key 'k' int i = heap_size - 1;
void insertKey_min(int k) harr[i] = k;
{ // Fix the min heap property if it is violated
if (heap_size == MAX) while (i != 0 && harr[parent(i)] > harr[i])
{ {
cout << "\nOverflow: Could not insertKey\n"; int temp = harr[i];
return; harr[i] = harr[parent(i)];
} harr[parent(i)] = temp;
// First insert the new key at the end i = parent(i);
heap_size++; }}
Implementation in C++
// Inserts a new key 'k'
void insertKey_max(int k) // Fix the max heap property if it is violated
{ while (i != 0 && harr[parent(i)] < harr[i])
if (heap_size == MAX) {
{ int temp = harr[i];
cout << "\nOverflow: Could not insertKey\n"; harr[i] = harr[parent(i)];
return;} harr[parent(i)] = temp;
// First insert the new key at the end i = parent(i);
heap_size++; }
int i = heap_size - 1; }
harr[i] = k;
Implementation in C++
// A recursive method to heapify a subtree with if (l < heap_size && harr[l] < harr[i])
root at given index smallest = l;
// This method assumes that the subtrees are if (r < heap_size && harr[r] < harr[smallest])
already heapified
smallest = r;
void MinHeapify(int i)
if (smallest != i)
{
{
int l = left(i);
int temp = harr[i];
int r = right(i);
harr[i] = harr[smallest];
int smallest = i;
harr[smallest] = temp;
MinHeapify(smallest);}}
Implementation in C++
void MaxHeapify(int i) if (largest != i)
{ {
int l = left(i); int temp = harr[i];
int r = right(i); harr[i] = harr[largest];
int largest = i; harr[largest] = temp;
if (l < heap_size && harr[l] > harr[i]) MaxHeapify(largest);
largest = l; }
if (r < heap_size && harr[r] > harr[largest]) }
largest = r;
Implementation in C++
int delete_key() // Store the minimum value, and remove it
{ from heap
if (heap_size <= 0) int root = harr[0];
return 0; harr[0] = harr[heap_size-1];
if (heap_size == 1) heap_size--;
{ // MinHeapify(0);
heap_size--; MaxHeapify(0);
return harr[0]; return root;
} }
Implementation in C++

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

Build - Max - Heap( A)


heap - size[ A]  length[ A];
for i  length[ A] / 2 downto 1
do Max - Heapify( A, i );

e.g., 4, 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

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

16, 14, 10, 8, 7, 9, 3, 2, 4, 1.


1

14 10

7 9 3
8

2 4 16
1

14 10

7 9 3
8

2 4 16

1, 14, 10, 8, 7, 9, 3, 2, 4, 16.


14

8 10

7 9 3
4

2 1 16
14

8 10

7 9 3
4

2 1 16

14, 8, 10, 4, 7, 9, 3, 2, 1, 16.


1

8 10

7 9 3
4

2 14 16
1

8 10

7 9 3
4

2 14 16

1, 8, 10, 4, 7, 9, 3, 2, 14, 16.


10

8 9

7 1 3
4

2 14 16
10

8 9

7 1 3
4

2 14 16

10, 8, 9, 4, 7, 1, 3, 2, 14, 16.


2

8 9

7 1 3
4

10 14 16
2

8 9

7 1 3
4

10 14 16

2, 8, 9, 4, 7, 1, 3, 10, 14, 16.


9

8 3

7 1 2
4

10 14 16

9, 8, 3, 4, 7, 1, 2, 10, 14, 16.


9

8 3

7 1 2
4

10 14 16

9, 8, 3, 4, 7, 1, 2, 10, 14, 16.


2

8 3

7 1 9
4

10 14 16

2, 8, 3, 4, 7, 1, 9, 10, 14, 16.


8

7 3

4 2 1 9

10 14 16

2, 8, 3, 4, 7, 1, 9, 10, 14, 16.


1

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.

You might also like