SlideShare a Scribd company logo
1 of 15
HEAPS
Introduction
โ€ข A heap is a specialized tree-based data structure that satisfies the
heap property which states that
โ€ข If B is a child of A, then key(A) โ‰ฅ key(B).
Or
โ€ข If B is a child of A, then key(A) โ‰ค key(B).
โ€ข This implies that the root node has the highest key value in the
heap. Such a heap is commonly known as a max-heap.
(Alternatively, if the root has the lowest key value, then the
resultant heap is called a min-heap.)
BINARY HEAP
A binary heap is a complete binary tree in which every node satisfies
the heap property which states that
โ€ข If B is a child of A, then key(A) โ‰ฅ key(B).
Or
โ€ข If B is a child of A, then key(A) โ‰ค key(B).
BINARY HEAP
MIN HEAP
A binary heap with elements at every node being either less than or
equal to the element at its left and the right child. Thus the root has
the lowest key value. Such a heap is called min-heap
BINARY HEAP
MAX HEAP
A binary heap with elements at every node being either greater than
or equal to the element at its left and the right child. Thus the root
has the highest key value. Such a heap is called max-heap
Insertion in a Binary Heap
โ€ข Consider a max heap H having n elements. Inserting a new value
into the heap is done in two major steps.
โ€ข Add the new value at the bottom of H in such a way that H is still a
complete binary tree but not necessarily a heap.
โ€ข Let the new value rise to its appropriate place in H so that H now
becomes a heap as well.
โ€ข To do this, compare the new value with its parent; to check if they
are in the correct order. If they are in the correct order, then the
procedure halts else, the new value and its parentโ€™s value is
swapped and step 2 is repeated.
Insertion in a Binary Heap
54
45 36
27 21 18 21
11
45 36
27 21 18 21
11
54
99
45 36
99 21 18 21
11
54
27
โ€ข Consider the heap given below and insert 99 in it
Insertion in a Binary Heap
99 36
45 21 18 21
11
54
27
54
45 21 18 21
11
99
27
36
23 12
44 33 19 28
87
11
99
Insertion in a Binary Heap
11 12
44
23
19 28
87
9
99 33
Insertion in a Binary Heap
Algorithm to insert a value in the heap
Step 1: [Add the new value and set its POS]
SET N = N + 1, POS = N
Step 2: SET HEAP[N] = VAL
Step 3: [Find appropriate location of VAL]
Repeat Steps 4 and 5 while POS < 0
Step 4: SET PAR = POS/2
Step 5: IF HEAP[POS] <= HEAP[PAR], then
Goto Step 6.
ELSE
SWAP HEAP[POS], HEAP[PAR]
POS = PAR
[END OF IF]
[END OF LOOP]
Step 6: Return
Deletion in a Binary Heap
(DeleteMin / DeleteMax)
โ€ข Consider a max heap H having n elements. An element is always
deleted from the root of the heap. So, deleting an element from
the heap is done in two major steps.
โ€ข Replace the root nodeโ€™s value with the last nodeโ€™s value so that H
is still a complete binary tree but not necessarily a heap.
โ€ข Delete the last node.
โ€ข Sink down the new root nodeโ€™s value so that H satisfies the heap
property. In this step, interchange the root nodeโ€™s value with its
child nodeโ€™s value (whichever is largest among its children).
Deletion in a Binary Heap
45 36
27 29 18 21
11
54
45 36
27 29 18 21
11
11 36
27 29 18 21
45
Consider the heap H given below and delete the root nodeโ€™s value.
29 36
27 11 18 21
45
Deletion in a Binary Heap
Algorithm to delete the root element from the heap- DELETE_HEAP(HEAP, N, VAL)
Step 1: [ Remove the last node from the heap]
SET LAST = HEAP[N], SET N = N โ€“ 1
Step 2: [Initialization]
SET PTR = 0, LEFT = 1, RIGHT = 2
Step 3: SET HEAP[PTR] = LAST
Step 4: Repeat Steps 5 to 7 while LEFT <= N
Step 5: IF HEAP{PTR] >= HEAP[LEFT] AND HEAP[PTR] >= HEAP[RIGHT], then
Go to Step
[END OF IF]
Step 6: IF HEAP[RIGHT] <= HEAP[LEFT], then
SWAP HEAP[PTR], HEAP[LEFT]
SET PTR = LEFT
ELSE
SWAP HEAP[PTR], HEAP[RIGHT]
SET PTR = RIGHT
[END OF IF]
Step 7: SET LEFT = 2 * PTR and RIGHT = LEFT + 1
[END OF LOOP]
Step 8: Return
5-heap.ppt
5-heap.ppt

More Related Content

Similar to 5-heap.ppt

Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortReetesh Gupta
ย 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptxFazlullah28
ย 
Sorting
SortingSorting
Sortingvatsaanadi
ย 
Lecture3
Lecture3Lecture3
Lecture3Ali Baba
ย 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxIsrar63
ย 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of HeapMeghaj Mallick
ย 
Heapsort
HeapsortHeapsort
Heapsortimishtiaq
ย 
Binary heap (Priority Queue).ppt
Binary heap (Priority Queue).pptBinary heap (Priority Queue).ppt
Binary heap (Priority Queue).pptMano Arun
ย 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialAymericTaylor
ย 
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxLesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxCrisannJasmin
ย 
Ch15 Heap
Ch15 HeapCh15 Heap
Ch15 Heapleminhvuong
ย 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort pptMariam Saeed
ย 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxhardmarcelia
ย 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptAmitShou
ย 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptHemantSharma134028
ย 

