SlideShare a Scribd company logo
1 of 17
Priority
Queues
~Dhruv Baswal~
B.Sc. Physical Science with
Computer Science
Roll no- 2238117
Semester 2
Content
• Introduction to Priority Queues in Data Structures
• Characteristics of a Priority queue
• Representation of Priority Queue.
• Different Implementation strategies
• What is Heap?
• Types of PriorityTypes of Priority Queue Queue
• Priority Queue Operations
• Applications of Priority Queue in Data Structure
Introduction to Priority Queue in Data Structures
 Priority Queue is an abstract data type that performs operations on data elements per their prio
rity. To understand it better, first analyze the real-life scenario of a priority queue.
 The hospital emergency queue is an ideal real-life example of a priority queue. In this queue of
patients, the patient with the most critical situation is the first in a queue, and the patient who
doesn’t need immediate medical attention will be the last. In this queue, the priority depends on
the medical condition of the patients.
The priority queue in data structure resembles the properties of the hospital emergency queue. Thus, it is
highly
used in sorting algorithms. It behaves similar toa linear queue except for the fact that each element has
some priority assigned to it.
The priority of elements determines the order of removal in a queue,
i.e., the element with higher priority will leave the queue first, whereas the element with the lowest priority
at last
Characteristics of Priority Queue
5
Priority queue in a data structure is an extension of a linear queue that possesses
the following properties:
•Every element has a certain priority assigned to it.
•Every element of this queue must be comparable. (similar data-type)
•It will delete the element with higher priority before the element with lower priority.
•If multiple elements have the same priority, it does their removal from the queue acc
ording to the FCFS principle.
6
Now, understand these properties with the help of an example. Consider you have to insert 7, 2, 45, 32,
and 12 in a priority queue. The element with the least value has the highest property. Thus, you should
maintain the lowest element at the front node.
The image shows how it maintains the priority during insertion in a queue. But, if you carry the N
comparisons for each insertion, time-complexity will become O(N^2)
 ”
7
Representation of Priority Queue
You can also implement
a priority queue using arrays, however, if you consider array implementation of the
priority queue, then inserting elements into the sorted array will cost you
O(n). In general, processing each element will further cost you O(n^2).
Because of this complexity, implementing a priority queue using arrays is not an ideal approach.
Hence, representation of the priority queue using a linked list is a better approach.
Consider a linked queue having 3 data elements 3, 17, 43, respectively. It arranges all these
elements according to priority. But, what if you have to insert a new node into the linked
queue consisting of value 2? Since 2 is smaller than the element at the front
(head) node, insertion from the front will be more efficient. Additionally, it will allow you to have O(1) time-
complexity during deletion.
8
The diagram back shows how it will insert the new node consisting of elements in
a linked queue. This particular scenario of insertion seems perfect as it doesn’t cost you more time. But what if the elemen
t is significantly larger than all the nodes of a queue?
For instance, say you want to insert a node consisting of element 45. Here, it will compare element 45 with each
element inside the queue. However, this insertion will cost you O(N). Representation of the linked queue below
displays how it will insert element 45 in a priority queue.
Different Implementation Strategies for Priority
Queue
Three approaches implement a priority queue in data structure with a complexity less than
O(n^2). They are shown in the table below:
Binary heap and binary tree provide almost similar complexities. These approaches cost you
O(logn) for insertion and deletion and O(1) for peek operation. But, which approach is the best to implement a priority queue?
You need to explore memory management in the case of both data structures. Since binary heap utilizes arrays, there is alw
ays a better locality of reference, and operations become more cache-
friendly. Whereas, the Binary search trees use pointers to implement front and rear nodes, which takes up more space in m
emory. Hence, building a self-balancing BST’s cost O(NlogN) where binary heap just costs you
O(n). These facts clarify that the Binary Heap is the best data structure for priority queue implementation.
What is Heap?
 A heap is a tree-like data structure that forms a complete tree and satisfies the heap invariant.
 The heap invariant states that, if A is a parent node of B, then A is ordered with respect to B for all nodes A and B in
