SlideShare a Scribd company logo
1 of 31
DATA STRUCTURES
AND ALGORITHMS
MIN-HEAP
CONSTRUCTION
SADIA ZAR
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 1
MIN-HEAP – DATA STRUCTURE
• Construct a min-heap from the following sequence:
15, 3, 6, 18 ,5 ,9 ,11
• In this lecture I’ve discussed two methods to construct a min-heap.
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
2
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
In the first method, at first we will construct a complete
binary tree first and then we will check the condition i.e
A[Parent[i]] < = A[i] for every node in the tree.
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
3
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
4
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
5
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
6
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
7
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
8
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
9
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
10
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
11
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
i = 3
Parent[i] = Parent[3] = 1
So the condition is A[1] <= A[3]
i.e, 3 <= 18 so no need to swap
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
12
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
i = 4
Parent[i] = Parent[4] = 1
So the condition is A[1] <= A[4]
i.e, 3 <= 5 so no need to swap
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
13
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
i = 5
Parent[i] = Parent[5] = 2
So the condition is A[2] <= A[5]
i.e, 6 <= 9 so no need to swap
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
14
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
i = 6
Parent[i] = Parent[6] = 2
So the condition is A[2] <= A[6]
i.e, 6 <= 11 so no need to swap
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
15
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
i = 1
Parent[i] = Parent[1] = 0
So the condition is A[0] <= A[1]
i.e, 15 <= 3 (false) so we have to swap
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
16
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
After swapping check other nodes as well
i = 2
Parent[i] = Parent[2] = 0
So the condition is A[0] <= A[2]
i.e, 3 <= 6 so no need to swap
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
17
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
Again check all the nodes now the problem
Lies in node 1 and node 4
i = 4
Parent[i] = Parent[4] = 1
So the condition is A[1] <= A[4]
i.e, 15 <= 5 (false) so we have to swap
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
18
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
Now again check
i = 4
Parent[i] = Parent[4] = 1
So the condition is A[1] <= A[4]
i.e, 5 <= 15 so no need to swap
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
19
MIN-HEAP – DATA STRUCTURE
FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
As you can see that the min-heap is ready
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
20
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
In the second method, as we construct a complete binary
tree, we will check the condition i.e A[Parent[i]] < = A[i] for
every node that is inserted in the tree.
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
21
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
0
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
22
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
i = 1
Parent[i] = Parent[1] = 0
Check the condition A[0]<=A[1]
15<=3 (false) so, we need to swap these two values
0
1
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
23
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
i = 1
Parent[i] = Parent[1] = 0
Check the condition A[0]<=A[1]
3<=15 (true) so, continue
0
1
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
24
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
i = 2
Parent[i] = Parent[2] = 0
Check the condition A[0]<=A[2]
i.e, 3<=15 so no need to swap
0
1 2
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
25
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
i = 3
Parent[i] = Parent[3] = 1
Check the condition A[1]<=A[3]
i.e, 15<=18 so no need to swap
0
1 2
3
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
26
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
i = 4
Parent[i] = Parent[4] = 1
Check the condition A[1]<=A[4]
i.e, 15<=5 (false) so we have to
swap
0
1 2
3 4
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
27
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
No the min-heap is okay
0
1 2
3 4
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
28
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
i = 5
Parent[i] = Parent[5] = 2
Check the condition A[2]<=A[5]
i.e, 6<=9 so no need to swap
0
1 2
3 4 5
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
29
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
i = 6
Parent[i] = Parent[6] = 2
Check the condition A[2]<=A[6]
i.e, 6<=11 so no need to swap
0
1 2
3 4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
30
MIN-HEAP – DATA STRUCTURE
SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11
Now Let’s check the condition for every node
Starting from leaf nodes
As you can see that the min-heap is ready
0
1
2
3
4 5 6
GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W),
RAWALPINDI.
31

