Heaps and Priority Queues in ds 1.31

I recently revised and improved the Heap and PriorityQueue classes, mainly because the difference between both has always been somewhat blurry. The new implementations are included in ds 1.31, so let’s talk a bit about the changes.

The Heap class

By definition, a heap is a dense binary tree for which every parent node has a value that is less (or greater; henceforward I assume elements are sorted in ascending order) than or equal to any of its children. A heap defines a set of simple operations:

  • Insert element
  • Query smallest element
  • Remove smallest element

In the old implementation, insertion was done by calling enqueue(), and removal by calling dequeue(). This was a bit misleading because a heap is not a queue, instead it’s used as an efficient data structure for implementing priority queues. Thus the existing heap API has been modified and now consists of the methods add(), top() and pop(), corresponding to the old methods enqueue(), front() and dequeue(), respectively. I’ve also added some additional methods:

  • replace() – replaces the smallest element with a new element
  • change() – updates an existing element after it has been changed (restores heap condition)
  • sort() – returns a sorted array of all elements (runs the heap sort algorithm)
  • bottom() – finds and returns the largest element
  • repair() – rebuilds the entire heap

Note that replace() and change() is a lot faster than a combined remove() and add() operation. Also, bottom() runs in O(n) wheres top() is O(1). This can be further improved by a Min-Max Heap, which I may add in a future release.

Last but not least I did some optimizations, lowering memory requirements and almost doubling performance. And you don’t have to enable element-removal capabilities through the constructor as the heap now always supports removal of arbitrary elements.

The PriorityQueue class

As mentioned above, heaps are used for implementing priority queues and as the name implies, it’s a queue ADT so PriorityQueue now implements the Queue interface, defining the following methods:

  • enqueue() – adds an element
  • dequeue() – removes the element with the highest priority
  • peek() – returns the element with the highest priority (without removing it)
  • back() – returns the element with the lowest priority (without removing it)

The Heap class requires that every element provides a comparison function by implementing the Heapable interface. This is not needed for the PriorityQueue class, because here the elements are sorted using integer keys, resulting in a much better performance. So if you are only interested in managing prioritized data this is the perfect choice, while the Heap class is best used as an all-round tool for solving common problems like finding the min, max or k-th largest element.


Elements to be inserted into a heap have to implement the Heapable interface, which is defined in haXe like so:

interface Heapable implements Comparable
     * Tracks the position inside a binary heap.
     * This value should never be changed by the user.
    var position:Int;

Declaring a field in an interface is not supported in ActionScript 3.0, so when you inspect the compiled class from an SWC file, it has become an empty marker interface. So don’t forget to define the position field as you don’t get compile-time errors. I could have defined position as a getter/setter style function, but I don’t want to give up all those nice haXe features because it has become my primary language for a long time now. So the AS3 implementation of a heap element would look like this:

    import de.polygonal.ds.Heapable;
    class HeapElement implements Heapable
        public var value:int;
        public var position:int;

        public function HeapElement(value:int)
            this.value = value;

        public function compare(other:Object):int
            return other.value - value;

        public function toString():String
            return "" + value;

Similary, elements to be inserted into a PriorityQueue have to implement Prioritizable like this:

    import de.polygonal.ds.Prioritizable;
    class PriorityQueueElement implements Prioritizable
        public var priority:Int;
        public var position:Int;
        public function PriorityQueueElement() {}

Once we have a heap element defined, we can use the Heap like so:

var heap = new Heap();
var a:HeapElement = new HeapElement(2);
var b:HeapElement = new HeapElement(5);
var c:HeapElement = new HeapElement(9);

//insert elements

//dump all elements

//query smallest element

//change element
a.value += 4;

//remove smallest element

//remove largest element

Yo, Ho! Welcome on deck, deque!

The newly released ds library (version 1.3) now includes an efficient deque implementation.

What is a deque?

A deque is a double-ended queue and pronounced “deck”. In contrast to a queue, which allows insertions at one end and removals at the opposite end, a deque allows insertions and removals at both ends. The new Deque interface defines four methods to modify the Deque along with two methods to access the element at the front (head of the list) and back (tail of the list):

