Double-ended priority queue


In computer science, a double-ended priority queue or double-ended heap is a data structure similar to a priority queue or heap, but allows for efficient removal of both the maximum and minimum, according to some ordering on the keys stored in the structure. Every element in a DEPQ has a priority or value. In a DEPQ, it is possible to remove the elements in both ascending as well as descending order.

Operations

A double-ended priority queue features the following operations:
;isEmpty: Checks if DEPQ is empty and returns true if empty.
;size: Returns the total number of elements present in the DEPQ.
;getMin: Returns the element having least priority.
;getMax: Returns the element having highest priority.
;put: Inserts the element x in the DEPQ.
;removeMin: Removes an element with minimum priority and returns this element.
;removeMax: Removes an element with maximum priority and returns this element.
If an operation is to be performed on two elements having the same priority, then the element inserted first is chosen. Also, the priority of any element can be changed once it has been inserted in the DEPQ.

Implementation

Double-ended priority queues can be built from balanced binary search trees, or using 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

In this method two different priority queues for min and max are maintained. The same elements in both the PQs are shown with the help of correspondence pointers.
Here, the minimum and maximum elements are values contained in the root nodes of min heap and max heap respectively.
Half the elements are in the min PQ and the other half in the max PQ. Each element in the min PQ has a one-to-one correspondence with an element in max PQ. If the number of elements in the DEPQ is odd, one of the elements is retained in a buffer. Priority of every element in the min PQ will be less than or equal to the corresponding element in the max PQ.

Leaf correspondence

In this method only the leaf elements of the min and max PQ form corresponding one-to-one pairs. It is not necessary for non-leaf elements to be in a one-to-one correspondence pair.

Interval heaps

Apart from the above-mentioned correspondence methods, DEPQ's can be obtained efficiently using interval heaps. An interval heap is like an embedded min-max heap in which each node contains two elements. It is a complete binary tree in which:
Depending on the number of elements, two cases are possible -
  1. Even number of elements: In this case, each node contains two elements say p and q, with pq. Every node is then represented by the interval .
  2. Odd number of elements: In this case, each node except the last contains two elements represented by the interval whereas the last node will contain a single element and is represented by the interval .

    Inserting an element

Depending on the number of elements already present in the interval heap, following cases are possible:
The time required for inserting an element depends on the number of movements required to meet all the conditions and is O.

Deleting an element

Thus, with interval heaps, both the minimum and maximum elements can be removed efficiently traversing from root to leaf. Thus, a DEPQ can be obtained from an interval heap where the elements of the interval heap are the priorities of elements in the DEPQ.

Time Complexity

Interval Heaps

When DEPQ's are implemented using Interval heaps consisting of n elements, the time complexities for the various functions are formulated in the table below
OperationTime Complexity
initO
isEmptyO
getminO
getmaxO
sizeO
insertO
removeMinO
removeMaxO

Pairing heaps

When DEPQ's are implemented using heaps or pairing heaps consisting of n elements, the time complexities for the various functions are formulated in the table below. For pairing heaps, it is an amortized complexity.
OperationTime Complexity
isEmptyO
getminO
getmaxO
insertO
removeMaxO
removeMinO

Applications

External sorting

One example application of the double-ended priority queue is external sorting. In an external sort, there are more elements than can be held in the computer's memory. The elements to be sorted are initially on a disk and the sorted sequence is to be left on the disk. The external quick sort is implemented using the DEPQ as follows:
  1. Read in as many elements as will fit into an internal DEPQ. The elements in the DEPQ will eventually be the middle group of elements.
  2. Read in the remaining elements. If the next element is ≤ the smallest element in the DEPQ, output this next element as part of the left group. If the next element is ≥ the largest element in the DEPQ, output this next element as part of the right group. Otherwise, remove either the max or min element from the DEPQ ; if the max element is removed, output it as part of the right group; otherwise, output the removed element as part of the left group; insert the newly input element into the DEPQ.
  3. Output the elements in the DEPQ, in sorted order, as the middle group.
  4. Sort the left and right groups recursively.