SlideShare a Scribd company logo
1 of 57
Download to read offline
Data Structures & Algorithm
CS-102
Lecture 8
Binary Heaps
A priority Queue Data Structures
Lecturer: Syeda Nazia Ashraf
1
2
Heap
• A heap is a complete binary tree that
conforms to the heap order.
• The heap order property: in a (min)
heap, for every node X, the key in the
parent is smaller than (or equal to) the key
in X.
• Or, the parent node has key smaller than
or equal to both of its children nodes.
3
Heap
• Analogously, we can define a max-heap,
where the parent has a key larger than the
its two children.
• Thus the largest key would be in the root.
4
5
Heap
13
16
21
31
26
24
65 32
68
19
This is a min
heap
6
Heap
Not a heap: (min)heap property violated
13
19
21
31
26
6
65 32
68
16


19>16
6<21
7
Make Min Heap
Pushdown(6,10)
Pushdown(7,10)
8
Make Max Heap
9
Inserting into a Heap
13
16
21
31
26
24
65 32
68
19
13 21 16 24 31 19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
This is an existing heap
10
Inserting into a Heap
insert(14) 13
16
21
31
26
24
65 32
68
19
13 21 16 24 31 19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
14
14
11
11
Inserting into a Heap
insert(14) 13
16
21
31
26
24
65 32
68
19
13 21 16 24 31 19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10 11
12
Inserting into a Heap
insert(14) 13
16
21
31
26
24
65 32
68
19
13 21 16 24 31
19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10 11
13
Inserting into a Heap
insert(14) 13
16
21
31
26
24
65 32
68
19
13 21
16 24 31
19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10 11
14
Inserting into a Heap
insert(14) 13
16
21
31
26
24
65 32
68
19
13 21
16 24 31
19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10 11
14
14
15
Inserting into a Heap
insert(14) with
exchange
13
16
21
31
26
24
65 32
68
19
13 21 16 24 31 19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
14
14
11
16
Inserting into a Heap
insert(14) with
exchange
13
16
21
31
26
24
65 32
68
19
13 21 16 24 31
19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
14
14
11
17
Inserting into a Heap
insert(14) with
exchange
13
16
21
31
26
24
65 32
68
19
13 21
16 24 31
19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
14
14
11
18
Inserting into a Heap
insert(15) with
exchange
13
16
21
31
26
24
65 32
68
19
13 21
16 24 31
19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
14
14
11
15
15
12
19
Inserting into a Heap
insert(15) with
exchange
13
16
21
31
26
24
65 32
68
19
13 21
16 24 31 19
68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
14
14
11
15
15
12
20
Inserting into a Heap
insert(15) with
exchange
13
16
21
31
26
24
65 32
68
19
13 21 16
24 31 19
68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
14
14
11
15
15
12
21
Inserting into a Heap
insert(15) with
exchange
13
16
21
31
26
24
65 32
68
19
13 21 16
24 31 19
68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
14
14
11
15
15
12
Inserting into a Heap
22
Max Heap
To add an item to a heap, we follow the reverse procedure. Place it in the next
leaf position and move it up.
Again, we require O(h) or O(logn) exchanges.
23
P<X
N<X
24
T<X
25
Insert in Min Heap
26
Insert in Max Heap
27
DeleteMin
• Deletion in Max (or Min) Heap always happens at the root
to remove the Maximum (or minimum) value.
• Deleting it (or removing it) from root causes a hole which
needs to be filled.
13
16
21
31
26
24
65 32
68
19
1
2 3
7
6
5
4
8 9 10
14
11
28
DeleteMin
deleteMin()
16
21
31
26
24
65 32
68
19
1
2 3
7
6
5
4
8 9 10
14
11
29
DeleteMin
deleteMin()
14
16
21
31
26
24
65 32
68
19
1
2 3
7
6
5
4
8 9 10 11
30
DeleteMin
deleteMin()
14
16
31
26
24
65 32
68
19
1
2 3
7
6
5
4
8 9 10
21
11
31
DeleteMin
deleteMin()
14
16
31
26
24
65 32
68
19
1
2 3
7
6
5
4
8 9 10
21
11
32
DeleteMin
deleteMin(): heap size is reduced by 1.
14
16
31
26
24
65 32
68
19
1
2 3
7
6
5
4
8 9 10
21
DeleteMin
33
34
35
DelMin
Let's start with this heap. A
deletion will remove the T
at the root.
To work out how we're going
to maintain the heap property,
use the fact that a complete
tree is filled from the left. So
that the position which must
become empty is the one
occupied by the M. Put it in
the vacant root position.
DeleteMax
36
This has violated the condition
that the root must be greater
than each of its children. So
interchange the M with the
larger of its children.
The left subtree has now lost
the heap property. So again
interchange the M with the
larger of its children.
This tree is now a heap
again, so we're finished.
DeleteMax
We need to make at most h interchanges of a root of a subtree with one of its children to
fully restore the heap property. Thus deletion from a heap is O(h) or O(logn).
37
38
BuildHeap
• Suppose we are given as input N keys (or
items) and we want to build a heap of the
keys.
• Obviously, this can be done with N
successive inserts.
• Each call to insert will either take unit time
(leaf node) or log2N (if new key percolates
all the way up to the root).
39
BuildHeap
• The worst time for building a heap of N
keys could be Nlog2N.
• It turns out that we can build a heap in
linear time.
40
BuildHeap
• Suppose we have a method
percolateDown(p) which moves down the
key in node p downwards.
• This is what was happening in deleteMin.
41
BuildHeap
Initial data (N=15)
65 21 19
26 14 16
68 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
31 32
15
4
5 70 12
42
BuildHeap
Initial data (N=15) 65
19
21
14
24
26
13 15
68
16
65 21 19
26 14 16
68 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
31
31
11
32
32
12
15
4
5
13 70
14 12
15
5 70 12
43
BuildHeap
• The general algorithm is to place the N
keys in an array and consider it to be an
unordered binary tree.
• The following algorithm will build a heap
out of N keys.
for( i = N/2; i > 0; i-- )
percolateDown(i);
44
BuildHeap
i = 15/2 = 7 65
19
21
14
24
26
13 15
68
16
65 21 19
26 14 16
68 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
31
31
11
32
32
12
15
4
5
13 70
14 12
15
5 70 12
i
 i
