Heap (data structure)
From Wikipedia, the free encyclopedia
In computer science, a heap is a specialized treebased 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 maxheap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a minheap.) The several variants of heaps are the prototypical most efficient implementations of the abstract data type priority queues. Priority queues are useful in many applications. In particular, heaps are crucial in several efficient graph algorithms.
The operations commonly performed with a heap are
 deletemax or deletemin: removing the root node of a max or minheap, respectively
 increasekey or decreasekey: updating a key within a max or minheap, 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.
Contents 
[edit] Variants
 Binary heap
 Binomial heap
 Dary heap
 Fibonacci heap
 Pairing heap
 Leftist heap
 Soft heap
 23 heap
 Ternary heap
 Treap
 Beap
 Skew heap
[edit] Comparison of theoretic bounds for variants
The following time complexities^{[1]} are worstcase time for binary and binomial heaps and amortized time complexity for Fibonacci heap. O(f) gives asymptotic upper bound and Θ(f) is asymptotically tight bound (see Big O notation). Function names assume a minheap.
Operation  Binary  Binomial  Fibonacci 

createHeap  Θ(n)  Θ(n)  Θ(n) 
findMin  Θ(1)  Θ(log n) or Θ(1)  Θ(1) 
deleteMin  Θ(log n)  Θ(log n)  O(log n) 
insert  Θ(log n)  O(log n)  Θ(1) 
decreaseKey  Θ(log n)  Θ(log n)  Θ(1) 
merge  Θ(n)  O(log n)  Θ(1) 
For pairing heaps the insert and merge operations are conjectured^{[citation needed]} to be O(1) amortized complexity but this has not yet been proven. decreaseKey is not O(1) amortized complexity [1] [2]
[edit] Heap applications
Heaps are a favorite data structure for many applications.
 Heapsort: One of the best sorting methods being inplace and with no quadratic worstcase scenarios.
 Selection algorithms: Finding the min, max or both of them, median or even any kth element in sublinear time^{[citation needed]} can be done dynamically with 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.
Interestingly, full and almost full binary heaps may be represented in a very spaceefficient 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 onebased array, or 2n+1
and 2n+2
in a zerobased 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 inplace.
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.
[edit] 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 arraytoheap conversion detailed above.
[edit] See also
[edit] References
Wikimedia Commons has media related to: Heaps 
 ^ Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest (1990): Introduction to algorithms. MIT Press / McGrawHill.
