SlideShare a Scribd company logo
QUEUE
CHAPTER 4:
BBP 25203: DATA STRUCTURE AND ALGORITHM
SUBTOPICS
4.1 Queues
4.2 Priority Queues
4.3 Heap
QUEUES
Queues are data structures that, like the stack, have restrictions on where you can add and remove
elements. To understand a queue, think of a cafeteria line: the person at the front is served first, and
people are added to the line at the back. Thus, the first person in line is served first, and the last
person is served last. This can be abbreviated to First In, First Out (FIFO).
In a real-world analogy, we can imagine a bus queue where the passengers wait for the bus in a queue
or a line. The first passenger in the line enters the bus first as that passenger happens to be the one
who had come first.
QUEUE
QUEUE
We have two ends i.e. “front” and “rear” of the queue. When the queue is empty, then both the
pointers are set to -1.
The “rear” end pointer is the place from where the elements are inserted in the queue. The
operation of adding /inserting elements in the queue is called “enqueue”.
The “front” end pointer is the place from where the elements are removed from the queue. The
operation to remove/delete elements from the queue is called “dequeue”.
When the rear pointer value is size-1, then we say that the queue is full. When the front is null,
then the queue is empty.
QUEUE
Basic Operations
The queue data structure includes the following operations:
•EnQueue: Adds an item to the queue. Addition of an item to the queue is always done at
the rear of the queue.
•DeQueue: Removes an item from the queue. An item is removed or de-queued always
from the front of the queue.
•isEmpty: Checks if the queue is empty.
•isFull: Checks if the queue is full.
•peek: Gets an element at the front of the queue without removing it.
QUEUE
Enqueue
In this process, the following steps are performed:
•Check if the queue is full.
•If full, produce overflow error and exit.
•Else, increment ‘rear’.
•Add an element to the location pointed by ‘rear’.
•Return success.
Dequeue
Dequeue operation consists of the following steps:
•Check if the queue is empty.
•If empty, display an underflow error and exit.
•Else, the access element is pointed out by ‘front’.
•Increment the ‘front’ to point to the next accessible data.
•Return success.
QUEUE
QUEUE
Example of Queue in C++
PRIORITY QUEUE
PRIORITY QUEUE
What is a Priority Queue?
A priority queue in c++ is a type of container adapter, which processes only the highest
priority element, i.e. the first element will be the maximum of all elements in the queue, and
elements are in decreasing order.
Difference between a queue and priority queue :
•Priority Queue container processes the element with the highest priority, whereas no
priority exists in a queue.
•Queue follows First-in-First-out (FIFO) rule, but in the priority queue highest priority
element will be deleted first.
•If more than one element exists with the same priority, then, in this case, the order of
queue will be taken.
PRIORITY QUEUE
Methods of Priority Queue :
Following are the methods of Priority Queue :
1.empty() – This method checks whether the priority_queue container is empty or not. If it is
empty, return true, else false. It does not take any parameter.
syntax : p1.empty() // p1 is priority_queue object
2.size() – This method gives the number of elements in the priority queue container. It
returns the size in an integer. It does not take any parameter.
syntax : p2.size() // p2 is priority_queue object
3.push() – This method inserts the element into the queue. Firstly, the element is added to
the end of the queue, and simultaneously elements reorder themselves with priority. It
takes value in the parameter.
syntax : p3.push(value) //value to be inserted
PRIORITY QUEUE
4.pop() – This method delete the top element (highest priority) from the priority_queue. It
does not take any parameter.
syntax : p3.pop() // p3 is priority_queue object
5.top() – This method gives the top element from the priority queue container. It does not
take any parameter.
syntax : p3.top()
6.swap() – This method swaps the elements of a priority_queue with another priority_queue
of the same size and type. It takes the priority queue in a parameter whose values need to
be swapped.
syntax : p3.swap(p1)
7.emplace() – This method adds a new element in a container at the top of the priority
queue. It takes value in a parameter.
syntax : p3.emplace(value)
PRIORITY QUEUE
Inserting elements in a Priority Queue – :
Live Demo!!
PRIORITY QUEUE
Accessing elements in a Priority Queue :
Live Demo!!
PRIORITY QUEUE
Deleting elements in a Priority Queue :
Live Demo!!
HEAP
HEAP
Heap data structure is a complete binary tree that satisfies the heap property. It
is also called as a binary heap.
A complete binary tree is a special binary tree in which
•every level, except possibly the last, is filled
•all the nodes are as far left as possible
HEAP
Heap Property is the property of a node in which
•(for max heap) key of each node is always greater than its child node/s and the
key of the root node is the largest among all other nodes;
HEAP
•(for min heap) key of each node is always smaller than the child node/s and the
key of the root node is the smallest among all other nodes.
HEAP
Heap Operations
Some of the important operations performed on a heap are described below along with their
algorithms.
Heapify
Heapify is the process of creating a heap data structure from a binary tree. It is used to create
a Min-Heap or a Max-Heap.
Let the input array be
HEAP
Create a complete binary tree from the array
HEAP
Start from the first index of non-leaf node whose index is given by n/2 - 1
HEAP
Repeat steps until the subtrees are also heapified.
For Min-Heap, both leftChild and rightChild must be smaller than the parent for all nodes.
HEAP
Insert element to the heap
1. Insert the new element at the end of the tree.
HEAP
Insert element to the heap
2. Heapify the tree.
HEAP
Delete Element from Heap
1. Select the element
HEAP
Delete Element from Heap
2. Swap the element to the last leaf node
HEAP
Delete Element from Heap
3. Remove the last element.
HEAP
Delete Element from Heap
4. Heapify the tree
Live Demo!!
THANK
YOU