i=n/2
Minimum Heap
45
BuildHeap
i = 15/2 = 7 65
19
21
14
24
26
13 15
12
16
65 21 19
26 14 16
12 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
31
31
11
32
32
12
15
4
5
13 70
14 68
15
5 70 68
i
 i
46
BuildHeap
i = 6 65
19
21
14
24
26
13 15
12
16
65 21 19
26 14 16
12 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
31
31
11
32
32
12
15
4
5
13 70
14 68
15
5 70 68
i
 i
BuildHeap
i = 5 65
5
21
14
24
26
13 15
12
16
65 21 5
26 14 16
12 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
31
31
11
32
32
12
15
4
19
13 70
14 68
15
19 70 68
i
 i
47
BuildHeap
i = 4 65
5
14
21
24
26
13 15
12
16
65 14 5
26 21 16
12 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
31
31
11
32
32
12
15
4
19
13 70
14 68
15
19 70 68
i
 i
48
BuildHeap
i = 3 65
5
14
21
24
13
26 15
12
16
65 14 5
13 21 16
12 26 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
31
31
11
32
32
12
15
4
19
13 70
14 68
15
19 70 68
i
 i
49
BuildHeap
i = 2 65
16
14
21
24
13
26 15
12
32
65 14 16
13 21 32
12 26 24 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
31
31
11
5
5
12
15
4
19
13 70
14 68
15
19 70 68
i
 i
50
BuildHeap
i = 1 65
16
14
21
31
24
26 15
12
32
65 14 16
24 21 32
12 26 31 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
13
13
11
5
5
12
15
4
19
13 70
14 68
15
19 70 68
i
 i
51
BuildHeap
Min heap 5
16
14
21
31
24
26 15
65
32
5 14 16
24 21 32
65 26 31 15
1 2 3 5 6 7 8 9 10 11 12 13 14
0
1
2 3
7
6
5
4
8 9 10
13
13
11
12
12
12
15
4
19
13 70
14 68
15
19 70 68
52
53
Build Min Heap
i
i i
i=n/2
i = 5/2 = 2
54
Build Max Heap Insert sequence: RPOFESSIRKAL Letter which comes after, it is greater
55
Max Heap
56
Other Heap Operations
• decreaseKey(p, delta):
lowers the value of the key at position ‘p’ by the
amount ‘delta’. Since this might violate the heap order,
the heap must be reorganized with percolate up (in
min heap) or down (in max heap).
• increaseKey(p, delta):
opposite of decreaseKey.
• remove(p):
removes the node at position p from the heap. This is
done by first decreaseKey(p, delta) and then
performing deleteMin().
57

