Heap (Data Structure) - Wiki
Heap (Data Structure) - Wiki
org/wiki/Heap_(data_structure)
Contents
1 Variants
2 Comparison of theoretic bounds for variants
3 Heap applications
4 Heap implementations
5 See also
6 References
Variants
2-3 heap
Beap
Binary heap
Binomial heap
D-ary heap
Fibonacci heap
Leftist heap
Pairing heap
Skew heap
Soft heap
1 of 3 06/04/2010 22:03
Heap (data structure) - Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Heap_(data_structure)
Ternary heap
Treap
For pairing heaps the insert and merge operations have O(1) amortized complexity[2]. decreaseKey is not O(1)
amortized complexity.[3][4]
Heap applications
The heap data structure has many applications.
Heapsort: One of the best sorting methods being in-place and with no quadratic worst-case scenarios.
Selection algorithms: Finding the min, max, both the min and max, median, or even the k-th largest
element can be done in linear time using heaps.
Graph algorithms: By using heaps as internal traversal data structures, run time will be reduced by an
order of polynomial. Examples of such problems are Prim's minimal spanning tree algorithm and Dijkstra's
shortest path problem.
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.
Heap implementations
The C++ Standard Template Library provides the make_heap, push_heap and pop_heap algorithms for
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 detailed above.
2 of 3 06/04/2010 22:03
Heap (data structure) - Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Heap_(data_structure)
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 (http://search.cpan.org
/perldoc?Heap) distribution available on CPAN
See also
Sorting algorithm
Fibonacci heap
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, 1851, Springer-Verlag, pp. 63–77, doi:10.1007/3-540-44985-X_5
(http://dx.doi.org/10.1007%2F3-540-44985-X_5) .
3. ^ "CiteSeerX — Pairing Heaps are Sub-optimal" (http://citeseer.ist.psu.edu/112443.html) . Citeseer.ist.psu.edu.
http://citeseer.ist.psu.edu/112443.html. Retrieved 2009-09-23.
4. ^ "On the efficiency of pairing heaps and related data structures" (http://portal.acm.org/citation.cfm?id=320214) .
Portal.acm.org. 2003-07-31. doi:10.1007/BF01840439 (http://dx.doi.org/10.1007%2FBF01840439) .
http://portal.acm.org/citation.cfm?id=320214. Retrieved 2009-09-23.
Retrieved from "http://en.wikipedia.org/wiki/Heap_(data_structure)"
Categories: Heaps (structure)
3 of 3 06/04/2010 22:03