a heap. This means a parent node’s value is always greater than or equal to the value of the child nodes for all nodes in
a heap. Or the value of the parent node is less than or equal to the value of the child node for all nodes in a heap.
 Additionally, there are two types of heap data structures, termed Max Heap and Min heap. The Max heap is a tree-
like structure in which the parent node’s value is greater than the value of the child node. The diagram given below represents
a binary max heap having the highest value at its root node.
 The Min heap is a treelike structure in which the parent node’s value is smaller than the value
of the child node.
 The tree diagram given below shows a binary heap tree having the smallest value at its root node.
Types of Priority Queue
 There are two types of priority queues based on the priority of elements.
• If the element with the smallest value has the highest priority, then that priority queue is called the min priority queue.
• If the element with a higher value has the highest priority, then that priority queue is known as the max priority queue.
• Furthermore, you can implement the min priority queue using
a min heap, whereas you can implement the max priority queue using a max heap.
14
Priority Queue Operations
•Inserting the Element in a Priority Queue:
Once you perform the new insertion, the new element will move to the empty space from top to bottom and left to right. Addi
tionally, if the element is not in the correct position, it will compare it with the parent node. Following that, if the element is no
t in proper order, then it swaps the elements. The swapping process continues until all the elements inside the queue are in
the correct positions.
In the example above, it inserted the new element
43 into the max heap representation of the priority queue. But because of this insertion, the order gets disturb
ed. To make sure that all the elements arrive in proper order,
a swapping operation takes place. This operation is also known as Heapify in the Heap data structure
 Deletion in Priority Queue:
As you know that in a max heap, the maximum element is the root node.
And it will remove the element which has maximum priority first.
Thus, you remove the root node from the queue.
This removal creates an empty slot, which will be further filled with new insertion.
Then, it compares the newly inserted element with all the elements inside the queue
to maintain the heap invariant.
Presentation title
In this example , it will delete the element at the root
node, and the heapify operation is performed to
restore the priority-based order.
Applications of Priority Queue in Data Structure
 The following are the applications of the priority queue in data structures:
• IP Routing to Find Open Shortest Path First: OSPF is a link-
state routing protocol that is used to find the best path between the source and the destination router. This protocol work
s on the principle of Dijkstra’s shortest path algorithm by using a priority queue to track an optimal route.
• Data Compression in WINZIP / GZIP: The Huffman encoding algorithm uses
a priority queue to maintain the codes for data contents. They store these codes in
a min heap, considering the size of codes as a parameter to decide the priority.
• Used in implementing Prim’s algorithm: Prim’s algorithm generates
a minimum spanning tree from an undirected, connected, and weighted graph. It uses
a min priority queue to maintain the order of elements for generating a minimum spanning tree.
• Used to perform the heap sort: When you provide an unsorted array to this algorithm, it converts it into
a sorted array. This algorithm uses a min priority queue to generate an order of elements.
• Load balancing and Interrupt handling: Load balancing fields the requests from the users and distributes them to differe
nt available servers. Whereas, interrupt handling is
a mechanism for handling interrupts in OS. The priority queue is used to manage requests, and it interrupts both functio
nalities simultaneously.
Presentation title

More Related Content

Similar to PriorityqDhruvBaswal.pptx

unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSAjunnubabu
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operationsbabuk110
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptxTekle12
 
ADS UNIT II -Priority queue.pptx
ADS UNIT II -Priority queue.pptxADS UNIT II -Priority queue.pptx
ADS UNIT II -Priority queue.pptxlaxman1216
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptxDr.Shweta
 
chapter-4-data-structure.pdf
chapter-4-data-structure.pdfchapter-4-data-structure.pdf
chapter-4-data-structure.pdfstudy material
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structuresDeepa Rani
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structuresSenthil Murugan
 
Stacks
StacksStacks
StacksAcad
 

Similar to PriorityqDhruvBaswal.pptx (20)

unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptx
 
