|
In computer science, a heap is a specialized tree-based data structure that satisfies the ''heap property:'' If A is a parent node of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap. A heap can be classified further as either a "max heap" or a "min heap". In a max heap, the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node. In a min heap, the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node. Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm, and in the sorting algorithm heapsort. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree (see figure). In a heap, the highest (or lowest) priority element is always stored at the root, hence the name heap. A heap is not a sorted structure and can be regarded as partially ordered. As visible from the heap-diagram, there is no particular relationship among nodes on any given level, even among the siblings. When a heap is a complete binary tree, it has a smallest possible height—a heap with N nodes always has log N height. A heap is a useful data structure when you need to remove the object with the highest (or lowest) priority. Note that, as shown in the graphic, there is no implied ordering between siblings or cousins and no implied sequence for an 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, etc. The maximum number of children each node can have depends on the type of heap, but in many types it is at most two, which is known as a binary heap. 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. Note that despite the similarity of the name "heap" to "stack" and "queue", the latter two are abstract data types, while a heap is a specific data structure, and "priority queue" is the proper term for the abstract data type. A ''heap'' data structure should not be confused with ''the heap'' which is a common name for the pool of memory from which dynamically allocated memory is allocated. The term was originally used only for the data structure. ==Operations== The common operations involving heaps are: ;Basic * ''find-max'' or ''find-min'': find the maximum item of a max-heap or a minimum item of a min-heap (a.k.a. ''peek'') * ''insert'': adding a new key to the heap (a.k.a., ''push''〔The Python Standard Library, 8.4. heapq — Heap queue algorithm, (heapq.heappush )〕) * ''extract-min'' (''extract-max'' ): returns the node of minimum value from a min heap (maximum value from a max heap ) after removing it from the heap (a.k.a., ''pop''〔The Python Standard Library, 8.4. heapq — Heap queue algorithm, (heapq.heappop )〕) * ''delete-max'' or ''delete-min'': removing the root node of a max- or min-heap, respectively * ''replace'': pop root and push a new key. More efficient than pop followed by push, since only need to balance once, not twice, and appropriate for fixed-size heaps.〔The Python Standard Library, 8.4. heapq — Heap queue algorithm, (heapq.heapreplace )〕 ;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 – an optimized form of size when total size is not needed. ;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. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Heap (data structure)」の詳細全文を読む スポンサード リンク
|