More Related Content

What's hot

Measures of Variability Part 2
Measures of Variability Part 2Measures of Variability Part 2
Measures of Variability Part 2James Bausa
 
as400 built in function- %SECONDS
as400 built in function- %SECONDSas400 built in function- %SECONDS
as400 built in function- %SECONDSaminem_mp
 
Mathematical Modelling of Electro-Mechanical System in Matlab
Mathematical Modelling of Electro-Mechanical System in MatlabMathematical Modelling of Electro-Mechanical System in Matlab
Mathematical Modelling of Electro-Mechanical System in MatlabCOMSATS Abbottabad
 
GraphBLAS: A linear algebraic approach for high-performance graph queries
GraphBLAS: A linear algebraic approach for high-performance graph queriesGraphBLAS: A linear algebraic approach for high-performance graph queries
GraphBLAS: A linear algebraic approach for high-performance graph queriesGábor Szárnyas
 
Auntonomous second no4
Auntonomous second no4Auntonomous second no4
Auntonomous second no4Jun Terauchi
 
Ethereum 9¾ @ Devcon5
Ethereum 9¾ @ Devcon5Ethereum 9¾ @ Devcon5
Ethereum 9¾ @ Devcon5Wanseob Lim
 
Big data big_skills_data_visualization
Big data big_skills_data_visualizationBig data big_skills_data_visualization
Big data big_skills_data_visualizationKyle Hailey
 
Multiplying polynomials powerpoint
Multiplying polynomials powerpointMultiplying polynomials powerpoint
Multiplying polynomials powerpoint40394303
 
Making use of reliability statistics
Making use of reliability statisticsMaking use of reliability statistics
Making use of reliability statisticsAccendo Reliability
 
高いChurn耐性と検索性能を持つキー順序保存型構造化オーバレイネットワークSuzakuの提案と評価
高いChurn耐性と検索性能を持つキー順序保存型構造化オーバレイネットワークSuzakuの提案と評価高いChurn耐性と検索性能を持つキー順序保存型構造化オーバレイネットワークSuzakuの提案と評価
高いChurn耐性と検索性能を持つキー順序保存型構造化オーバレイネットワークSuzakuの提案と評価Kota Abe
 
A successful maximum likelihood parameter estimation in skewed distributions ...
A successful maximum likelihood parameter estimation in skewed distributions ...A successful maximum likelihood parameter estimation in skewed distributions ...
A successful maximum likelihood parameter estimation in skewed distributions ...Hideo Hirose
 

What's hot (12)

Need download
Need downloadNeed download
Need download
 
Measures of Variability Part 2
Measures of Variability Part 2Measures of Variability Part 2
Measures of Variability Part 2
 
as400 built in function- %SECONDS
as400 built in function- %SECONDSas400 built in function- %SECONDS
as400 built in function- %SECONDS
 
Mathematical Modelling of Electro-Mechanical System in Matlab
Mathematical Modelling of Electro-Mechanical System in MatlabMathematical Modelling of Electro-Mechanical System in Matlab
Mathematical Modelling of Electro-Mechanical System in Matlab
 
GraphBLAS: A linear algebraic approach for high-performance graph queries
GraphBLAS: A linear algebraic approach for high-performance graph queriesGraphBLAS: A linear algebraic approach for high-performance graph queries
GraphBLAS: A linear algebraic approach for high-performance graph queries
 
Auntonomous second no4
Auntonomous second no4Auntonomous second no4
Auntonomous second no4
 
Ethereum 9¾ @ Devcon5
Ethereum 9¾ @ Devcon5Ethereum 9¾ @ Devcon5
Ethereum 9¾ @ Devcon5
 
Big data big_skills_data_visualization
Big data big_skills_data_visualizationBig data big_skills_data_visualization
Big data big_skills_data_visualization
 
