ArticleslgStudy

computer science

Double-ended priority queue

Double-ended priority queue is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Double-ended priority queue rather than just read about it. In short: In computer science, a double-ended priority queue (DEPQ) or double-ended heap or priority deque 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 (items) stored in the structure. Every element in a DEPQ has a priority or value.

Double-ended priority queue — main illustration
Double-ended priority queue — illustration

Key takeaways

  • Double-ended priority queue belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Double-ended priority queue to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Double-ended priority queue from memory before moving on to harder problems.

Reference excerpt

In computer science, a double-ended priority queue (DEPQ) or double-ended heap or priority deque 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 (items) 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(x) 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. 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 (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:

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.

Removing the min element: Perform removemin() on the min heap and remove(node value) on the max heap, where node value is the value in the corresponding node in the max heap. Removing the max element: Perform removemax() on the max heap and remove(node value) on the min heap, where node value is the value in the corresponding node in the min heap.

Total correspondence

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 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. If the number of elements in the DEPQ is odd, one of the elements is retained in a buffer.

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:

The left element is less than or equal to the right element. Both the elements define a closed interval. Interval represented by any node except the root is a sub-interval of the parent node. Elements on the left hand side define a min heap. Elements on the right hand side define a max heap. Depending on the number of elements, two cases are possible -

Even number of elements: In this case, each node contains two elements say p and q, with p ≤ q. Every node is then represented by the interval [p, q]. Odd number of elements: In this case, each node except the last contains two elements represented by the interval [p, q] whereas the last node will contain a single element and is represented by the interval [p, p].

Inserting an element Depending on the number of elements already present in the interval heap, following cases are possible:

Odd number of elements: If the number of elements in the interval heap is odd, the new element is firstly inserted in the last node. Then, it is successively compared with the previous node elements and tested to satisfy the criteria essential for an interval heap as stated above. In case if the element does not satisfy any of the criteria, it is moved from the last node to the root until all the conditions are satisfied. Even number of elements: If the number of elements is even, then for the insertion of a new element an additional node is created. If the element falls to the left of the parent interval, it is considered to be in the min heap and if the element falls to the right of the parent interval, it is considered in the max heap. Further, it is compared successively and moved from the last node to the root until all the conditions for interval heap are satisfied. If the element lies within the interval of the parent node itself, the process is stopped then and there itself and moving of elements does not take place. The time required for inserting an element depends on the number of movements required to meet all the conditions and is O(log n).

Deleting an element Min element: In an interval heap, the minimum element is the element on the left hand side of the root node. This element is removed and returned. To fill in the vacancy created on the left hand side of the root node, an element from the last node is removed and reinserted into the root node. This element is then compared successively with all the left hand elements of the descending nodes and the process stops when all the conditions for an interval heap are satisfied. In case if the left hand side element in the node becomes greater than the right side element at any stage, the two elements are swapped and then further comparisons are done. Finally, the root node will again contain the minimum element on the left hand side. Max element: In an interval heap, the maximum element is the element on the right hand side of the root node. This element is removed and returned. To fill in the vacancy created on the right hand side of the root node, an element from the last node is removed and reinserted into the root node. Further comparisons are carried out on a similar basis as discussed above. Finally, the root node will again contain the max element on the right hand side. 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

… excerpt ends here. Continue reading the full article.

Illustrations

Double-ended priority queue: A dual structure with 14,12,4,10,8 as the members of DEPQ.[1]
A dual structure with 14,12,4,10,8 as the members of DEPQ.[1]
Double-ended priority queue: A total correspondence heap for the elements 3, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11 with element 11 as buffer.[1]
A total correspondence heap for the elements 3, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11 with element 11 as buffer.[1]
Double-ended priority queue: A leaf correspondence heap for the same elements as above.[1]
A leaf correspondence heap for the same elements as above.[1]
Double-ended priority queue: Implementing a DEPQ using interval heap.
Implementing a DEPQ using interval heap.

Worked examples

Example 1 — a first encounter with Double-ended priority queue

Start with the simplest possible case. Write down what Double-ended priority queue claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Double-ended priority queue before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Double-ended priority queue ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Double-ended priority queue

In research
Double-ended priority queue appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Double-ended priority queue in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Double-ended priority queue is common in secondary-school and first-year university syllabi. It links to neighbouring topics Abstract data types, Heaps (data structures), Priority queues, so understanding it makes those chapters shorter.
In everyday life
Look for Double-ended priority queue outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Double-ended priority queue” →

How to study Double-ended priority queue in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Double-ended priority queue means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Double-ended priority queue out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Double-ended priority queue in simple terms?

In computer science, a double-ended priority queue (DEPQ) or double-ended heap or priority deque 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 (items) stored in the structure. Every e…

Why does Double-ended priority queue matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Double-ended priority queue?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Double-ended priority queue.

Tags

  • Abstract data types
  • Heaps (data structures)
  • Priority queues

Keep exploring