5.
10 Double-Ended Heap Structures and Multidimensional Heaps 267
list from n->first to n->last, and whenever there are three consecutive
nodes of the same rank, we combine them to the next larger rank, so that
in the end each rank occurs either one or two times. Then we set the rank
n->rank to n->last->rank + 1. After this, the heap again satisfies all
the conditions. In total all these operations for the delete min took O(log n)
time.
To summarize the performance of this structure, we have the following:
Theorem. Brodal’s heap structure supports the operations insert, merge,
find min in worst-case O(1) and delete min in worst-case O(log n)
time.
If one adds upward pointers, one can also get the operations decrease
key and arbitrary deletions in O(log n) time. Some care is required; however,
the structure does not give an O(log n) height bound, because a list of nodes of
rank 0, which each have the special neighbor, would be a correct heap structure.
The strategy is therefore to bubble up until one meets a special neighbor link,
and then clear the special neighbor of r->special as described before, and
insert the current node there instead.
An array-based heap of the same performance, O(1) worst-case insert
and find min, and O(log n) delete min, was developed in Carlsson,
Munro, and Poblete (1988). As always in array-based heaps, we cannot merge
two heaps in this structure, but the space requirement is significantly smaller
by the implicit representation than in Brodal’s heap, where we need at least six
pointers per element.
5.10 Double-Ended Heap Structures
and Multidimensional Heaps
The heap structures that we discussed so far allow fast access to one end of the
set of keys to the minimum key element in the current set, the way we have pre-
sented it here, or the maximum key element if we reverse all the inequality con-
ditions. This is sufficient for all natural applications, but an obvious generaliza-
tion is to ask for fast access both to the minimum and to the maximum element.
That structure is called a double-ended heap, and it must support at least the
operations insert, find min, find max, delete min, delete max,
and possibly additional operations like merge or change key.
If we use balanced search trees as heap, as described in Section 5.1, we
immediately get a double-ended heap with the heap operations all in O(log n),
268 5 Heaps
and with the lazy deletion improvement we can get insert and change key
in O(log n), and find min, find max, delete min, delete max in
O(1) worst-case time. And all this requires not much extra effort beyond the
balanced search tree with leaves arranged in a doubly linked list.
Nonetheless, a large number of other double-ended heaps have been pro-
posed.4 The most obvious solution would be to have two heaps – a min-heap
and a max-heap – and insert each element in both, linking the two copies to-
gether by pointers. This requires that the underlying heap structure supports not
only delete min, but deletion of arbitrary elements, given a pointer to that
element. This element duplication reduces an insert to two insert operations
in the underlying heaps, and a delete min or delete max to the corre-
sponding deletion in one of the underlying heaps, and an arbitrary deletion in
the other. A merge operation reduces to two merges of the underlying heaps,
when these are supported, but decrease key fails to generalize, unless the
underlying heap allows key changes in both directions, because the min-heap
and the max-heap have opposite preferred orientations. We discussed this in
Section 5.7.
An alternative to element duplication is to group the elements into pairs,
again linked by pointers, and the smaller element of each pair is inserted into the
min-heap and the larger element into the max-heap. Then any delete min in
the min-heap or delete max in the max-heap does indeed delete the global
minimum or maximum, it breaks only one of these pairs, which has to be
corrected. This might again require deletion of arbitrary elements from one
heap, if that heap contained several unmatched elements, of which half have to
be moved to the other heap. If the underlying structure is a heap-ordered tree,
this can be avoided by matching only the leaves.
This idea, combined with array-based heaps, was already observed in Knuth
(1973) and Carlsson (1987/88), also Carlsson, Chen, and Strothotte (1989),
van Leeuwen and Wood (1993), Chang and Du (1993), Chen (1995), and
Jung (2005). These structures differ essentially only in the way these heaps
are mapped into an array and how the pairing between them is established;
this influences the multiplicative constant in the O(log n) bound per operation.
Because these structures are all based on the array-based heaps, they achieve
O(log n) per insert, delete min, delete max, or even arbitrary dele-
tions of elements with known positions, and O(1) find min and find max.
Arbitrary key changes of elements at known position can be done in O(log n)
time by deleting and reinserting it.
4 Frequently,the original note by Williams (1964), in which he first defined the heap, is also cited
as the source of the first double-ended heap, but this is not true.
5.10 Double-Ended Heap Structures and Multidimensional Heaps 269
Array-based heaps are combined with a different order structure in Atkinson
et al. (1986) and Arvind and Rangan (1999) to implement double-ended heaps.
They again achieve the same O(log n) time for all operations supported by
array-based heaps; the only differences are the multiplicative constants in the
number of comparisons and possibly the difficulty of the implementation.
The idea of pairing elements in a min-heap and a max-heap structure is,
of course, not restricted to array-based heaps. It is applied to binomial heaps
in Khoong and Leong (1993) and to leftist heaps in Cho and Sahni (1999),
where all three variants – element duplication, global element pairing, and
leaves-only pairing – are discussed. The method is studied as general con-
struction principle in Chong and Sahni (2000) and Makris, Tsakalidis, and
Tsichlas (2003). If we have any underlying heap that supports merge and
deletion of arbitrary elements, the derived double-ended heap consists of the
following parts:
{ at most one unmatched element,
{ a min-heap,
{ a max-heap, and
{ a pairing of the elements of the min-heap and the max-heap, so that for each
pair, the min-heap element is smaller than the max-heap element, and from
any element we can access the other half of its pair in O(1).
Now the operations work as follows:
{ insert: If there is an unmatched element, the new element is paired with
it, and the smaller part of the pair is inserted into the min-heap, the larger
into the max-heap. If there is no unmatched element, the new element
becomes the unmatched element.
{ find min: Performs a find min in the min-heap and compares the
result with the unmatched element if there is one and returns the smaller.
{ find max: Performs a find max in the max-heap and compares the
result with the unmatched element if there is one and returns the larger.
{ delete min: Performs a find min in the min-heap and compares the
result with the unmatched element if there is one. If the unmatched element
is smaller, it deletes and returns the unmatched element. Otherwise it
performs a delete min in the min-heap, a general delete of the
matched element in the max-heap, and again an insert of that element
from the max-heap.
{ delete max: Performs a find max in the max-heap and compares the
result with the unmatched element if there is one. If the unmatched element
is larger, it deletes and returns the unmatched element. Otherwise it
performs a delete max in the max-heap, a general delete of the
270 5 Heaps
matched element in the min-heap, and again an insert of that element
from the min-heap.
{ merge: Performs a merge for the two min-heaps and another merge for
the two max-heaps, and if there are two unmatched elements, one from each
of the merged heaps, it matches them and inserts the smaller into the
min-heap, the larger into the max-heap.
If we apply this construction to the heap invented by Brodal (1995) that we
described in the previous section, which supported insert and merge in
O(1) and delete min as well as arbitrary deletions in O(log n), we obtain a
double-ended heap with insert, find min, find max, merge in O(1) and
delete min, delete max in O(log n) time (Chong and Sahni 2000; Makris
et al. 2003). Brodal (1995) himself proposed element duplication instead, which
gives exactly the same performance, but needs twice the space for the heap. But
if the objects associated with the keys are larger, this does not matter because
the objects themselves are not duplicated.
Theorem. There is a double-ended heap that supports insert, find min,
find max, merge in O(1) and delete min, delete max in O(log n)
worst-case time.
Further pointer-based double-ended heaps were proposed in Olariu, Over-
street, and Wen (1991) and Ding and Weiss (1993), which reuses the alternative
order structure of min-layers and max-layers developed in Atkinson et al. (1986)
for array-based heaps. The heaps of Atkinson et al. (1986) were also studied in
Hasham and Sack (1987) and Strothotte, Eriksson, and Vallner (1989).
A further generalization of the double-ended heap is the d-dimensional
interval heaps proposed in van Leeuwen and Wood (1993) and discussed further
by Ding and Weiss (1994). They model a set of objects, where to each object a d-
tuple of key values is attached, and one can query for the objects with minimum
or maximum ith coordinate for each i = 1, . . . , d. This looks somewhat similar
to range searching, and indeed van Leeuwen and Wood (1993) observed that
their structure allows to solve complementary orthogonal range queries, that
is, listing the points outside a given box in output-sensitive time O(log n + k).
They are realized as array-based heaps, with insert, delete min, and
delete max for each coordinate in O(log n) time.
A d-dimensional min-heap is the natural generalization of all these struc-
tures: a set of objects, each with d key values, in a structure that allows inserts,
and query for and deletion of the object with minimum ith coordinate. A
double-ended heap is a special case of a two-dimensional heap because we can
5.11 Heap-Related Structures with Constant-Time Updates 271
replace each key by the pair (key, −key). Then the maximum queries translate
into minimum queries for the second coordinate. In the same way, the queries
supported by a d-dimensional interval heap are a special case of the queries in
a 2d-dimensional min-heap.
Again, one can implement this using several heaps whose elements are
linked, one heap for each coordinate (Brass 2007). The main difference is that
we cannot group the elements into d-tuples and insert one in each heap, because
it is possible that the same element is minimal for each coordinate and has thus
to be entered in each heap. The simplest way to realize this structure is element
duplication. We have d min-heaps, one for each coordinate, and we insert each
element in each heap, joining the nodes that refer to the same element in a
cyclic linked list. Then each insert reduces to d insertions in the underlying
heaps, and each delete min in one coordinate reduces to one delete min
in one heap, which gives us the beginning of the list of copies, and d − 1
general deletions at known places in the other heaps. And for a merge, we
just merge the d coordinate-heaps. Using again Brodal’s heap as the underlying
heap structure, we obtain the following bounds:
Theorem. There is a d-dimensional min-heap that supports insert, merge,
and find min in each coordinate in O(1) and delete min in each coordi-
nate in O(log n) worst-case time.
5.11 Heap-Related Structures with Constant-Time
Updates
Several structures have been studied that keep track of the minimum key in
a dynamically changing set if the changes are subject to some restrictions. In
general, because we can use a search tree to allow arbitrary insertions and
deletions in O(log n) and find the minimum in O(1), we are interested in such
situations where the updates are significantly faster than O(log n), at best in
time O(1).
The simplest example of such a structure is to keep track of the minimum
value of elements on a stack. One can view the stack as a set that changes in
a very restricted way: if y is inserted after x, then it must be deleted before
x. For the minimum of the key values of the current set, this implies that
either the insertion of y decreases the minimum, then the previous minimum
becomes irrelevant until y is deleted, or the minimum stays the same. So we
can keep track of the current minimum by using a second stack, which contains
the current minimum. For each push on the stack, we compare the current