Information about Heap (data Structure)



Enlarge picture
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 the 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.) This is why heaps are used to implement priority queues. The efficiency of heap operations is crucial in several graph algorithms.

The operations commonly performed with a heap are
  • 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 called heapsort.

Variants

Comparison of theoretic bounds for variants

The following complexities[1] are worst-case for binary and binomial heaps and amortized complexity for Fibonacci heap. 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
createHeapΘ(n)Θ(1)Θ(1)
findMinΘ(1)O(lg n) or Θ(1)Θ(1)
deleteMinΘ(lg n)Θ(lg n)O(lg n)
insertΘ(lg n)O(lg n)Θ(1)
decreaseKeyΘ(lg n)Θ(lg n)Θ(1)
mergeΘ(n)O(lg n)Θ(1)


For pairing heaps the insert, decreaseKey and merge operations are conjectured to be O amortized complexity but this has not yet been proven.

Heap applications

Heaps are favorite data structures for many applications. Interestingly, binary heaps may be represented 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. 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.

See also

Notes

1. ^ Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest (1990): Introduction to algorithms. MIT Press / McGraw-Hill.

External links

In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. It is a way of distributing ownership of limited memory resources among many pieces of data and code.
..... Click the link for more information.
Computer science, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems.
..... Click the link for more information.
tree is a widely-used data structure that emulates a tree structure with a set of linked nodes.

Nodes

A node may contain a value or a condition or represents a separate data structure or a tree of its own.
..... Click the link for more information.
data structure is a way of storing data in a computer so that it can be used efficiently. Often a carefully chosen data structure will allow the most efficient algorithm to be used. The choice of the data structure often begins from the choice of an abstract data type.
..... Click the link for more information.
tree is a widely-used data structure that emulates a tree structure with a set of linked nodes.

Nodes

A node may contain a value or a condition or represents a separate data structure or a tree of its own.
..... Click the link for more information.
A priority queue is an abstract data type in computer programming, supporting the following three operations:
  • add an element to the queue with an associated priority
  • remove the element from the queue that has the highest priority, and return it

..... Click the link for more information.
graph theory is the study of graphs; mathematical structures used to model pairwise relations between objects from a certain collection. A "graph" in this context refers to a collection of vertices or 'nodes' and a collection of edges
..... Click the link for more information.
In mathematics, computing, linguistics, and related disciplines, an algorithm is a finite list of well-defined instructions for accomplishing some task that, given an initial state, will proceed through a well-defined series of successive states, eventually terminating in an
..... Click the link for more information.
Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines than a good implementation of quicksort, it has the advantage of a worst-case O(n log n) runtime.
..... Click the link for more information.
Binary heaps are a particularly simple kind of heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
  • The shape property

..... Click the link for more information.
In computer science, a binomial heap is a data structure similar to binary heap but also supporting the operation of merging two heaps quickly. This is achieved by using a special tree structure.
..... Click the link for more information.
In computer science, a Fibonacci heap is a heap data structure consisting of a forest of trees. It has a better amortized running time than a binomial heap. Fibonacci heaps were developed by Michael L. Fredman and Robert E.
..... Click the link for more information.
Pairing heaps are a type of heap data structure with relatively simple implementation, excellent practical amortized performance (particularly for applications where heaps must be merged).
..... Click the link for more information.
A leftist tree or leftist heap is a priority queue implemented with a variant of a binary tree. Leftist trees were invented by Clark Allan Crane and uses the heap structure. In contrast to a binary heap, a leftist tree attempts to be very unbalanced.
..... Click the link for more information.
soft heap, designed by Bernard Chazelle in 2000, is a variant on the simple heap data structure. By carefully "corrupting" (increasing) the keys of at most a certain fixed percentage of values in the heap, it is able to achieve amortized constant-time bounds for all five of its
..... Click the link for more information.
In computer science, a 2-3 heap is a data structure, a variation on the heap, designed by Tadao Takaoka in 1999. The structure is similar to the Fibonacci heap, and borrows from the 2-3 tree.
..... Click the link for more information.
In computer science, a treap is a binary search tree that orders the nodes by adding a priority attribute to a node, as well as a key. [1] The nodes are ordered so that the keys form a binary search tree and the priorities obey the max heap order property.
..... Click the link for more information.
Beap, short for bi-parental heap, introduced by Ian Munro and Hendra Suwanda. In this data structure a node usually has two parents (unless it is the first or last on a level) and two children (unless it is on the last level).
..... Click the link for more information.
A skew heap is a variant of a binary heap. In contrast to e.g. leftist heaps, there is no structural constraint on skew heaps.

There are only two constraints left:
  • The general heap order must be enforced

..... Click the link for more information.
amortized analysis refers to finding the average running time per operation over a worst-case sequence of operations. Amortized analysis differs from average-case performance in that probability is not involved; amortized analysis guarantees the time per operation over
..... Click the link for more information.
In computational complexity theory, big O notation is often used to describe how the size of the input data affects an algorithm's usage of computational resources (usually running time or memory).
..... Click the link for more information.
Binary heaps are a particularly simple kind of heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
  • The shape property

..... Click the link for more information.
Binary heaps are a particularly simple kind of heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
  • The shape property

..... Click the link for more information.
In computer science, a binomial heap is a data structure similar to binary heap but also supporting the operation of merging two heaps quickly. This is achieved by using a special tree structure.
..... Click the link for more information.
In computer science, a Fibonacci heap is a heap data structure consisting of a forest of trees. It has a better amortized running time than a binomial heap. Fibonacci heaps were developed by Michael L. Fredman and Robert E.
..... Click the link for more information.
Pairing heaps are a type of heap data structure with relatively simple implementation, excellent practical amortized performance (particularly for applications where heaps must be merged).
..... Click the link for more information.
amortized analysis refers to finding the average running time per operation over a worst-case sequence of operations. Amortized analysis differs from average-case performance in that probability is not involved; amortized analysis guarantees the time per operation over
..... Click the link for more information.
Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines than a good implementation of quicksort, it has the advantage of a worst-case O(n log n) runtime.
..... Click the link for more information.
In computer science, a selection algorithm is an algorithm for finding the kth smallest number in a list, called order statistics. This includes the cases of finding the minimum, maximum, and median elements. There are worst-case linear time selection algorithms.
..... Click the link for more information.
A sublinear function, in linear algebra and related areas of mathematics, is a function on a vector space V over F, an ordered field (e.g. the real numbers ), which satisfies, for all scalars γ and vectors x and y
(

..... Click the link for more information.


This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus


page counter