SlideShare a Scribd company logo
insertion,
deletion,
sorting
2
Heap
 A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
 A min tree is a tree in which the key value in
each node is no larger than the key values in
its children. A min heap is a complete binary
tree that is also a min tree.
 Operations on heaps
 creation of an empty heap
 insertion of a new element into the heap;
 deletion of the largest element from the heap
Figure : Sample max heaps
[4]
14
12 7
8
10 6
9
6 3
5
30
25
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
Property:
The root of max heap (min heap) contains
the largest (smallest).
2
7 4
8
10 6
10
20 83
50
11
21
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
[4]
Figure : Sample min heaps
5
Example of Insertion to Max Heap
20
15 2
14 10
initial location of new node
21
15 20
14 10 2
insert 21 into heap
20
15 5
14 10 2
insert 5 into heap
Insertion into a Max Heap
INSHEAP(TREE, N, ITEM)
1. Set N=N+1 and PTR=N.
2. Repeat Steps 3 to 6 while PTR>1.
3. Set PAR= └PTR/2┘
4. If ITEM<= TREE[PAR], then:
Set TREE[PTR]=ITEM, and Return.
[End of If structure]
5. Set TREE[PTR]=TREE[PAR]
6. Set PTR=PAR.
[End of step 2]
7. Set TREE[1]=ITEM
8. Return.
7
Example of Deletion from Max Heap
20
remove
15 2
14 10
10
15 2
14
15
14 2
10
Deletion from a Max Heap
DELHEAP(TREE, N, ITEM)
1. Set ITEM= TREE[1].
2. Set LAST= TREE[N] and N= N-1.
3. Set PTR=1, LEFT=2 and RIGHT=3.
4. Repeat Steps 5 to 7 while RIGHT<=N:
5. If LAST>=TREE[LEFT] and LAST>= TREE[RIGHT], then:
Set TREE[PTR]=LAST and Return.
[End of If structure.]
1. If TREE[RIGHT]<= TREE[LEFT], then:
Set TREE[PTR]= TREE[LEFT] and PTR=LEFT.
Else:
Set TREE[PTR]= TREE[RIGHT] and PTR=RIGHT.
[End of If structure.]
1. Set LEFT=2*PTR and RIGHT=LEFT+1.
[End of Step 4 loop.]
8. If LEFT=N and if LAST < TREE[LEFT], TREE[PTR]= TREE[LEFT]
then: Set PTR=LEFT.
9. Set TREE[PTR]= LAST.
10. Return.
HeapSort
HEAPSORT(A, N)
1. Repeat for J=1 to N-1:
Call INSHEAP(A, J, A[J+1]).
[End of loop.]
1. Repeat while N>1:
a) Call DELHEAP(A, N, ITEM).
b) A[N+1]=ITEM.
[End of loop.]
1. Exit.
DS_Mod4_3.pdf

More Related Content

Similar to DS_Mod4_3.pdf

07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Heap sort
Heap sortHeap sort
Heap sort
Ayesha Tahir
 
01_introduction_lab.pdf
01_introduction_lab.pdf01_introduction_lab.pdf
01_introduction_lab.pdf
zehiwot hone
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10Vince Vo
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
NareshBopparathi1
 
NumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxNumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptx
JohnWilliam111370
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
Krishna Nanda
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
EdFeranil
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
Yagna15
 
Array
ArrayArray
Array
PRN USM
 

Similar to DS_Mod4_3.pdf (20)

07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Heaps
HeapsHeaps
Heaps
 
Heap sort
Heap sortHeap sort
Heap sort
 
01_introduction_lab.pdf
01_introduction_lab.pdf01_introduction_lab.pdf
01_introduction_lab.pdf
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
 
Sorting
SortingSorting
Sorting
 
NumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptxNumPy_Broadcasting Data Science - Python.pptx
NumPy_Broadcasting Data Science - Python.pptx
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
Lec23
Lec23Lec23
Lec23
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 
Array
ArrayArray
Array
 

Recently uploaded

NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 

Recently uploaded (20)

NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 

DS_Mod4_3.pdf

  • 2. 2 Heap  A max tree is a tree in which the key value in each node is no smaller than the key values in its children. A max heap is a complete binary tree that is also a max tree.  A min tree is a tree in which the key value in each node is no larger than the key values in its children. A min heap is a complete binary tree that is also a min tree.  Operations on heaps  creation of an empty heap  insertion of a new element into the heap;  deletion of the largest element from the heap
  • 3. Figure : Sample max heaps [4] 14 12 7 8 10 6 9 6 3 5 30 25 [1] [2] [3] [5] [6] [1] [2] [3] [4] [1] [2] Property: The root of max heap (min heap) contains the largest (smallest).
  • 4. 2 7 4 8 10 6 10 20 83 50 11 21 [1] [2] [3] [5] [6] [1] [2] [3] [4] [1] [2] [4] Figure : Sample min heaps
  • 5. 5 Example of Insertion to Max Heap 20 15 2 14 10 initial location of new node 21 15 20 14 10 2 insert 21 into heap 20 15 5 14 10 2 insert 5 into heap
  • 6. Insertion into a Max Heap INSHEAP(TREE, N, ITEM) 1. Set N=N+1 and PTR=N. 2. Repeat Steps 3 to 6 while PTR>1. 3. Set PAR= └PTR/2┘ 4. If ITEM<= TREE[PAR], then: Set TREE[PTR]=ITEM, and Return. [End of If structure] 5. Set TREE[PTR]=TREE[PAR] 6. Set PTR=PAR. [End of step 2] 7. Set TREE[1]=ITEM 8. Return.
  • 7. 7 Example of Deletion from Max Heap 20 remove 15 2 14 10 10 15 2 14 15 14 2 10
  • 8. Deletion from a Max Heap DELHEAP(TREE, N, ITEM) 1. Set ITEM= TREE[1]. 2. Set LAST= TREE[N] and N= N-1. 3. Set PTR=1, LEFT=2 and RIGHT=3. 4. Repeat Steps 5 to 7 while RIGHT<=N: 5. If LAST>=TREE[LEFT] and LAST>= TREE[RIGHT], then: Set TREE[PTR]=LAST and Return. [End of If structure.] 1. If TREE[RIGHT]<= TREE[LEFT], then: Set TREE[PTR]= TREE[LEFT] and PTR=LEFT. Else: Set TREE[PTR]= TREE[RIGHT] and PTR=RIGHT. [End of If structure.] 1. Set LEFT=2*PTR and RIGHT=LEFT+1. [End of Step 4 loop.] 8. If LEFT=N and if LAST < TREE[LEFT], TREE[PTR]= TREE[LEFT] then: Set PTR=LEFT. 9. Set TREE[PTR]= LAST. 10. Return.
  • 9. HeapSort HEAPSORT(A, N) 1. Repeat for J=1 to N-1: Call INSHEAP(A, J, A[J+1]). [End of loop.] 1. Repeat while N>1: a) Call DELHEAP(A, N, ITEM). b) A[N+1]=ITEM. [End of loop.] 1. Exit.