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

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

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
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
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
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
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
 
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
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 

Recently uploaded (20)

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
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
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
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
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
 
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
 
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
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
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
 
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
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 

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