ADS UNIT II -Priority queue.pptx
ADS UNIT II -Priority queue.pptxADS UNIT II -Priority queue.pptx
ADS UNIT II -Priority queue.pptx
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
chapter-4-data-structure.pdf
chapter-4-data-structure.pdfchapter-4-data-structure.pdf
chapter-4-data-structure.pdf
 
1.1 ADS data-structure.pdf
1.1 ADS data-structure.pdf1.1 ADS data-structure.pdf
1.1 ADS data-structure.pdf
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structures
 
Queues
Queues Queues
Queues
 
Sorting.pptx
Sorting.pptxSorting.pptx
Sorting.pptx
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
Stacks
StacksStacks
Stacks
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
Data structures
Data structuresData structures
Data structures
 

Recently uploaded

Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 

Recently uploaded (20)

Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 

PriorityqDhruvBaswal.pptx

  • 1. Priority Queues ~Dhruv Baswal~ B.Sc. Physical Science with Computer Science Roll no- 2238117 Semester 2
  • 2. Content • Introduction to Priority Queues in Data Structures • Characteristics of a Priority queue • Representation of Priority Queue. • Different Implementation strategies • What is Heap? • Types of PriorityTypes of Priority Queue Queue • Priority Queue Operations • Applications of Priority Queue in Data Structure
  • 3. Introduction to Priority Queue in Data Structures  Priority Queue is an abstract data type that performs operations on data elements per their prio rity. To understand it better, first analyze the real-life scenario of a priority queue.  The hospital emergency queue is an ideal real-life example of a priority queue. In this queue of patients, the patient with the most critical situation is the first in a queue, and the patient who doesn’t need immediate medical attention will be the last. In this queue, the priority depends on the medical condition of the patients.
  • 4. The priority queue in data structure resembles the properties of the hospital emergency queue. Thus, it is highly used in sorting algorithms. It behaves similar toa linear queue except for the fact that each element has some priority assigned to it. The priority of elements determines the order of removal in a queue, i.e., the element with higher priority will leave the queue first, whereas the element with the lowest priority at last
  • 5. Characteristics of Priority Queue 5 Priority queue in a data structure is an extension of a linear queue that possesses the following properties: •Every element has a certain priority assigned to it. •Every element of this queue must be comparable. (similar data-type) •It will delete the element with higher priority before the element with lower priority. •If multiple elements have the same priority, it does their removal from the queue acc ording to the FCFS principle.
  • 6. 6 Now, understand these properties with the help of an example. Consider you have to insert 7, 2, 45, 32, and 12 in a priority queue. The element with the least value has the highest property. Thus, you should maintain the lowest element at the front node. The image shows how it maintains the priority during insertion in a queue. But, if you carry the N comparisons for each insertion, time-complexity will become O(N^2)
  • 7.  ” 7 Representation of Priority Queue You can also implement a priority queue using arrays, however, if you consider array implementation of the priority queue, then inserting elements into the sorted array will cost you O(n). In general, processing each element will further cost you O(n^2). Because of this complexity, implementing a priority queue using arrays is not an ideal approach. Hence, representation of the priority queue using a linked list is a better approach. Consider a linked queue having 3 data elements 3, 17, 43, respectively. It arranges all these elements according to priority. But, what if you have to insert a new node into the linked queue consisting of value 2? Since 2 is smaller than the element at the front (head) node, insertion from the front will be more efficient. Additionally, it will allow you to have O(1) time- complexity during deletion.
  • 8. 8
  • 9. The diagram back shows how it will insert the new node consisting of elements in a linked queue. This particular scenario of insertion seems perfect as it doesn’t cost you more time. But what if the elemen t is significantly larger than all the nodes of a queue? For instance, say you want to insert a node consisting of element 45. Here, it will compare element 45 with each element inside the queue. However, this insertion will cost you O(N). Representation of the linked queue below displays how it will insert element 45 in a priority queue.
  • 10. Different Implementation Strategies for Priority Queue Three approaches implement a priority queue in data structure with a complexity less than O(n^2). They are shown in the table below:
  • 11. Binary heap and binary tree provide almost similar complexities. These approaches cost you O(logn) for insertion and deletion and O(1) for peek operation. But, which approach is the best to implement a priority queue? You need to explore memory management in the case of both data structures. Since binary heap utilizes arrays, there is alw ays a better locality of reference, and operations become more cache- friendly. Whereas, the Binary search trees use pointers to implement front and rear nodes, which takes up more space in m emory. Hence, building a self-balancing BST’s cost O(NlogN) where binary heap just costs you O(n). These facts clarify that the Binary Heap is the best data structure for priority queue implementation.
  • 12. What is Heap?  A heap is a tree-like data structure that forms a complete tree and satisfies the heap invariant.  The heap invariant states that, if A is a parent node of B, then A is ordered with respect to B for all nodes A and B in a heap. This means a parent node’s value is always greater than or equal to the value of the child nodes for all nodes in a heap. Or the value of the parent node is less than or equal to the value of the child node for all nodes in a heap.  Additionally, there are two types of heap data structures, termed Max Heap and Min heap. The Max heap is a tree- like structure in which the parent node’s value is greater than the value of the child node. The diagram given below represents a binary max heap having the highest value at its root node.
  • 13.  The Min heap is a treelike structure in which the parent node’s value is smaller than the value of the child node.  The tree diagram given below shows a binary heap tree having the smallest value at its root node.
  • 14. Types of Priority Queue  There are two types of priority queues based on the priority of elements. • If the element with the smallest value has the highest priority, then that priority queue is called the min priority queue. • If the element with a higher value has the highest priority, then that priority queue is known as the max priority queue. • Furthermore, you can implement the min priority queue using a min heap, whereas you can implement the max priority queue using a max heap. 14 Priority Queue Operations •Inserting the Element in a Priority Queue: Once you perform the new insertion, the new element will move to the empty space from top to bottom and left to right. Addi tionally, if the element is not in the correct position, it will compare it with the parent node. Following that, if the element is no t in proper order, then it swaps the elements. The swapping process continues until all the elements inside the queue are in the correct positions.
  • 15. In the example above, it inserted the new element 43 into the max heap representation of the priority queue. But because of this insertion, the order gets disturb ed. To make sure that all the elements arrive in proper order, a swapping operation takes place. This operation is also known as Heapify in the Heap data structure
  • 16.  Deletion in Priority Queue: As you know that in a max heap, the maximum element is the root node. And it will remove the element which has maximum priority first. Thus, you remove the root node from the queue. This removal creates an empty slot, which will be further filled with new insertion. Then, it compares the newly inserted element with all the elements inside the queue to maintain the heap invariant. Presentation title In this example , it will delete the element at the root node, and the heapify operation is performed to restore the priority-based order.
  • 17. Applications of Priority Queue in Data Structure  The following are the applications of the priority queue in data structures: • IP Routing to Find Open Shortest Path First: OSPF is a link- state routing protocol that is used to find the best path between the source and the destination router. This protocol work s on the principle of Dijkstra’s shortest path algorithm by using a priority queue to track an optimal route. • Data Compression in WINZIP / GZIP: The Huffman encoding algorithm uses a priority queue to maintain the codes for data contents. They store these codes in a min heap, considering the size of codes as a parameter to decide the priority. • Used in implementing Prim’s algorithm: Prim’s algorithm generates a minimum spanning tree from an undirected, connected, and weighted graph. It uses a min priority queue to maintain the order of elements for generating a minimum spanning tree. • Used to perform the heap sort: When you provide an unsorted array to this algorithm, it converts it into a sorted array. This algorithm uses a min priority queue to generate an order of elements. • Load balancing and Interrupt handling: Load balancing fields the requests from the users and distributes them to differe nt available servers. Whereas, interrupt handling is a mechanism for handling interrupts in OS. The priority queue is used to manage requests, and it interrupts both functio nalities simultaneously. Presentation title