Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Heap (data structure)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Computer science data structure}} {{For|the memory heap (in low-level computer programming), which is unrelated to this data structure|C dynamic memory allocation}} [[File:Max-Heap-new.svg|thumb|Example of a [[Binary tree|binary]] max-heap with node keys being integers between 1 and 100]] In [[computer science]], a '''heap''' is a [[Tree (data structure)|tree]]-based [[data structure]] that satisfies the '''heap property''': In a ''max heap'', for any given [[Node (computer science)|node]] C, if P is the parent node of C, then the ''key'' (the ''value'') of P is greater than or equal to the key of C. In a ''min heap'', the key of P is less than or equal to the key of C.<ref>Black (ed.), Paul E. (2004-12-14). Entry for ''heap'' in ''[[Dictionary of Algorithms and Data Structures]]''. Online version. U.S. [[National Institute of Standards and Technology]], 14 December 2004. Retrieved on 2017-10-08 from https://xlinux.nist.gov/dads/HTML/heap.html.</ref> The node at the "top" of the heap (with no parents) is called the ''root'' node. The heap is one maximally efficient implementation of an [[abstract data type]] called a [[priority queue]], and in fact, priority queues are often referred to as "heaps", regardless of how they may be implemented. In a heap, the highest (or lowest) priority element is always stored at the root. However, a heap is not a sorted structure; it can be regarded as being partially ordered. A heap is a useful data structure when it is necessary to repeatedly remove the object with the highest (or lowest) priority, or when insertions need to be interspersed with removals of the root node. A common implementation of a heap is the [[binary heap]], in which the tree is a [[Binary_tree#Types_of_binary_trees|complete]]<ref>{{Cite book|title=INTRODUCTION TO ALGORITHMS| last=CORMEN|first=THOMAS H.|publisher=The MIT Press Cambridge, Massachusetts London, England|year=2009| isbn=978-0-262-03384-8|location=United States of America|pages=151β152}}</ref> binary tree (see figure). The heap data structure, specifically the binary heap, was introduced by [[J. W. J. Williams]] in 1964, as a data structure for the [[heapsort]] sorting algorithm.<ref>{{Citation |first=J. W. J. |last=Williams |author-link=J. W. J. Williams |title=Algorithm 232 - Heapsort |year=1964 |journal=[[Communications of the ACM]] |volume=7 |issue=6 |pages=347β348 |doi= 10.1145/512274.512284}}</ref> Heaps are also crucial in several efficient [[graph algorithms]] such as [[Dijkstra's algorithm]]. When a heap is a complete binary tree, it has the smallest possible heightβa heap with ''N'' nodes and ''a'' branches for each node always has log<sub>''a''</sub> ''N'' height. Note that, as shown in the graphic, there is no implied ordering between siblings or cousins and no implied sequence for an [[Inorder traversal|in-order traversal]] (as there would be in, e.g., a [[binary search tree]]). The heap relation mentioned above applies only between nodes and their parents, grandparents. The maximum number of children each node can have depends on the type of heap. Heaps are typically constructed in-place in the same array where the elements are stored, with their structure being implicit in the access pattern of the operations. Heaps differ in this way from other data structures with similar or in some cases better theoretic bounds such as [[radix tree]]s in that they require no additional memory beyond that used for storing the keys. ==Operations== The common operations involving heaps are: ;Basic * ''find-max'' (or ''find-min''): find a maximum item of a max-heap, or a minimum item of a min-heap, respectively (a.k.a. ''[[Peek (data type operation)|peek]]'') * ''insert'': adding a new key to the heap (a.k.a., ''push''<ref>The Python Standard Library, 8.4. heapq β Heap queue algorithm, [https://docs.python.org/3/library/heapq.html#heapq.heappush heapq.heappush]</ref>) * ''extract-max'' (or ''extract-min''): returns the node of maximum value from a max heap [or minimum value from a min heap] after removing it from the heap (a.k.a., ''pop''<ref>The Python Standard Library, 8.4. heapq β Heap queue algorithm, [https://docs.python.org/3/library/heapq.html#heapq.heappop heapq.heappop]</ref>) * ''delete-max'' (or ''delete-min''): removing the root node of a max heap (or min heap), respectively * ''replace'': pop root and push a new key. This is more efficient than a pop followed by a push, since it only needs to balance once, not twice, and is appropriate for fixed-size heaps.<ref>The Python Standard Library, 8.4. heapq β Heap queue algorithm, [https://docs.python.org/3/library/heapq.html#heapq.heapreplace heapq.heapreplace]</ref> ;Creation * ''create-heap'': create an empty heap * ''heapify'': create a heap out of given array of elements * ''merge'' (''union''): joining two heaps to form a valid new heap containing all the elements of both, preserving the original heaps. * ''meld'': joining two heaps to form a valid new heap containing all the elements of both, destroying the original heaps. ;Inspection * ''size'': return the number of items in the heap. * ''is-empty'': return true if the heap is empty, false otherwise. ;Internal * ''increase-key'' or ''decrease-key'': updating a key within a max- or min-heap, respectively * ''delete'': delete an arbitrary node (followed by moving last node and sifting to maintain heap) * ''sift-up'': move a node up in the tree, as long as needed; used to restore heap condition after insertion. Called "sift" because node moves up the tree until it reaches the correct level, as in a [[sieve]]. * ''sift-down'': move a node down in the tree, similar to sift-up; used to restore heap condition after deletion or replacement. ==Implementation using arrays== Heaps are usually implemented with an [[array data structure|array]], as follows: * Each element in the array represents a node of the heap, and * The parent / child relationship is [[implicit data structure|defined implicitly]] by the elements' indices in the array. [[File:Heap-as-array.svg|thumb|upright=1.5|Example of a complete binary max-heap with node keys being integers from 1 to 100 and how it would be stored in an array.]] For a [[binary heap]], in the array, the first index contains the root element. The next two indices of the array contain the root's children. The next four indices contain the four children of the root's two child nodes, and so on. Therefore, given a node at index {{mvar|i}}, its children are at indices {{tmath|2i + 1}} and {{tmath|2i + 2}}, and its parent is at index {{math|β(''i''β1)/2β}}. This simple indexing scheme makes it efficient to move "up" or "down" the tree. Balancing a heap is done by sift-up or sift-down operations (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. After an element is inserted into or deleted from a heap, the heap property may be violated, and the heap must be re-balanced by swapping elements within the array. Although different types of heaps implement the operations differently, the most common way is as follows: * '''Insertion:''' Add the new element at the end of the heap, in the first available free space. If this will violate the heap property, sift up the new element (''swim'' operation) until the heap property has been reestablished. * '''Extraction:''' Remove the root and insert the last element of the heap in the root. If this will violate the heap property, sift down the new root (''sink'' operation) to reestablish the heap property. * '''Replacement:''' Remove the root and put the ''new'' element in the root and sift down. When compared to extraction followed by insertion, this avoids a sift up step. Construction of a binary (or ''d''-ary) heap out of a given array of elements may be performed in linear time using the classic [[Heapsort#Variations|Floyd algorithm]], with the worst-case number of comparisons equal to 2''N'' β 2''s''<sub>2</sub>(''N'') β ''e''<sub>2</sub>(''N'') (for a binary heap), where ''s''<sub>2</sub>(''N'') is the sum of all digits of the binary representation of ''N'' and ''e''<sub>2</sub>(''N'') is the exponent of 2 in the prime factorization of ''N''.<ref>{{citation | last1 = Suchenek | first1 = Marek A. | title = Elementary Yet Precise Worst-Case Analysis of Floyd's Heap-Construction Program | doi = 10.3233/FI-2012-751 | pages = 75β92 | publisher = IOS Press | journal = Fundamenta Informaticae | volume = 120 | issue = 1 | year = 2012}}.</ref> This is faster than a sequence of consecutive insertions into an originally empty heap, which is log-linear.{{efn|Each insertion takes O(log(''k'')) in the existing size of the heap, thus <math>\sum_{k=1}^n O(\log k)</math>. Since <math>\log n/2 = (\log n) - 1</math>, a constant factor (half) of these insertions are within a constant factor of the maximum, so asymptotically we can assume <math>k = n</math>; formally the time is <math>n O(\log n) - O(n) = O(n \log n)</math>. This can also be readily seen from [[Stirling's approximation]].}} ==Variants== {{div col|colwidth=10em}} * [[2β3 heap]] * [[B-heap]] * [[Beap]] * [[Binary heap]] * [[Binomial heap]] * [[Brodal queue]] * [[D-ary heap|''d''-ary heap]] * [[Fibonacci heap]] * [[K-D Heap]] * [[Leaf heap]] * [[Leftist tree|Leftist heap]] * [[Skew binomial heap]] * [[Strict Fibonacci heap]] * [[Min-max heap]] * [[Pairing heap]] * [[Radix heap]] * [[Randomized meldable heap]] * [[Skew heap]] * [[Soft heap]] * [[Ternary heap]] * [[Treap]] * [[Weak heap]] {{div col end}} ==Comparison of theoretic bounds for variants== {{Heap Running Times | mode = max}} ==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 algorithm]]s: A heap allows access to the min or max element in constant time, and other selections (such as median or kth-element) can be done in sub-linear time on data that is in a heap.<ref>{{citation | last = Frederickson | first = Greg N. | contribution = An Optimal Algorithm for Selection in a Min-Heap | doi = 10.1006/inco.1993.1030 | pages = 197β214 | publisher = Academic Press | title = Information and Computation | volume = 104 | issue = 2 | year = 1993 | url = http://ftp.cs.purdue.edu/research/technical_reports/1991/TR%2091-027.pdf | access-date = 2010-10-31 | archive-url = https://web.archive.org/web/20121203045606/http://ftp.cs.purdue.edu/research/technical_reports/1991/TR%2091-027.pdf | archive-date = 2012-12-03 | url-status = dead | doi-access = free }}</ref> * [[List of algorithms#Graph algorithms|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 algorithm|Prim's minimal-spanning-tree algorithm]] and [[Dijkstra's algorithm|Dijkstra's shortest-path algorithm]]. *[[Priority queue]]: A priority queue is an abstract concept like "a list" or "a map"; just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods. *[[K-way merge algorithm|K-way merge]]: A heap data structure is useful to merge many already-sorted input streams into a single sorted output stream. Examples of the need for merging include external sorting and streaming results from distributed data such as a log structured merge tree. The inner loop is obtaining the min element, replacing with the next element for the corresponding input stream, then doing a sift-down heap operation. (Alternatively the replace function.) (Using extract-max and insert functions of a priority queue are much less efficient.) ==Programming language implementations== * The [[C++ Standard Library]] provides the {{mono|make_heap}}, {{mono|push_heap}} and {{mono|pop_heap}} algorithms for heaps (usually implemented as binary heaps), which operate on arbitrary random access [[iterator]]s. It treats the iterators as a reference to an array, and uses the array-to-heap conversion. It also provides the container adaptor {{mono|priority_queue}}, which wraps these facilities in a container-like class. However, there is no standard support for the replace, sift-up/sift-down, or decrease/increase-key operations. * The [[Boost (C++ libraries)|Boost C++ libraries]] include a heaps library. Unlike the STL, it supports decrease and increase operations, and supports additional types of heap: specifically, it supports ''d''-ary, binomial, Fibonacci, pairing and skew heaps. * There is a [https://github.com/valyala/gheap generic heap implementation] for [[C (programming language)|C]] and [[C++]] with [[D-ary heap]] and [[B-heap]] support. It provides an STL-like API. * The standard library of the [[D (programming language)|D programming language]] includes [https://dlang.org/phobos/std_container_binaryheap.html {{mono|std.container.BinaryHeap}}], which is implemented in terms of D's [https://tour.dlang.org/tour/en/basics/ranges ranges]. Instances can be constructed from any [https://dlang.org/phobos/std_range_primitives.html#isRandomAccessRange random-access range]. {{mono|BinaryHeap}} exposes an [https://dlang.org/phobos/std_range_primitives.html#isInputRange input range interface] that allows iteration with D's built-in {{mono|foreach}} statements and integration with the range-based API of the [https://dlang.org/phobos/std_algorithm.html {{mono|std.algorithm}} package]. * For [[Haskell]] there is the [https://hackage.haskell.org/package/heaps {{mono|Data.Heap}}] module. * The [[Java (programming language)|Java]] platform (since version 1.5) provides a binary heap implementation with the class {{Javadoc:SE|package=java.util|java/util|PriorityQueue}} in the [[Java Collections Framework]]. This class implements by default a min-heap; to implement a max-heap, programmer should write a custom comparator. There is no support for the replace, sift-up/sift-down, or decrease/increase-key operations. * [[Python (programming language)|Python]] has a [https://docs.python.org/library/heapq.html {{mono|heapq}}] module that implements a priority queue using a binary heap. The library exposes a heapreplace function to support k-way merging. Python only supports a min-heap implementation. * [[PHP]] has both max-heap ({{mono|SplMaxHeap}}) and min-heap ({{mono|SplMinHeap}}) as of version 5.3 in the Standard PHP Library. * [[Perl]] has implementations of binary, binomial, and Fibonacci heaps in the [https://metacpan.org/module/Heap {{mono|Heap}}] distribution available on [[CPAN]]. * The [[Go (programming language)|Go]] language contains a [http://golang.org/pkg/container/heap/ {{mono|heap}}] package with heap algorithms that operate on an arbitrary type that satisfies a given interface. That package does not support the replace, sift-up/sift-down, or decrease/increase-key operations. * Apple's [[Core Foundation]] library contains a [https://developer.apple.com/documentation/corefoundation/cfbinaryheap {{mono|CFBinaryHeap}}] structure. * [[Pharo]] has an implementation of a heap in the Collections-Sequenceable package along with a set of test cases. A heap is used in the implementation of the timer event loop. * The [[Rust (programming language)|Rust]] programming language has a binary max-heap implementation, [https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html {{mono|BinaryHeap}}], in the {{mono|collections}} module of its standard library. * [[.NET]] has [https://docs.microsoft.com/dotnet/api/system.collections.generic.priorityqueue-2 PriorityQueue] class which uses quaternary (d-ary) min-heap implementation. It is available from .NET 6. ==See also== * [[Sorting algorithm]] * [[Search data structure]] * [[Stack (abstract data type)]] * [[Queue (abstract data type)]] * [[Tree (data structure)]] * [[Treap]], a form of binary search tree based on heap-ordered trees ==References== {{Reflist}} ==External links== {{Commons category|Heap data structures}} {{Wikibooks|Data Structures|Min and Max Heaps}} *[http://mathworld.wolfram.com/Heap.html Heap] at Wolfram MathWorld *[https://www.cs.auckland.ac.nz/software/AlgAnim/heaps.html Explanation] of how the basic heap algorithms work *{{cite book|last1=Bentley|first1=Jon Louis|title=Programming Pearls|date=2000|publisher=Addison Wesley| isbn=0201657880|pages=147β162|edition=2nd}} {{CS-Trees}} {{Data structures}} {{DEFAULTSORT:Heap (Data Structure)}} [[Category:Heaps (data structures)| ]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:CS-Trees
(
edit
)
Template:Citation
(
edit
)
Template:Cite book
(
edit
)
Template:Commons category
(
edit
)
Template:Data structures
(
edit
)
Template:Div col
(
edit
)
Template:Div col end
(
edit
)
Template:Efn
(
edit
)
Template:For
(
edit
)
Template:Heap Running Times
(
edit
)
Template:Javadoc:SE
(
edit
)
Template:Math
(
edit
)
Template:Mono
(
edit
)
Template:Mvar
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Tmath
(
edit
)
Template:Wikibooks
(
edit
)