CHAPTER 4:
QUEUE
BBP 25203: DATA STRUCTURE AND ALGORITHM
SUBTOPICS
4.1 Queues
4.2 Priority Queues
4.3 Heap
QUEUES
QUEUE
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
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