Content deleted Content added
m →Implementation: * {{tmath}} |
→See also: Removed two overly tangential links Tags: Mobile edit Mobile web edit Advanced mobile edit |
||
(56 intermediate revisions by 47 users not shown) | |||
Line 1:
{{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
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
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
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==
Line 14 ⟶ 16:
;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/
* ''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/
* ''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.
;Creation
Line 35 ⟶ 37:
* ''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:
Line 48 ⟶ 50:
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
* '''Insertion:''' Add the new element at the end of the heap, in the first available free space.
* '''Extraction:''' Remove the root and insert the last element of the heap in the root.
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
Line 77 ⟶ 79:
* [[Leaf heap]]
* [[Leftist tree|Leftist heap]]
* [[Skew binomial heap]]
* [[Strict Fibonacci heap]]
* [[Min-max heap]]
* [[Pairing heap]]
* [[Radix heap]]
Line 113 ⟶ 118:
}}</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
*[[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==
Line 122 ⟶ 126:
* 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/
* [[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]]
* [[Treap]], a form of binary search tree based on heap-ordered trees
|