Deque Data Structure
Deque or Double Ended Queue is a type of queue in which insertion and removal of elements can either be performed from the front or the rear. Thus, it does not follow FIFO rule (First In First Out).
Types of Deque
- Input Restricted Deque
In this deque, input is restricted at a single end but allows deletion at both the ends. - Output Restricted Deque
In this deque, output is restricted at a single end but allows insertion at both the ends.
Operations on a Deque
Below is the circular array implementation of deque. In a circular array, if the array is full, we start from the beginning.
But in a linear array implementation, if the array is full, no more elements can be inserted. In each of the operations below, if the array is full, "overflow message" is thrown.
Before performing the following operations, these steps are followed.
- Take an array (deque) of size n.
- Set two pointers at the first position and set
front = -1
andrear = 0
.
1. Insert at the Front
This operation adds an element at the front.
- Check the position of front.
- If
front < 1
, reinitializefront = n-1
(last index). - Else, decrease front by 1.
- Add the new key 5 into
array[front]
.
2. Insert at the Rear
This operation adds an element to the rear.
- Check if the array is full.
- If the deque is full, reinitialize
rear = 0
. - Else, increase rear by 1.
- Add the new key 5 into
array[rear]
.
3. Delete from the Front
Priority Queue
A priority queue is a special type of queue in which each element is associated with a priority value. And, elements are served on the basis of their priority. That is, higher priority elements are served first.
However, if elements with the same priority occur, they are served according to their order in the queue.
Assigning Priority Value
Generally, the value of the element itself is considered for assigning the priority. For example,
The element with the highest value is considered the highest priority element. However, in other cases, we can assume the element with the lowest value as the highest priority element.
We can also set priorities according to our needs.
Priority Queue Operations
Basic operations of a priority queue are inserting, removing, and peeking elements.
1. Inserting an Element into the Priority Queue
Inserting an element into a priority queue (max-heap) is done by the following steps.
- Insert the new element at the end of the tree.
- Heapify the tree.
Algorithm for insertion of an element into priority queue (max-heap)
If there is no node, create a newNode. else (a node is already present) insert the newNode at the end (last node from left to right.) heapify the array
For Min Heap, the above algorithm is modified so that parentNode
is always smaller than newNode
.
2. Deleting an Element from the Priority Queue
Deleting an element from a priority queue (max-heap) is done as follows:
- Select the element to be deleted.
- Swap it with the last element.
- Remove the last element.
- Heapify the tree.
No comments:
Post a Comment