SlideShare a Scribd company logo
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

Need download
Need downloadNeed download
Need download
Dwisamen Paruruk
 
Measures of Variability Part 2
Measures of Variability Part 2Measures of Variability Part 2
Measures of Variability Part 2
James Bausa
 
as400 built in function- %SECONDS
as400 built in function- %SECONDSas400 built in function- %SECONDS
as400 built in function- %SECONDS
aminem_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 Matlab
COMSATS 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 queries
Gábor Szárnyas
 
Auntonomous second no4
Auntonomous second no4Auntonomous second no4
Auntonomous second no4
Jun Terauchi
 
Ethereum 9¾ @ Devcon5
Ethereum 9¾ @ Devcon5Ethereum 9¾ @ Devcon5
Ethereum 9¾ @ Devcon5
Wanseob 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 statistics
Accendo 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 Heap
ecomputernotes
 
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
 
Sorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .pptSorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .ppt
saadpisjes
 
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
 
cs1311lecture16wdl.ppt
cs1311lecture16wdl.pptcs1311lecture16wdl.ppt
cs1311lecture16wdl.ppt
RameshKumarYadav29
 
Bubble Sort Python
Bubble Sort PythonBubble Sort Python
Bubble Sort Python
ValneyFilho1
 
Bubble Sort.ppt
Bubble Sort.pptBubble Sort.ppt
Bubble Sort.ppt
MuhammadSheraz836877
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heap
Nazir 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 Dutt
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
subhashchandra197
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
12_prime.pdf
12_prime.pdf12_prime.pdf
3. sifir petak 9
3. sifir petak 93. sifir petak 9
3. sifir petak 9yasmiranina
 
Recursion.pptx
Recursion.pptxRecursion.pptx

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?
 
Sorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .pptSorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .ppt
 
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
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
12_prime.pdf
12_prime.pdf12_prime.pdf
12_prime.pdf
 
3. sifir petak 9
3. sifir petak 93. sifir petak 9
3. sifir petak 9
 
Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptx
 

More from MuhammadUmerIhtisham

LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
MuhammadUmerIhtisham
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
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.pdf
MuhammadUmerIhtisham
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
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
MuhammadUmerIhtisham
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
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
MuhammadUmerIhtisham
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
MuhammadUmerIhtisham
 
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
MuhammadUmerIhtisham
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdf
MuhammadUmerIhtisham
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdf
MuhammadUmerIhtisham
 
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
MuhammadUmerIhtisham
 

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

Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

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