Heap (data structure)
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.) The maximum number of children each node can have depends on the type of heap, but in many types it is at most two. 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, and in the sorting algorithm heapsort.
A heap data structure should not be confused with the heap which is a common name for dynamically allocated memory. The term was originally used only for the data structure. Some early popular languages such as Lisp provided dynamic memory allocation using heap data structures, which gave the memory area its name.[1]
Contents |
Implementation and operations
Heaps are usually implemented in an array, and do not require pointers between elements.
The operations commonly performed with a heap are:
- create-heap: create an empty heap
- 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.
Different types of heaps implement the operations in different ways, but notably, insertion is often done by adding the new element at the end of the heap in the first available free space. This will tend to violate the heap property, and so the elements are then reordered until the heap property has been reestablished.
Variants
- 2-3 heap
- Beap
- Binary heap
- Binomial heap
- Brodal queue
- D-ary heap
- Fibonacci heap
- Leftist heap
- Pairing heap
- Skew heap
- Soft heap
- Leaf heap
- Radix heap
- Randomized meldable heap
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[1] | Binomial[1] | Fibonacci[1] | Pairing[2] | Brodal[3] |
---|---|---|---|---|---|
create-heap | Θ(1) | Θ(1) | Θ(1) | ? | O(1) |
findMin | Θ(1) | O(log n) | Θ(1) | O(1)* | O(1) |
deleteMin | Θ(log n) | Θ(log n) | O(log n)* | O(log n)* | O(log n) |
insert | Θ(log n) | O(log n) | Θ(1) | O(1)* | O(1) |
decreaseKey | Θ(log n) | Θ(log n) | Θ(1)* | O(log n)* | O(1) |
merge | Θ(n) | O(log n)** | Θ(1) | O(1)* | O(1) |
(*)Amortized time
(**)Where n is the size of the larger heap
Heaps with n elements can be constructed bottom-up in O(n).[4]
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 (often constant time) using heaps.[5]
- Graph algorithms: By using heaps as internal traversal data structures, run time will be reduced by polynomial order. 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.
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. Container adaptor priority_queue also exists. However, there is no standard support for the decrease/increase-key operation. See also gheap - STL-like generalized heap implementation in C++ with D-heap and B-heap support.
- 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.
- The Go library contains a heap package with heap algorithms that operate on an arbitrary type that satisfied a given interface.
- Apple's Core Foundation library contains a CFBinaryHeap structure.
See also
- Sorting algorithm
- Stack (data structure)
- Queue (data structure)
- Tree (data structure)
- Treap, a form of binary search tree based on heap-ordered trees
References
- ^ a b c d e Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest (1990): Introduction to algorithms. MIT Press / McGraw-Hill.
- ^ 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://www.cs.au.dk/~gerth/papers/soda96.pdf
- ^ Goodrich, Michael T.; Tamassia, Roberto (2004). "7.3.6. Bottom-Up Heap Construction". Data Structures and Algorithms in Java (3rd ed.). pp. 338–341.
- ^ Frederickson, Greg N. (1993), "An Optimal Algorithm for Selection in a Min-Heap", Information and Computation, 104, Academic Press, pp. 197–214, doi:10.1006/inco.1993.1030, http://ftp.cs.purdue.edu/research/technical_reports/1991/TR%2091-027.pdf
External links
- Heap at Wolfram MathWorld
|
|