Multiplying polynomials powerpoint
Multiplying polynomials powerpointMultiplying polynomials powerpoint
Multiplying polynomials powerpoint
 
Making use of reliability statistics
Making use of reliability statisticsMaking use of reliability statistics
Making use of reliability statistics
 
高いChurn耐性と検索性能を持つキー順序保存型構造化オーバレイネットワークSuzakuの提案と評価
高いChurn耐性と検索性能を持つキー順序保存型構造化オーバレイネットワークSuzakuの提案と評価高いChurn耐性と検索性能を持つキー順序保存型構造化オーバレイネットワークSuzakuの提案と評価
高いChurn耐性と検索性能を持つキー順序保存型構造化オーバレイネットワークSuzakuの提案と評価
 
A successful maximum likelihood parameter estimation in skewed distributions ...
A successful maximum likelihood parameter estimation in skewed distributions ...A successful maximum likelihood parameter estimation in skewed distributions ...
A successful maximum likelihood parameter estimation in skewed distributions ...
 

Similar to LEC 8-DS ALGO(heaps).pdf

Heapsortokkay
HeapsortokkayHeapsortokkay
HeapsortokkayRemesha
 
computer notes - Data Structures - 25
computer notes - Data Structures - 25computer notes - Data Structures - 25
computer notes - Data Structures - 25ecomputernotes
 
Computer notes - Build Heap
Computer notes  - Build HeapComputer notes  - Build Heap
Computer notes - Build Heapecomputernotes
 
Overview of sparse and low-rank matrix / tensor techniques
Overview of sparse and low-rank matrix / tensor techniques Overview of sparse and low-rank matrix / tensor techniques
Overview of sparse and low-rank matrix / tensor techniques Alexander Litvinenko
 
Identification of unknown parameters and prediction with hierarchical matrice...
Identification of unknown parameters and prediction with hierarchical matrice...Identification of unknown parameters and prediction with hierarchical matrice...
Identification of unknown parameters and prediction with hierarchical matrice...Alexander Litvinenko
 
Identification of unknown parameters and prediction of missing values. Compar...
Identification of unknown parameters and prediction of missing values. Compar...Identification of unknown parameters and prediction of missing values. Compar...
Identification of unknown parameters and prediction of missing values. Compar...Alexander Litvinenko
 
Fast and accurate metrics. Is it actually possible?
Fast and accurate metrics. Is it actually possible?Fast and accurate metrics. Is it actually possible?
Fast and accurate metrics. Is it actually possible?Bogdan Storozhuk
 
Application of parallel hierarchical matrices for parameter inference and pre...
Application of parallel hierarchical matrices for parameter inference and pre...Application of parallel hierarchical matrices for parameter inference and pre...
Application of parallel hierarchical matrices for parameter inference and pre...Alexander Litvinenko
 
Bubble Sort Python
Bubble Sort PythonBubble Sort Python
Bubble Sort PythonValneyFilho1
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heapNazir Ahmed
 
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...Muhammad Abuzar
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil DuttAnil Dutt
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1subhashchandra197
 
Next Generation Sequencing file Formats ( 2017 )
Next Generation Sequencing file Formats ( 2017 )Next Generation Sequencing file Formats ( 2017 )
Next Generation Sequencing file Formats ( 2017 )Pierre Lindenbaum
 
3. sifir petak 9
3. sifir petak 93. sifir petak 9
3. sifir petak 9yasmiranina
 
01 - 01 January - Sorting
01 - 01 January - Sorting01 - 01 January - Sorting
01 - 01 January - SortingNeeldhara Misra
 

Similar to LEC 8-DS ALGO(heaps).pdf (20)

Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
computer notes - Data Structures - 25
computer notes - Data Structures - 25computer notes - Data Structures - 25
computer notes - Data Structures - 25
 
Computer notes - Build Heap
Computer notes  - Build HeapComputer notes  - Build Heap
Computer notes - Build Heap
 
Overview of sparse and low-rank matrix / tensor techniques
Overview of sparse and low-rank matrix / tensor techniques Overview of sparse and low-rank matrix / tensor techniques
Overview of sparse and low-rank matrix / tensor techniques
 