interface Deque<T>
    function front():T;
    function back():T;
    function pushFront(x:T):Void;
    function popFront():T;
    function pushBack(x:T):Void;
    function popBack():T;

There are two common ways to implement a deque – by using a doubly-linked list or by using arrays. While the first option is very simple and straightforward, the latter one is more of a challenge because there are many approaches which all have their pros and cons.

Linked implementation – de.polygonal.ds.LinkedDeque

This is basically just a stripped down, lightweight version of the doubly linked list class DLL. A doubly-linked list is capable of doing insertions & removals in constant time, but the operation itself is rather slow, since we need to instantiate a node object that stores the element and manage pointers to prevent the list from falling apart.
Another important thing to watch out is that the LinkedDeque class requires considerable more memory – for each and every element we need a container object for storing the “cargo” (+16 bytes), a reference to the previous and next node (+4 bytes each) and finally a reference that points to the element (+4 bytes). So in Flash we need a total of 28 extra bytes per element. This means that 86% of the space is wasted on nodes and pointers!
Despite those shortcomings it’s still useful for small to average sizes; and to minimize costly node object instantiation the LinkedDeque class (and all linked structures in ds) comes with built-in node pooling – all you have to do is to create a LinkedDeque object with a reserved size greater than zero:

var deque = new LinkedDeque<Int>(100); //reuses up to 100 nodes

This is very useful if the maximum size is known in advance, as performance nearly doubles. The benchmarks below were all done with “node-caching” turned on.

Arrayed implementation – de.polygonal.ds.ArrayedDeque

While most sites explain how a doubly-linked list relates to a Deque, there is not much information available on how to efficiently implement a deque on top of an array. My first attempt used a circular array similar to the ArrayedQueue class. It seemed to be a good solution until I realized that resizing the deque would be slow and difficult to implement so I discarded this idea and started from scratch. This time I decided to use an array of arrays (turned out the C++ STL deque uses this approach) and I was very happy with the result.
Let me briefly explain how it works. Data is organized in chunks or blocks of memory. A block is simply a fixed-sized array and all blocks are stored in a separate, dynamic array. Upon initialization the deque only contains a single block but once it fills up an additional block is allocated: adding elements to the back calls something like “blockListArray.push(newBlock)” and adding elements to the front calls “blockListArray.unshift(newBlock)”.
Inserting elements at the beginning of an array is slow but in this case it doesn’t matter because the list of blocks is usually very small and it doesn’t happen very often. Therefore we can say that the ArrayedDeque performs modifications at both ends in amortized constant time.


So how do both classes perform? Showing raw numbers would be pointless without being able to compare them to some reference values. For this reason I’ve created a minimal deque implementation called “NaiveDeque”:

class NaiveDeque<T> implements Deque<T>
    var _data:Array<T>;
    public function new() {
        _data = new Array<T>();
    public function front():T {
        return _data[0];
    public function pushFront(x:T):Void {
    public function popFront():T {
        return _data.shift();
    public function back():T {
        return _data[_data.length - 1];
    public function pushBack(x:T):Void {
    public function popBack():T {
        return _data.pop();

As you see it just uses what the Flash language has to offers. Clearly, the problem which the code above is that modifications at the beginning of the array are slow because a lot of memory is shifted around, either to fill a gap or to make room for a new element. So we kinda have the worst-case at the front and the best-case at the back. It’s not very fair to use this as a reference but on the other hand it shows how things can be drastically improved by using some elbow grease to write clever data structures. Here are the results:

deque benchmark

size 1000 2000 3000 4000 5000
NaiveDeque 1.0 1.0 1.0 1.0 1.0
LinkedDeque 1.65 4.4 6.6 7.2 7.7
ArrayedDeque 5.0 9.3 11.7 14.8 19.3

The benchmark results were taken by equally distributing n elements at both ends, like this:

for (i in 0...n)
    if ((i & 1) == 0)
for (i in 0...n)
    if ((i & 1) == 0)


One real-world application that comes to my mind is a software application’s list of undo operations; but as a deque is a very flexible abstract data structure it can be used in countless algorithms. The deque classes are all documented so I hope it’s clear how to use them. If you have problems feel free to contact me.