Jump to content

Heap (data structure)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Mellum (talk | contribs) at 00:23, 13 March 2011 (Undid revision 418116411 by 59.98.208.174 (talk)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Example of a complete binary max-heap

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if B is a child node of A, then key(A) ≥ key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.) There is no restriction as to how many children each node has in a heap. The heap is one maximally-efficient implementation of an abstract data type called a priority queue. Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm.

Heaps are usually implemented in an array, and do not require pointers between elements[citation needed].

The operations commonly performed with a heap are:

  • find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap, respectively
  • delete-max or delete-min: removing the root node of a max- or min-heap, respectively
  • increase-key or decrease-key: updating a key within a max- or min-heap, respectively
  • insert: adding a new key to the heap
  • merge: joining two heaps to form a valid new heap containing all the elements of both.

Heaps are used in the sorting algorithm heapsort.

Variants

Comparison of theoretic bounds for variants

The following time complexities[1] are amortized (worst-time) time complexity for entries marked by an asterisk, and regular worst case time complexities for all other entries. O(f) gives asymptotic upper bound and Θ(f) is asymptotically tight bound (see Big O notation). Function names assume a min-heap.

Operation Binary Binomial Fibonacci Pairing[2] Brodal
findMin Θ(1) Θ(log n) or Θ(1) Θ(1)[citation needed] Θ(1)[citation needed] Θ(1)[citation needed]
deleteMin Θ(log n) Θ(log n) O(log n)* O(log n)* O(log n)
insert Θ(log n) O(log n) Θ(1)[citation needed] O(1)*[citation needed] Θ(1)[citation needed]
decreaseKey Θ(log n) Θ(log n) Θ(1)* O(log n)* Θ(1)
merge Θ(n) O(log n)** Θ(1) O(1)* Θ(1)

(*)Amortized time
(**)Where n is the size of the larger heap

Note that a "Brodal queue" is an implementation of a parallel priority queue created by Gerth Stølting Brodal et. al.[3]

Heap applications

The heap data structure has many applications.

Full and almost full binary heaps may be represented in a very space-efficient way using an array alone. The first (or last) element will contain the root. The next two elements of the array contain its children. The next four contain the four children of the two child nodes, etc. Thus the children of the node at position n would be at positions 2n and 2n+1 in a one-based array, or 2n+1 and 2n+2 in a zero-based array. This allows moving up or down the tree by doing simple index computations. Balancing a heap is done by swapping elements which are out of order. As we can build a heap from an array without requiring extra memory (for the nodes, for example), heapsort can be used to sort an array in-place.

One more advantage of heaps over trees in some applications is that construction of heaps can be done in linear time using Tarjan's algorithm.[how?]

Heap implementations

  • The C++ Standard Template Library provides the make_heap, push_heap and pop_heap algorithms for heaps (usually implemented as binary heaps), which operate on arbitrary random access iterators. It treats the iterators as a reference to an array, and uses the array-to-heap conversion.
  • The Java 2 platform (since version 1.5) provides the binary heap implementation with class java.util.PriorityQueue<E> in Java Collections Framework.
  • Python has a heapq module that implements a priority queue using a binary heap.
  • PHP has both maxheap (SplMaxHeap) and minheap (SplMinHeap) as of version 5.3 in the Standard PHP Library.
  • Perl has implementations of binary, binomial, and Fibonacci heaps in the Heap distribution available on CPAN.

See also

References

  1. ^ Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest (1990): Introduction to algorithms. MIT Press / McGraw-Hill.
  2. ^ Iacono, John (2000), "Improved upper bounds for pairing heaps", Proc. 7th Scandinavian Workshop on Algorithm Theory, Lecture Notes in Computer Science, vol. 1851, Springer-Verlag, pp. 63–77, doi:10.1007/3-540-44985-X_5
  3. ^ A Parallel Priority Queue with Constant Time Operations (PDF) {{citation}}: Text "web" ignored (help)
  4. ^ Frederickson, Greg N. (1993), "An Optimal Algorithm for Selection in a Min-Heap", Information and Computation (PDF), vol. 104, Academic Press, pp. 197–214, doi:10.1006/inco.1993.1030