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)
(section)
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.
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)