Similar to 5-heap.ppt (20)

Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
ย 
Heap
HeapHeap
Heap
ย 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptx
ย 
Sorting
SortingSorting
Sorting
ย 
Lecture3
Lecture3Lecture3
Lecture3
ย 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
ย 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
ย 
Heap sort
Heap sortHeap sort
Heap sort
ย 
Heap sort
Heap sort Heap sort
Heap sort
ย 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
ย 
Heapsort
HeapsortHeapsort
Heapsort
ย 
Binary heap (Priority Queue).ppt
Binary heap (Priority Queue).pptBinary heap (Priority Queue).ppt
Binary heap (Priority Queue).ppt
ย 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational material
ย 
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxLesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
ย 
Ch15 Heap
Ch15 HeapCh15 Heap
Ch15 Heap
ย 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
ย 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
ย 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
ย 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
ย 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
ย 

Recently uploaded

Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
ย 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
ย 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
ย 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
ย 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
ย 
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night StandCall Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Standamitlee9823
ย 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
ย 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsArindam Chakraborty, Ph.D., P.E. (CA, TX)
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
ย 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
ย 
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
ย 

Recently uploaded (20)

Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
ย 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
ย 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
ย 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
ย 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
ย 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
ย 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night StandCall Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
ย 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
ย 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
ย 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
ย 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
ย 

5-heap.ppt

  • 2. Introduction โ€ข A heap is a specialized tree-based data structure that satisfies the heap property which states that โ€ข If B is a child of A, then key(A) โ‰ฅ key(B). Or โ€ข If B is a child of A, then key(A) โ‰ค key(B). โ€ข This implies that the root node has the highest key value in the heap. Such a heap is commonly known as a max-heap. (Alternatively, if the root has the lowest key value, then the resultant heap is called a min-heap.)
  • 3. BINARY HEAP A binary heap is a complete binary tree in which every node satisfies the heap property which states that โ€ข If B is a child of A, then key(A) โ‰ฅ key(B). Or โ€ข If B is a child of A, then key(A) โ‰ค key(B).
  • 4. BINARY HEAP MIN HEAP A binary heap with elements at every node being either less than or equal to the element at its left and the right child. Thus the root has the lowest key value. Such a heap is called min-heap
  • 5. BINARY HEAP MAX HEAP A binary heap with elements at every node being either greater than or equal to the element at its left and the right child. Thus the root has the highest key value. Such a heap is called max-heap
  • 6. Insertion in a Binary Heap โ€ข Consider a max heap H having n elements. Inserting a new value into the heap is done in two major steps. โ€ข Add the new value at the bottom of H in such a way that H is still a complete binary tree but not necessarily a heap. โ€ข Let the new value rise to its appropriate place in H so that H now becomes a heap as well. โ€ข To do this, compare the new value with its parent; to check if they are in the correct order. If they are in the correct order, then the procedure halts else, the new value and its parentโ€™s value is swapped and step 2 is repeated.
  • 7. Insertion in a Binary Heap 54 45 36 27 21 18 21 11 45 36 27 21 18 21 11 54 99 45 36 99 21 18 21 11 54 27 โ€ข Consider the heap given below and insert 99 in it
  • 8. Insertion in a Binary Heap 99 36 45 21 18 21 11 54 27 54 45 21 18 21 11 99 27 36
  • 9. 23 12 44 33 19 28 87 11 99 Insertion in a Binary Heap 11 12 44 23 19 28 87 9 99 33
  • 10. Insertion in a Binary Heap Algorithm to insert a value in the heap Step 1: [Add the new value and set its POS] SET N = N + 1, POS = N Step 2: SET HEAP[N] = VAL Step 3: [Find appropriate location of VAL] Repeat Steps 4 and 5 while POS < 0 Step 4: SET PAR = POS/2 Step 5: IF HEAP[POS] <= HEAP[PAR], then Goto Step 6. ELSE SWAP HEAP[POS], HEAP[PAR] POS = PAR [END OF IF] [END OF LOOP] Step 6: Return
  • 11. Deletion in a Binary Heap (DeleteMin / DeleteMax) โ€ข Consider a max heap H having n elements. An element is always deleted from the root of the heap. So, deleting an element from the heap is done in two major steps. โ€ข Replace the root nodeโ€™s value with the last nodeโ€™s value so that H is still a complete binary tree but not necessarily a heap. โ€ข Delete the last node. โ€ข Sink down the new root nodeโ€™s value so that H satisfies the heap property. In this step, interchange the root nodeโ€™s value with its child nodeโ€™s value (whichever is largest among its children).
  • 12. Deletion in a Binary Heap 45 36 27 29 18 21 11 54 45 36 27 29 18 21 11 11 36 27 29 18 21 45 Consider the heap H given below and delete the root nodeโ€™s value. 29 36 27 11 18 21 45
  • 13. Deletion in a Binary Heap Algorithm to delete the root element from the heap- DELETE_HEAP(HEAP, N, VAL) Step 1: [ Remove the last node from the heap] SET LAST = HEAP[N], SET N = N โ€“ 1 Step 2: [Initialization] SET PTR = 0, LEFT = 1, RIGHT = 2 Step 3: SET HEAP[PTR] = LAST Step 4: Repeat Steps 5 to 7 while LEFT <= N Step 5: IF HEAP{PTR] >= HEAP[LEFT] AND HEAP[PTR] >= HEAP[RIGHT], then Go to Step [END OF IF] Step 6: IF HEAP[RIGHT] <= HEAP[LEFT], then SWAP HEAP[PTR], HEAP[LEFT] SET PTR = LEFT ELSE SWAP HEAP[PTR], HEAP[RIGHT] SET PTR = RIGHT [END OF IF] Step 7: SET LEFT = 2 * PTR and RIGHT = LEFT + 1 [END OF LOOP] Step 8: Return