SlideShare une entreprise Scribd logo
1  sur  21
1
Priority Queues
Supports the following operations.
 Insert element x.
 Return min element.
 Return and delete minimum element.
Applications.
 Dijkstra's shortest path algorithm.
 Prim's MST algorithm.
 Event-driven simulation.
 Huffman encoding.
 Heapsort.
 . . .
2
Binary Heap: Definition
Binary heap.
 Structure property :Almost complete binary tree.
– filled on all levels, except last, where filled from left to right
 Heap ordered property: Min-heap ordered.
– every child greater than (or equal to) parent
06
14
78 18
81 77
91
45
53
47
64
84 99
83
3
Binary Heap: Properties
Properties.
 Min element is in root.
 Heap with N elements has height = log2 N.
06
14
78 18
81 77
91
45
53
47
64
84 99
83
N = 14
Height = 3
4
Binary Heaps: Array Implementation
Implementing binary heaps.
 Since it has complete tree property we can use an array: no need for
explicit parent or child pointers.
– Parent(i) = i/2
– Left(i) = 2i
– Right(i) = 2i + 1 due to these operations i must be >0;
06
14
78 18
81 77
91
45
53
47
64
84 99
83
1
2 3
4 5 6 7
8 9 10 11 12 13 14
5
Binary Heap: Insertion
Insert element x into heap.
 Insert into next available slot in the array.
 Bubble up until it's heap ordered.
– Peter principle: nodes rise to level of incompetence
06
14
78 18
81 77
91
45
53
47
64
84 99
83 42 next free slot
6
Binary Heap: Insertion
Insert element x into heap.
 Insert into next available slot.
 Bubble up until it's heap ordered.
– Peter principle: nodes rise to level of incompetence
06
14
78 18
81 77
91
45
53
47
64
84 99
83 42
42
swap with parent
7
Binary Heap: Insertion
Insert element x into heap.
 Insert into next available slot.
 Bubble up until it's heap ordered.
06
14
78 18
81 77
91
45
42
47
64
84 99
83 42
53
swap with parent
8
Binary Heap: Insertion
Insert element x into heap.
 Insert into next available slot.
 Bubble up until it's heap ordered is satisfied. (percolate up)
– Peter principle: nodes rise to level of incompetence
 O(log N) operations.
06
14
78 18
81 77
91
42
45
47
64
84 99
83 53
stop: heap ordered
9
Binary Heap: Insertion
1. First increase the heap size by 1, so that it can store
the new element.
2. Insert the new element at the end of the Heap.
3. This newly inserted element may violate the heap
order property of Heap.
4. So, in order to keep the properties of Heap, percolate-
up this newly inserted element following a bottom-up
approach.
Percolate up procedure/ Heapify operation
i) Compare the value of this child node inserted with
its parent.
ii) If the value of parent is less than child, then swap them.
iii) Repeat step i) & ii) until Heap order property is satisfied
Max heap- Insert 99
10
45 36
27 21 18 21
11
54
99
Insert 99
11
Insert 99
12
54 36
45 21 18 21
11
99
27
13
Binary Heap: Insertion
void insert(MinHeap heap, int x)
{ if ( hsize<capacity) //check heap full
{ hsize=hsize+1;
int ctnode=hsize;
While(ctnode!=1 && heap[ctnode/2]>x)
{//shiftup
heap[ctnode]=heap[ctnode/2];
//move the empty hole up
ctnode=ctnode/2;
}
heap[ctnode]=x;
} }
14
Binary Heap: Delete Min
Delete minimum element from heap.
 Exchange root with rightmost leaf (last element in array).
 Bubble root down until it's heap ordered is satisfied. (percolate
down)
– power struggle principle: better subordinate is promoted
06
14
78 18
81 77
91
42
45
47
64
84 99
83 53
15
Binary Heap: Delete Min
Delete minimum element from heap.
 Exchange root with rightmost leaf.
 Bubble root down until it's heap ordered.
– power struggle principle: better subordinate is promoted
53
14
78 18
81 77
91
42
45
47
64
84 99
83 06
16
Binary Heap: Delete Min
Delete minimum element from heap.
 Exchange root with rightmost leaf.
 Bubble root down until it's heap ordered.
– power struggle principle: better subordinate is promoted
53
14
78 18
81 77
91
42
45
47
64
84 99
83
exchange with left child
17
Binary Heap: Delete Min
Delete minimum element from heap.
 Exchange root with rightmost leaf.
 Bubble root down until it's heap ordered.
