- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Double Ended Priority Queue (DEPQ)

A double-ended priority queue (DEPQ) or double-ended heap is defined as a data structure like a priority queue or heap, but permits for efficient removal of both the maximum and minimum, according to some ordering on the keys or items stored in the structure. Every element in a DEPQ associated with a priority or value. In a DEPQ, it is possible to eliminate or remove the elements in both ascending and descending order.

**Operations**

A double-ended priority queue consists of the following operations

**isEmpty()**

This function is responsible to check if DEPQ is empty and returns true if empty.

**size()**

This function is responsible to return the total number of elements present in the DEPQ.

**getMin(y)**

This function is responsible to return the element y having least priority.

**getMax(y)**

This function is responsible to return the element y having highest priority.

**put(y)**

This function is responsible to insert the element y in the DEPQ.

**removeMin(y)**

This function is responsible to remove an element y with minimum priority and return this element.

**removeMax(y)**

This function is responsible to remove an element y with maximum priority and return this element.

## Implementation

Double-ended priority queues can be built or formed from balanced binary search trees (where the smallest and largest elements are treated as the leftmost and rightmost leaves, respectively), or implementing specialized data structures like min-max heap and pairing heap.

Generic methods of arriving at double-ended priority queues from normal priority queues are:

## Dual structure method

According to this method, two different priority queues for min and max are set or maintained. The same elements in both the priority queues are displayed or shown with the help of correspondence pointers.

Here, the minimum and maximum elements are denoted as the values contained in the root nodes of min heap and max heap respectively.

**Removing the min element** − Operate removemin() on the min heap and remove(node value) on the max heap, where node value is defined as the value in the corresponding node in the max heap.

**Removing the max element** − Operate removemax() on the max heap and remove(node value) on the min heap, where node value is defined as the value in the corresponding node in the min heap.

## Total correspondence

Half the elements are in the min priority queue and the other half in the max priority queue. Each element in the min priority queue has a one-to-one correspondence with an element in max priority queue. If the number of elements in the DEPQ indicates odd value, one of the elements is retained in a buffer i.e. a specific storage area. Priority of every element in the min priority queue will be less than or equal to the corresponding element in the max priority queue.

## Leaf correspondence

According to this method only the leaf elements of the min and max priority queue form corresponding one-to-one pairs. It is not required for non-leaf elements to be in a one-to-one correspondence pair.

## Interval heaps

Besides the above-mentioned correspondence methods, DEPQ's can be obtained perfectly implementing interval heaps. An interval heap is like an embedded min-max heap in which each node consists of two elements. It is defined as a complete binary tree in which −

- The left element is always less than or equal to the right element.
- Both the elements define or specify a closed interval.
- Interval represented by any node except the root is denoted as a sub-interval of the parent node.
- Elements on the left hand side define or denote a min heap.
- Elements on the right hand side define or denote a max heap.