More Related Content

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Construct min heap from the given sequence

  • 1. DATA STRUCTURES AND ALGORITHMS MIN-HEAP CONSTRUCTION SADIA ZAR GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 1
  • 2. MIN-HEAP – DATA STRUCTURE • Construct a min-heap from the following sequence: 15, 3, 6, 18 ,5 ,9 ,11 • In this lecture I’ve discussed two methods to construct a min-heap. GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 2
  • 3. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 In the first method, at first we will construct a complete binary tree first and then we will check the condition i.e A[Parent[i]] < = A[i] for every node in the tree. GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 3
  • 4. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 4
  • 5. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 5
  • 6. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 6
  • 7. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 7
  • 8. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 8
  • 9. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 9
  • 10. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 10
  • 11. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 11
  • 12. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes i = 3 Parent[i] = Parent[3] = 1 So the condition is A[1] <= A[3] i.e, 3 <= 18 so no need to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 12
  • 13. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes i = 4 Parent[i] = Parent[4] = 1 So the condition is A[1] <= A[4] i.e, 3 <= 5 so no need to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 13
  • 14. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes i = 5 Parent[i] = Parent[5] = 2 So the condition is A[2] <= A[5] i.e, 6 <= 9 so no need to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 14
  • 15. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes i = 6 Parent[i] = Parent[6] = 2 So the condition is A[2] <= A[6] i.e, 6 <= 11 so no need to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 15
  • 16. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes i = 1 Parent[i] = Parent[1] = 0 So the condition is A[0] <= A[1] i.e, 15 <= 3 (false) so we have to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 16
  • 17. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes After swapping check other nodes as well i = 2 Parent[i] = Parent[2] = 0 So the condition is A[0] <= A[2] i.e, 3 <= 6 so no need to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 17
  • 18. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes Again check all the nodes now the problem Lies in node 1 and node 4 i = 4 Parent[i] = Parent[4] = 1 So the condition is A[1] <= A[4] i.e, 15 <= 5 (false) so we have to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 18
  • 19. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes Now again check i = 4 Parent[i] = Parent[4] = 1 So the condition is A[1] <= A[4] i.e, 5 <= 15 so no need to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 19
  • 20. MIN-HEAP – DATA STRUCTURE FIRST METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes As you can see that the min-heap is ready 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 20
  • 21. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 In the second method, as we construct a complete binary tree, we will check the condition i.e A[Parent[i]] < = A[i] for every node that is inserted in the tree. GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 21
  • 22. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 0 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 22
  • 23. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 i = 1 Parent[i] = Parent[1] = 0 Check the condition A[0]<=A[1] 15<=3 (false) so, we need to swap these two values 0 1 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 23
  • 24. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 i = 1 Parent[i] = Parent[1] = 0 Check the condition A[0]<=A[1] 3<=15 (true) so, continue 0 1 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 24
  • 25. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 i = 2 Parent[i] = Parent[2] = 0 Check the condition A[0]<=A[2] i.e, 3<=15 so no need to swap 0 1 2 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 25
  • 26. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 i = 3 Parent[i] = Parent[3] = 1 Check the condition A[1]<=A[3] i.e, 15<=18 so no need to swap 0 1 2 3 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 26
  • 27. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 i = 4 Parent[i] = Parent[4] = 1 Check the condition A[1]<=A[4] i.e, 15<=5 (false) so we have to swap 0 1 2 3 4 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 27
  • 28. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 No the min-heap is okay 0 1 2 3 4 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 28
  • 29. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 i = 5 Parent[i] = Parent[5] = 2 Check the condition A[2]<=A[5] i.e, 6<=9 so no need to swap 0 1 2 3 4 5 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 29
  • 30. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 i = 6 Parent[i] = Parent[6] = 2 Check the condition A[2]<=A[6] i.e, 6<=11 so no need to swap 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 30
  • 31. MIN-HEAP – DATA STRUCTURE SECOND METHOD - 15, 3, 6, 18 ,5 ,9 ,11 Now Let’s check the condition for every node Starting from leaf nodes As you can see that the min-heap is ready 0 1 2 3 4 5 6 GOVERNMENT VIQAR-UN-NISA POST GRADUATE COLLEGE (W), RAWALPINDI. 31