Identification of unknown parameters and prediction with hierarchical matrice...
Identification of unknown parameters and prediction with hierarchical matrice...Identification of unknown parameters and prediction with hierarchical matrice...
Identification of unknown parameters and prediction with hierarchical matrice...
 
Identification of unknown parameters and prediction of missing values. Compar...
Identification of unknown parameters and prediction of missing values. Compar...Identification of unknown parameters and prediction of missing values. Compar...
Identification of unknown parameters and prediction of missing values. Compar...
 
Fast and accurate metrics. Is it actually possible?
Fast and accurate metrics. Is it actually possible?Fast and accurate metrics. Is it actually possible?
Fast and accurate metrics. Is it actually possible?
 
Application of parallel hierarchical matrices for parameter inference and pre...
Application of parallel hierarchical matrices for parameter inference and pre...Application of parallel hierarchical matrices for parameter inference and pre...
Application of parallel hierarchical matrices for parameter inference and pre...
 
cs1311lecture16wdl.ppt
cs1311lecture16wdl.pptcs1311lecture16wdl.ppt
cs1311lecture16wdl.ppt
 
Bubble Sort Python
Bubble Sort PythonBubble Sort Python
Bubble Sort Python
 
Bubble Sort.ppt
Bubble Sort.pptBubble Sort.ppt
Bubble Sort.ppt
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heap
 
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil Dutt
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
 
12_prime.pdf
12_prime.pdf12_prime.pdf
12_prime.pdf
 
Next Generation Sequencing file Formats ( 2017 )
Next Generation Sequencing file Formats ( 2017 )Next Generation Sequencing file Formats ( 2017 )
Next Generation Sequencing file Formats ( 2017 )
 
3. sifir petak 9
3. sifir petak 93. sifir petak 9
3. sifir petak 9
 
Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptx
 
01 - 01 January - Sorting
01 - 01 January - Sorting01 - 01 January - Sorting
01 - 01 January - Sorting
 

More from MuhammadUmerIhtisham

Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfMuhammadUmerIhtisham
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdfMuhammadUmerIhtisham
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfMuhammadUmerIhtisham
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfMuhammadUmerIhtisham
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfMuhammadUmerIhtisham
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfMuhammadUmerIhtisham
 

More from MuhammadUmerIhtisham (14)

LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdf
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
 
lect 1-ds algo(final)_2.pdf
lect 1-ds  algo(final)_2.pdflect 1-ds  algo(final)_2.pdf
lect 1-ds algo(final)_2.pdf
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdf
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdf
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdf
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
 

Recently uploaded

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 

Recently uploaded (20)

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 