– power struggle principle: better subordinate is promoted
14
53
78 18
81 77
91
42
45
47
64
84 99
83
exchange with right child
18
Binary Heap: Delete Min
Delete minimum element from heap.
 Exchange root with rightmost leaf (last node stored in array).
 Bubble root down until it's heap ordered.
 O(log N) operations.
14
18
78 53
81 77
91
42
45
47
64
84 99
83
stop: heap ordered
19
Binary Heap: Delete min
1. Replace the root or element to be deleted by the
last element in the last level.
2. Delete the last element from the Heap.
3. Since, the last element is now placed at the
position of the root node. So, it may not follow
the heap order property.
4. Therefore, Percolate-down the last node placed
at the position of root.
Percolate down procedure/ Heapify operation
i) Compare the value of this parent node with its minimum
child.
ii) If the value of parent is greater than minimum child,
then swap them.
iii) Repeat step i) & ii) until Heap order property is satisfied
20
Binary Heap: Delete min
void delete min(minheap heap)
{
if ( hsize>0) //check if empty
{ int lastelement=heap[hsize];
hsize=hsize-1;
//shift down the hole
int ctnode=1;
int child=2;
21
Binary Heap: Delete min
While(child<hsize)
{
if ( (child<hsize) && heap[child]>heap[child+1]
child=child+1; //right child is min child
//else left child is the minimum child
if (lastelement <= heap[child])
break;
heap[ctnode]=heap[child];
ctnode=child;
child=ctnode*2;
}
heap[ctnode]=lastelement;
}
}

Contenu connexe

Similaire à Binary heap (Priority Queue).ppt

Heap Hand note
Heap Hand noteHeap Hand note
Heap Hand noteAbdur Rouf
 
Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxMohammed472103
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptxZainiXh
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 
Unit-5 Advanced tree zxcppt
Unit-5 Advanced tree                     zxcpptUnit-5 Advanced tree                     zxcppt
Unit-5 Advanced tree zxcpptDhruvilSTATUS
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortReetesh Gupta
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmLearning Courses Online
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examplesBst Ali
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.pptAmitShou
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 

Similaire à Binary heap (Priority Queue).ppt (20)

Heap sort
Heap sortHeap sort
Heap sort
 
Heap Hand note
Heap Hand noteHeap Hand note
Heap Hand note
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Leftlist Heap-1.pdf
Leftlist Heap-1.pdfLeftlist Heap-1.pdf
Leftlist Heap-1.pdf
 
Priority queue
Priority queuePriority queue
Priority queue
 
Lec23
Lec23Lec23
Lec23
 
Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptx
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
HeapSort
HeapSortHeapSort
HeapSort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Unit-5 Advanced tree zxcppt
Unit-5 Advanced tree                     zxcpptUnit-5 Advanced tree                     zxcppt
Unit-5 Advanced tree zxcppt
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptx
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.ppt
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 

Dernier

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
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 FellowsMebane Rash
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 

Dernier (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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
 
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
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 

Binary heap (Priority Queue).ppt

  • 1. 1 Priority Queues Supports the following operations.  Insert element x.  Return min element.  Return and delete minimum element. Applications.  Dijkstra's shortest path algorithm.  Prim's MST algorithm.  Event-driven simulation.  Huffman encoding.  Heapsort.  . . .
  • 2. 2 Binary Heap: Definition Binary heap.  Structure property :Almost complete binary tree. – filled on all levels, except last, where filled from left to right  Heap ordered property: Min-heap ordered. – every child greater than (or equal to) parent 06 14 78 18 81 77 91 45 53 47 64 84 99 83
  • 3. 3 Binary Heap: Properties Properties.  Min element is in root.  Heap with N elements has height = log2 N. 06 14 78 18 81 77 91 45 53 47 64 84 99 83 N = 14 Height = 3
  • 4. 4 Binary Heaps: Array Implementation Implementing binary heaps.  Since it has complete tree property we can use an array: no need for explicit parent or child pointers. – Parent(i) = i/2 – Left(i) = 2i – Right(i) = 2i + 1 due to these operations i must be >0; 06 14 78 18 81 77 91 45 53 47 64 84 99 83 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  • 5. 5 Binary Heap: Insertion Insert element x into heap.  Insert into next available slot in the array.  Bubble up until it's heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 77 91 45 53 47 64 84 99 83 42 next free slot
  • 6. 6 Binary Heap: Insertion Insert element x into heap.  Insert into next available slot.  Bubble up until it's heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 77 91 45 53 47 64 84 99 83 42 42 swap with parent
  • 7. 7 Binary Heap: Insertion Insert element x into heap.  Insert into next available slot.  Bubble up until it's heap ordered. 06 14 78 18 81 77 91 45 42 47 64 84 99 83 42 53 swap with parent
  • 8. 8 Binary Heap: Insertion Insert element x into heap.  Insert into next available slot.  Bubble up until it's heap ordered is satisfied. (percolate up) – Peter principle: nodes rise to level of incompetence  O(log N) operations. 06 14 78 18 81 77 91 42 45 47 64 84 99 83 53 stop: heap ordered
  • 9. 9 Binary Heap: Insertion 1. First increase the heap size by 1, so that it can store the new element. 2. Insert the new element at the end of the Heap. 3. This newly inserted element may violate the heap order property of Heap. 4. So, in order to keep the properties of Heap, percolate- up this newly inserted element following a bottom-up approach. Percolate up procedure/ Heapify operation i) Compare the value of this child node inserted with its parent. ii) If the value of parent is less than child, then swap them. iii) Repeat step i) & ii) until Heap order property is satisfied
  • 10. Max heap- Insert 99 10 45 36 27 21 18 21 11 54 99
  • 12. Insert 99 12 54 36 45 21 18 21 11 99 27
  • 13. 13 Binary Heap: Insertion void insert(MinHeap heap, int x) { if ( hsize<capacity) //check heap full { hsize=hsize+1; int ctnode=hsize; While(ctnode!=1 && heap[ctnode/2]>x) {//shiftup heap[ctnode]=heap[ctnode/2]; //move the empty hole up ctnode=ctnode/2; } heap[ctnode]=x; } }
  • 14. 14 Binary Heap: Delete Min Delete minimum element from heap.  Exchange root with rightmost leaf (last element in array).  Bubble root down until it's heap ordered is satisfied. (percolate down) – power struggle principle: better subordinate is promoted 06 14 78 18 81 77 91 42 45 47 64 84 99 83 53
  • 15. 15 Binary Heap: Delete Min Delete minimum element from heap.  Exchange root with rightmost leaf.  Bubble root down until it's heap ordered. – power struggle principle: better subordinate is promoted 53 14 78 18 81 77 91 42 45 47 64 84 99 83 06
  • 16. 16 Binary Heap: Delete Min Delete minimum element from heap.  Exchange root with rightmost leaf.  Bubble root down until it's heap ordered. – power struggle principle: better subordinate is promoted 53 14 78 18 81 77 91 42 45 47 64 84 99 83 exchange with left child
  • 17. 17 Binary Heap: Delete Min Delete minimum element from heap.  Exchange root with rightmost leaf.  Bubble root down until it's heap ordered. – power struggle principle: better subordinate is promoted 14 53 78 18 81 77 91 42 45 47 64 84 99 83 exchange with right child
  • 18. 18 Binary Heap: Delete Min Delete minimum element from heap.  Exchange root with rightmost leaf (last node stored in array).  Bubble root down until it's heap ordered.  O(log N) operations. 14 18 78 53 81 77 91 42 45 47 64 84 99 83 stop: heap ordered
  • 19. 19 Binary Heap: Delete min 1. Replace the root or element to be deleted by the last element in the last level. 2. Delete the last element from the Heap. 3. Since, the last element is now placed at the position of the root node. So, it may not follow the heap order property. 4. Therefore, Percolate-down the last node placed at the position of root. Percolate down procedure/ Heapify operation i) Compare the value of this parent node with its minimum child. ii) If the value of parent is greater than minimum child, then swap them. iii) Repeat step i) & ii) until Heap order property is satisfied
  • 20. 20 Binary Heap: Delete min void delete min(minheap heap) { if ( hsize>0) //check if empty { int lastelement=heap[hsize]; hsize=hsize-1; //shift down the hole int ctnode=1; int child=2;
  • 21. 21 Binary Heap: Delete min While(child<hsize) { if ( (child<hsize) && heap[child]>heap[child+1] child=child+1; //right child is min child //else left child is the minimum child if (lastelement <= heap[child]) break; heap[ctnode]=heap[child]; ctnode=child; child=ctnode*2; } heap[ctnode]=lastelement; } }

Notes de l'éditeur

  1. Event driven simulation: generate random inter-arrival times between customers, generate random processing times per customer, Advance clock to next event, skipping intervening ticks
  2. parent and children formula require only simple bit twiddling operations