Heap (data structure): Difference between revisions

Content deleted Content added
No edit summary
Line 4:
In [[computer science]], a '''heap''' is a specialized [[Tree (data structure)|tree]]-based [[data structure]] which is essentially an almost 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> tree that satisfies the '''heap property''': in a ''max heap'', for any given [[Node (computer science)|node]] C, if P is a 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]] (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 a smallest possible height—a heap with ''N'' nodes and for each node ''a'' branches always has log<sub>''a''</sub> ''N'' height.