LEC 8-DS ALGO(heaps).pdf

  • 1. Data Structures & Algorithm CS-102 Lecture 8 Binary Heaps A priority Queue Data Structures Lecturer: Syeda Nazia Ashraf 1
  • 2. 2 Heap • A heap is a complete binary tree that conforms to the heap order. • The heap order property: in a (min) heap, for every node X, the key in the parent is smaller than (or equal to) the key in X. • Or, the parent node has key smaller than or equal to both of its children nodes.
  • 3. 3 Heap • Analogously, we can define a max-heap, where the parent has a key larger than the its two children. • Thus the largest key would be in the root.
  • 4. 4
  • 6. 6 Heap Not a heap: (min)heap property violated 13 19 21 31 26 6 65 32 68 16   19>16 6<21
  • 9. 9 Inserting into a Heap 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 This is an existing heap
  • 10. 10 Inserting into a Heap insert(14) 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 14 14 11
  • 11. 11 Inserting into a Heap insert(14) 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 11
  • 12. 12 Inserting into a Heap insert(14) 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 11
  • 13. 13 Inserting into a Heap insert(14) 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 11
  • 14. 14 Inserting into a Heap insert(14) 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 11 14 14
  • 15. 15 Inserting into a Heap insert(14) with exchange 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 14 14 11
  • 16. 16 Inserting into a Heap insert(14) with exchange 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 14 14 11
  • 17. 17 Inserting into a Heap insert(14) with exchange 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 14 14 11
  • 18. 18 Inserting into a Heap insert(15) with exchange 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 14 14 11 15 15 12
  • 19. 19 Inserting into a Heap insert(15) with exchange 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 14 14 11 15 15 12
  • 20. 20 Inserting into a Heap insert(15) with exchange 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 14 14 11 15 15 12
  • 21. 21 Inserting into a Heap insert(15) with exchange 13 16 21 31 26 24 65 32 68 19 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 14 14 11 15 15 12
  • 22. Inserting into a Heap 22 Max Heap To add an item to a heap, we follow the reverse procedure. Place it in the next leaf position and move it up. Again, we require O(h) or O(logn) exchanges.
  • 27. 27 DeleteMin • Deletion in Max (or Min) Heap always happens at the root to remove the Maximum (or minimum) value. • Deleting it (or removing it) from root causes a hole which needs to be filled. 13 16 21 31 26 24 65 32 68 19 1 2 3 7 6 5 4 8 9 10 14 11
  • 32. 32 DeleteMin deleteMin(): heap size is reduced by 1. 14 16 31 26 24 65 32 68 19 1 2 3 7 6 5 4 8 9 10 21
  • 34. 34
  • 36. Let's start with this heap. A deletion will remove the T at the root. To work out how we're going to maintain the heap property, use the fact that a complete tree is filled from the left. So that the position which must become empty is the one occupied by the M. Put it in the vacant root position. DeleteMax 36
  • 37. This has violated the condition that the root must be greater than each of its children. So interchange the M with the larger of its children. The left subtree has now lost the heap property. So again interchange the M with the larger of its children. This tree is now a heap again, so we're finished. DeleteMax We need to make at most h interchanges of a root of a subtree with one of its children to fully restore the heap property. Thus deletion from a heap is O(h) or O(logn). 37
  • 38. 38 BuildHeap • Suppose we are given as input N keys (or items) and we want to build a heap of the keys. • Obviously, this can be done with N successive inserts. • Each call to insert will either take unit time (leaf node) or log2N (if new key percolates all the way up to the root).
  • 39. 39 BuildHeap • The worst time for building a heap of N keys could be Nlog2N. • It turns out that we can build a heap in linear time.
  • 40. 40 BuildHeap • Suppose we have a method percolateDown(p) which moves down the key in node p downwards. • This is what was happening in deleteMin.
  • 41. 41 BuildHeap Initial data (N=15) 65 21 19 26 14 16 68 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 31 32 15 4 5 70 12
  • 42. 42 BuildHeap Initial data (N=15) 65 19 21 14 24 26 13 15 68 16 65 21 19 26 14 16 68 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 12 15 5 70 12
  • 43. 43 BuildHeap • The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. • The following algorithm will build a heap out of N keys. for( i = N/2; i > 0; i-- ) percolateDown(i);
  • 44. 44 BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 68 16 65 21 19 26 14 16 68 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 12 15 5 70 12 i  i i=n/2 Minimum Heap
  • 45. 45 BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i  i
  • 46. 46 BuildHeap i = 6 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i  i
  • 47. BuildHeap i = 5 65 5 21 14 24 26 13 15 12 16 65 21 5 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i 47
  • 48. BuildHeap i = 4 65 5 14 21 24 26 13 15 12 16 65 14 5 26 21 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i 48
  • 49. BuildHeap i = 3 65 5 14 21 24 13 26 15 12 16 65 14 5 13 21 16 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i 49
  • 50. BuildHeap i = 2 65 16 14 21 24 13 26 15 12 32 65 14 16 13 21 32 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i  i 50
  • 51. BuildHeap i = 1 65 16 14 21 31 24 26 15 12 32 65 14 16 24 21 32 12 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i  i 51
  • 52. BuildHeap Min heap 5 16 14 21 31 24 26 15 65 32 5 14 16 24 21 32 65 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 12 12 12 15 4 19 13 70 14 68 15 19 70 68 52
  • 53. 53 Build Min Heap i i i i=n/2 i = 5/2 = 2
  • 54. 54 Build Max Heap Insert sequence: RPOFESSIRKAL Letter which comes after, it is greater
  • 56. 56
  • 57. Other Heap Operations • decreaseKey(p, delta): lowers the value of the key at position ‘p’ by the amount ‘delta’. Since this might violate the heap order, the heap must be reorganized with percolate up (in min heap) or down (in max heap). • increaseKey(p, delta): opposite of decreaseKey. • remove(p): removes the node at position p from the heap. This is done by first decreaseKey(p, delta) and then performing deleteMin(). 57