際際滷

際際滷Share a Scribd company logo
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 Ive 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 (20)

Urinary Tract Infection & Sexually Transmt ted Diseases.pptx
Urinary Tract Infection & Sexually Transmt ted Diseases.pptxUrinary Tract Infection & Sexually Transmt ted Diseases.pptx
Urinary Tract Infection & Sexually Transmt ted Diseases.pptx
Ashish Umale
ANTIVIRAL agent by Mrs. Manjushri Dabhade
ANTIVIRAL agent by Mrs. Manjushri DabhadeANTIVIRAL agent by Mrs. Manjushri Dabhade
ANTIVIRAL agent by Mrs. Manjushri Dabhade
Dabhade madam Dabhade
3. AI Trust Layer, Governance Explainability, Security & Compliance.pdf
3. AI Trust Layer, Governance  Explainability, Security & Compliance.pdf3. AI Trust Layer, Governance  Explainability, Security & Compliance.pdf
3. AI Trust Layer, Governance Explainability, Security & Compliance.pdf
Mukesh Kala
STOMACH Gross Anatomy & Clinical Anatomy.pptx
STOMACH Gross Anatomy & Clinical Anatomy.pptxSTOMACH Gross Anatomy & Clinical Anatomy.pptx
STOMACH Gross Anatomy & Clinical Anatomy.pptx
Sid Roy
Yale VMOC Special Report - Measles Outbreak Southwest US 3-26-2025 FINAL.pptx
Yale VMOC  Special Report - Measles Outbreak  Southwest US 3-26-2025  FINAL.pptxYale VMOC  Special Report - Measles Outbreak  Southwest US 3-26-2025  FINAL.pptx
Yale VMOC Special Report - Measles Outbreak Southwest US 3-26-2025 FINAL.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷sHow to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
Celine George
EDL 290F Week 5 - Facing Headwinds and Hairpin Turns (2025).pdf
EDL 290F Week 5  - Facing Headwinds and Hairpin Turns (2025).pdfEDL 290F Week 5  - Facing Headwinds and Hairpin Turns (2025).pdf
EDL 290F Week 5 - Facing Headwinds and Hairpin Turns (2025).pdf
Liz Walsh-Trevino
S. Y. G. N. M. CHILD HEALTH NURSING Leukemia in Children.pptx
S. Y. G. N. M. CHILD HEALTH NURSING Leukemia in Children.pptxS. Y. G. N. M. CHILD HEALTH NURSING Leukemia in Children.pptx
S. Y. G. N. M. CHILD HEALTH NURSING Leukemia in Children.pptx
sachin7989
Crafting Winning STEM & Science Grant Proposals
Crafting Winning STEM & Science Grant ProposalsCrafting Winning STEM & Science Grant Proposals
Crafting Winning STEM & Science Grant Proposals
Kevin Welch
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VIAnti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Samruddhi Khonde
EDL 290F Week 4 - Group Ride (2025).pdf
EDL 290F Week 4  - Group Ride (2025).pdfEDL 290F Week 4  - Group Ride (2025).pdf
EDL 290F Week 4 - Group Ride (2025).pdf
Liz Walsh-Trevino
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
 Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ... Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
