A double-ended priority queue features the following operations:
Also, the priority of any element can be changed once it has been inserted in the DEPQ.4
Double-ended priority queues can be built from balanced binary search trees (where the minimum and maximum elements are the leftmost and rightmost leaves, respectively), 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:5
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.6 Priority of every element in the min PQ will be less than or equal to the corresponding element in the max PQ.
In contrast to a total 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.7 If the number of elements in the DEPQ is odd, one of the elements is retained in a buffer.8
Apart from the above-mentioned correspondence methods, DEPQ's can be obtained efficiently using interval heaps.9 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:10
Depending on the number of elements, two cases are possible11 -
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(log n).
Thus, with interval heaps, both the minimum and maximum elements can be removed efficiently traversing from root to leaf. Thus, a DEPQ can be obtained15 from an interval heap where the elements of the interval heap are the priorities of elements in the DEPQ.
When DEPQ's are implemented using Interval heaps consisting of n elements, the time complexities for the various functions are formulated in the table below16
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.17 For pairing heaps, it is an amortized complexity.
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:
Data Structures, Algorithms, & Applications in Java: Double-Ended Priority Queues, Sartaj Sahni, 1999. http://www.cise.ufl.edu/~sahni/dsaaj/enrich/c13/double.htm ↩
Brass, Peter (2008). Advanced Data Structures. Cambridge University Press. p. 211. ISBN 9780521880374. 9780521880374 ↩
"Depq - Double-Ended Priority Queue". Archived from the original on 2012-04-25. Retrieved 2011-10-04. https://web.archive.org/web/20120425045245/http://depq.rubyforge.org/ ↩
"depq". http://rubygems.org/gems/depq ↩
Fundamentals of Data Structures in C++ - Ellis Horowitz, Sartaj Sahni and Dinesh Mehta /wiki/Sartaj_Sahni ↩
http://www.mhhe.com/engcs/compsci/sahni/enrich/c9/interval.pdf [bare URL PDF] http://www.mhhe.com/engcs/compsci/sahni/enrich/c9/interval.pdf ↩