More Related Content

PDF
5-Queue-----------------------------in c++
PDF
Priority Queue using HEAP. ( Using C in data structure )
PPTX
Priority queue
PPTX
DS UNIT2QUEUES.pptx
PDF
Basic Terminologies of Queue...Basic operations on Queue
PPT
GAC DS Priority Queue Presentation 2022.ppt
PPTX
My lectures circular queue
PPTX
PriorityqDhruvBaswal.pptx
5-Queue-----------------------------in c++
Priority Queue using HEAP. ( Using C in data structure )
Priority queue
DS UNIT2QUEUES.pptx
Basic Terminologies of Queue...Basic operations on Queue
GAC DS Priority Queue Presentation 2022.ppt
My lectures circular queue
PriorityqDhruvBaswal.pptx

Similar to CHAPTER 4 Learning QUEUE data structure.pptx (20)

PDF
Priorty queue
PPTX
queue.pptx
PPTX
DS ppt1.pptx.c programing. Engineering. Data structure
PDF
Chapter11
PPTX
The presention is about the queue data structure
PPTX
Unit ii linear data structures
PPT
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
PDF
LEC4-DS ALGO.pdf
PPTX
ADS UNIT II -Priority queue.pptx
PPTX
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
PPT
linked list in c++
PPTX
Queue ppt
PPTX
GROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffff
PPT
The Queue in Data structure and algorithm
PPTX
Queue types of queue and algorithms and queue
PPTX
queueppt-191018053228 (1).pptx
PPT
Queue in Data Structure
PPTX
Data structure
Priorty queue
queue.pptx
DS ppt1.pptx.c programing. Engineering. Data structure
Chapter11
The presention is about the queue data structure
Unit ii linear data structures
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
LEC4-DS ALGO.pdf
ADS UNIT II -Priority queue.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
linked list in c++
Queue ppt
GROUP2.pptxfdfffffffffffffffffffffffffffffffffffffffffff
The Queue in Data structure and algorithm
Queue types of queue and algorithms and queue
queueppt-191018053228 (1).pptx
Queue in Data Structure
Data structure
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Structure & Organelles in detailed.
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Lesson notes of climatology university.
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Computing-Curriculum for Schools in Ghana
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Classroom Observation Tools for Teachers
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
Microbial diseases, their pathogenesis and prophylaxis
Sports Quiz easy sports quiz sports quiz
Cell Structure & Organelles in detailed.
VCE English Exam - Section C Student Revision Booklet
Lesson notes of climatology university.
PPH.pptx obstetrics and gynecology in nursing
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Computing-Curriculum for Schools in Ghana
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Institutional Correction lecture only . . .
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
GDM (1) (1).pptx small presentation for students
Classroom Observation Tools for Teachers
Abdominal Access Techniques with Prof. Dr. R K Mishra
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Anesthesia in Laparoscopic Surgery in India
Ad