coreylewis960
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
home
Yale VMOC Special Report - Measles Outbreak Southwest US 3-30-2025 FINAL v2...
Yale VMOC Special Report - Measles Outbreak  Southwest US 3-30-2025  FINAL v2...Yale VMOC Special Report - Measles Outbreak  Southwest US 3-30-2025  FINAL v2...
Yale VMOC Special Report - Measles Outbreak Southwest US 3-30-2025 FINAL v2...
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
U.S. Department of Education certification
U.S. Department of Education certificationU.S. Department of Education certification
U.S. Department of Education certification
Mebane Rash
Tollywood Quiz- 21st March 2025, Quiz Club NITW
Tollywood Quiz- 21st March 2025, Quiz Club NITWTollywood Quiz- 21st March 2025, Quiz Club NITW
Tollywood Quiz- 21st March 2025, Quiz Club NITW
Quiz Club NITW
Unit1 Inroduction to Internal Combustion Engines
Unit1  Inroduction to Internal Combustion EnginesUnit1  Inroduction to Internal Combustion Engines
Unit1 Inroduction to Internal Combustion Engines
NileshKumbhar21
URLS and routing in odoo 18 - Odoo 際際滷s
URLS and routing in odoo 18 - Odoo 際際滷sURLS and routing in odoo 18 - Odoo 際際滷s
URLS and routing in odoo 18 - Odoo 際際滷s
Celine George
VTU notes for Indian Knowledge System 2022 scheme ppt
VTU notes for Indian Knowledge System 2022 scheme pptVTU notes for Indian Knowledge System 2022 scheme ppt
VTU notes for Indian Knowledge System 2022 scheme ppt
Suvarna Hiremath
Sulfonamides by Mrs. Manjushri P. Dabhade
Sulfonamides by Mrs. Manjushri P. DabhadeSulfonamides by Mrs. Manjushri P. Dabhade
Sulfonamides by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Urinary Tract Infection & Sexually Transmt ted Diseases.pptx
Urinary Tract Infection & Sexually Transmt ted Diseases.pptxUrinary Tract Infection & Sexually Transmt ted Diseases.pptx
Urinary Tract Infection & Sexually Transmt ted Diseases.pptx
Ashish Umale
ANTIVIRAL agent by Mrs. Manjushri Dabhade
ANTIVIRAL agent by Mrs. Manjushri DabhadeANTIVIRAL agent by Mrs. Manjushri Dabhade
ANTIVIRAL agent by Mrs. Manjushri Dabhade
Dabhade madam Dabhade
3. AI Trust Layer, Governance Explainability, Security & Compliance.pdf
3. AI Trust Layer, Governance  Explainability, Security & Compliance.pdf3. AI Trust Layer, Governance  Explainability, Security & Compliance.pdf
3. AI Trust Layer, Governance Explainability, Security & Compliance.pdf
Mukesh Kala
STOMACH Gross Anatomy & Clinical Anatomy.pptx
STOMACH Gross Anatomy & Clinical Anatomy.pptxSTOMACH Gross Anatomy & Clinical Anatomy.pptx
STOMACH Gross Anatomy & Clinical Anatomy.pptx
Sid Roy
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷sHow to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
How to Install Odoo 18 with Pycharm - Odoo 18 際際滷s
Celine George
EDL 290F Week 5 - Facing Headwinds and Hairpin Turns (2025).pdf
EDL 290F Week 5  - Facing Headwinds and Hairpin Turns (2025).pdfEDL 290F Week 5  - Facing Headwinds and Hairpin Turns (2025).pdf
EDL 290F Week 5 - Facing Headwinds and Hairpin Turns (2025).pdf
Liz Walsh-Trevino
S. Y. G. N. M. CHILD HEALTH NURSING Leukemia in Children.pptx
S. Y. G. N. M. CHILD HEALTH NURSING Leukemia in Children.pptxS. Y. G. N. M. CHILD HEALTH NURSING Leukemia in Children.pptx
S. Y. G. N. M. CHILD HEALTH NURSING Leukemia in Children.pptx
sachin7989
Crafting Winning STEM & Science Grant Proposals
Crafting Winning STEM & Science Grant ProposalsCrafting Winning STEM & Science Grant Proposals
Crafting Winning STEM & Science Grant Proposals
Kevin Welch
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VIAnti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Anti-Viral Agents.pptx Medicinal Chemistry III, B Pharm SEM VI
Samruddhi Khonde
EDL 290F Week 4 - Group Ride (2025).pdf
EDL 290F Week 4  - Group Ride (2025).pdfEDL 290F Week 4  - Group Ride (2025).pdf
EDL 290F Week 4 - Group Ride (2025).pdf
Liz Walsh-Trevino
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
 Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ... Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
Marketing is Everything in the Beauty Business! 憓 Talent gets you in the ...
coreylewis960
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
20250402 ACCA TeamScienceAIEra 20250402 v10.pptx
home
U.S. Department of Education certification
U.S. Department of Education certificationU.S. Department of Education certification
U.S. Department of Education certification
Mebane Rash
Tollywood Quiz- 21st March 2025, Quiz Club NITW
Tollywood Quiz- 21st March 2025, Quiz Club NITWTollywood Quiz- 21st March 2025, Quiz Club NITW
Tollywood Quiz- 21st March 2025, Quiz Club NITW
Quiz Club NITW
Unit1 Inroduction to Internal Combustion Engines
Unit1  Inroduction to Internal Combustion EnginesUnit1  Inroduction to Internal Combustion Engines
Unit1 Inroduction to Internal Combustion Engines
NileshKumbhar21
URLS and routing in odoo 18 - Odoo 際際滷s
URLS and routing in odoo 18 - Odoo 際際滷sURLS and routing in odoo 18 - Odoo 際際滷s
URLS and routing in odoo 18 - Odoo 際際滷s
Celine George
VTU notes for Indian Knowledge System 2022 scheme ppt
VTU notes for Indian Knowledge System 2022 scheme pptVTU notes for Indian Knowledge System 2022 scheme ppt
VTU notes for Indian Knowledge System 2022 scheme ppt
Suvarna Hiremath
Sulfonamides by Mrs. Manjushri P. Dabhade
Sulfonamides by Mrs. Manjushri P. DabhadeSulfonamides by Mrs. Manjushri P. Dabhade
Sulfonamides by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
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
Neil Kimberley
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)
contently
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
Albert Qian
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)
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
Search Engine Journal
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
SpeakerHub
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
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
Tessa Mero
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
Lily Ray
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
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
MindGenius
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...
RachelPearson36
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...
Applitools
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
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
Neil Kimberley
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)
contently
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
Albert Qian
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)
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
Search Engine Journal
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
SpeakerHub
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
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
Tessa Mero
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
Lily Ray
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
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
MindGenius
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...
RachelPearson36
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...
Applitools

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 Ive 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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 Lets 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