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

PPTX
Build Heap Step by Step
PDF
Work-stealing Tree Data Structure
PDF
Ctrie Data Structure
PDF
04 - 15 Jan - Heap Sort
PDF
04 - 15 Jan - Heap Sort
PDF
Mathematical Modelling of Electrical/Mechanical modellinng in MATLAB
PDF
Introduction to MATLAB
PDF
Analysis of Electro-Mechanical System
Build Heap Step by Step
Work-stealing Tree Data Structure
Ctrie Data Structure
04 - 15 Jan - Heap Sort
04 - 15 Jan - Heap Sort
Mathematical Modelling of Electrical/Mechanical modellinng in MATLAB
Introduction to MATLAB
Analysis of Electro-Mechanical System

What's hot (12)

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

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

PPT
Heapsortokkay
PPT
Computer notes - Build Heap
PDF
Overview of sparse and low-rank matrix / tensor techniques
PDF
Identification of unknown parameters and prediction with hierarchical matrice...
PDF
Identification of unknown parameters and prediction of missing values. Compar...
PDF
Fast and accurate metrics. Is it actually possible?
PPT
Sorting of arrays, types in c++ .IST .ppt
PPT
sortingofarrays-240425134605-b7961d41.ppt
PDF
DES (Data Encryption Standard) Algorithm
PPT
Bubble Sort Python
PPT
cs1311lecture16wdl.ppt
PPT
Bubble Sort.ppt
PPT
mergesort_data_structure_algorithm-102-fa08.ppt
PPT
mergesort-102-fa08.pptfghgfy6fyHvYHBYHJYBJU
PPTX
12_m-way tree_Btree_Heapppppppppppppp.pptx
PPT
Complete binary tree and heap
PPT
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
PPTX
Sorting techniques Anil Dutt
PPT
3. sifir petak 9
Heapsortokkay
Computer notes - Build Heap
Overview of sparse and low-rank matrix / tensor techniques
Identification of unknown parameters and prediction with hierarchical matrice...
Identification of unknown parameters and prediction of missing values. Compar...
Fast and accurate metrics. Is it actually possible?
Sorting of arrays, types in c++ .IST .ppt
sortingofarrays-240425134605-b7961d41.ppt
DES (Data Encryption Standard) Algorithm
Bubble Sort Python
cs1311lecture16wdl.ppt
Bubble Sort.ppt
mergesort_data_structure_algorithm-102-fa08.ppt
mergesort-102-fa08.pptfghgfy6fyHvYHBYHJYBJU
12_m-way tree_Btree_Heapppppppppppppp.pptx
Complete binary tree and heap
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Sorting techniques Anil Dutt
3. sifir petak 9
Ad

More from MuhammadUmerIhtisham (14)

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

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PDF
Pre independence Education in Inndia.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Business Ethics Teaching Materials for college
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
Pre independence Education in Inndia.pdf
Anesthesia in Laparoscopic Surgery in India
Module 4: Burden of Disease Tutorial Slides S2 2025
102 student loan defaulters named and shamed – Is someone you know on the list?
VCE English Exam - Section C Student Revision Booklet
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Business Ethics Teaching Materials for college
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Week 4 Term 3 Study Techniques revisited.pptx
Cell Types and Its function , kingdom of life
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

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