CHAPTER 4 Learning QUEUE data structure.pptx

  • 1. QUEUE CHAPTER 4: BBP 25203: DATA STRUCTURE AND ALGORITHM
  • 4. Queues are data structures that, like the stack, have restrictions on where you can add and remove elements. To understand a queue, think of a cafeteria line: the person at the front is served first, and people are added to the line at the back. Thus, the first person in line is served first, and the last person is served last. This can be abbreviated to First In, First Out (FIFO). In a real-world analogy, we can imagine a bus queue where the passengers wait for the bus in a queue or a line. The first passenger in the line enters the bus first as that passenger happens to be the one who had come first. QUEUE
  • 5. QUEUE We have two ends i.e. “front” and “rear” of the queue. When the queue is empty, then both the pointers are set to -1. The “rear” end pointer is the place from where the elements are inserted in the queue. The operation of adding /inserting elements in the queue is called “enqueue”. The “front” end pointer is the place from where the elements are removed from the queue. The operation to remove/delete elements from the queue is called “dequeue”. When the rear pointer value is size-1, then we say that the queue is full. When the front is null, then the queue is empty.
  • 6. QUEUE Basic Operations The queue data structure includes the following operations: •EnQueue: Adds an item to the queue. Addition of an item to the queue is always done at the rear of the queue. •DeQueue: Removes an item from the queue. An item is removed or de-queued always from the front of the queue. •isEmpty: Checks if the queue is empty. •isFull: Checks if the queue is full. •peek: Gets an element at the front of the queue without removing it.
  • 7. QUEUE Enqueue In this process, the following steps are performed: •Check if the queue is full. •If full, produce overflow error and exit. •Else, increment ‘rear’. •Add an element to the location pointed by ‘rear’. •Return success. Dequeue Dequeue operation consists of the following steps: •Check if the queue is empty. •If empty, display an underflow error and exit. •Else, the access element is pointed out by ‘front’. •Increment the ‘front’ to point to the next accessible data. •Return success.
  • 11. PRIORITY QUEUE What is a Priority Queue? A priority queue in c++ is a type of container adapter, which processes only the highest priority element, i.e. the first element will be the maximum of all elements in the queue, and elements are in decreasing order. Difference between a queue and priority queue : •Priority Queue container processes the element with the highest priority, whereas no priority exists in a queue. •Queue follows First-in-First-out (FIFO) rule, but in the priority queue highest priority element will be deleted first. •If more than one element exists with the same priority, then, in this case, the order of queue will be taken.
  • 12. PRIORITY QUEUE Methods of Priority Queue : Following are the methods of Priority Queue : 1.empty() – This method checks whether the priority_queue container is empty or not. If it is empty, return true, else false. It does not take any parameter. syntax : p1.empty() // p1 is priority_queue object 2.size() – This method gives the number of elements in the priority queue container. It returns the size in an integer. It does not take any parameter. syntax : p2.size() // p2 is priority_queue object 3.push() – This method inserts the element into the queue. Firstly, the element is added to the end of the queue, and simultaneously elements reorder themselves with priority. It takes value in the parameter. syntax : p3.push(value) //value to be inserted
  • 13. PRIORITY QUEUE 4.pop() – This method delete the top element (highest priority) from the priority_queue. It does not take any parameter. syntax : p3.pop() // p3 is priority_queue object 5.top() – This method gives the top element from the priority queue container. It does not take any parameter. syntax : p3.top() 6.swap() – This method swaps the elements of a priority_queue with another priority_queue of the same size and type. It takes the priority queue in a parameter whose values need to be swapped. syntax : p3.swap(p1) 7.emplace() – This method adds a new element in a container at the top of the priority queue. It takes value in a parameter. syntax : p3.emplace(value)
  • 14. PRIORITY QUEUE Inserting elements in a Priority Queue – : Live Demo!!
  • 15. PRIORITY QUEUE Accessing elements in a Priority Queue : Live Demo!!
  • 16. PRIORITY QUEUE Deleting elements in a Priority Queue : Live Demo!!
  • 17. HEAP
  • 18. HEAP Heap data structure is a complete binary tree that satisfies the heap property. It is also called as a binary heap. A complete binary tree is a special binary tree in which •every level, except possibly the last, is filled •all the nodes are as far left as possible
  • 19. HEAP Heap Property is the property of a node in which •(for max heap) key of each node is always greater than its child node/s and the key of the root node is the largest among all other nodes;
  • 20. HEAP •(for min heap) key of each node is always smaller than the child node/s and the key of the root node is the smallest among all other nodes.
  • 21. HEAP Heap Operations Some of the important operations performed on a heap are described below along with their algorithms. Heapify Heapify is the process of creating a heap data structure from a binary tree. It is used to create a Min-Heap or a Max-Heap. Let the input array be
  • 22. HEAP Create a complete binary tree from the array
  • 23. HEAP Start from the first index of non-leaf node whose index is given by n/2 - 1
  • 24. HEAP Repeat steps until the subtrees are also heapified. For Min-Heap, both leftChild and rightChild must be smaller than the parent for all nodes.
  • 25. HEAP Insert element to the heap 1. Insert the new element at the end of the tree.
  • 26. HEAP Insert element to the heap 2. Heapify the tree.
  • 27. HEAP Delete Element from Heap 1. Select the element
  • 28. HEAP Delete Element from Heap 2. Swap the element to the last leaf node
  • 29. HEAP Delete Element from Heap 3. Remove the last element.
  • 30. HEAP Delete Element from Heap 4. Heapify the tree Live Demo!!