Binary heaps are a particularly simple kind of heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
"Greater than" means according to whatever comparison function is chosen to sort the heap, not necessarily "greater than" in the mathematical sense (since the quantities are not even always numerical). Heaps where the comparison function is mathematical "greater than" are called max-heaps; those where the comparison function is mathematical "less than" are called "min-heaps". Conventionally, min-heaps are used, since they are readily applicable for use in priority queues.
Note that the ordering of siblings in a heap is not specified by the heap property, so the two children of a parent can be freely interchanged (as long as this does not violate the shape property, compare with treap).
We do this at maximum for each level in the tree — the height of the tree, which is O(log n). However, since approximately 50% of the elements are leaves and 75% are in the bottom two levels, it is likely that the new element to be inserted will only move a few levels upwards to maintain the heap. Thus, binary heaps support insertion in average constant time, O(1).
Say we have a max-heap
and we want to add the number 15 to the heap. We first place the 15 in the position marked by the X. However the heap property is violated since 15 is greater than 8, so we need to swap the 15 and the 8. So, we have the heap looking as follows after the first swap:
However the heap property is still violated since 15 is greater than 11, so we need to swap again:
which is a valid max-heap.
Now the heap property is violated since 8 is greater than 4. If we swap these two elements, we have restored the heap property (Note: 4 is swapped with its larger child, if this were a min-heap it would have been swapped with its smaller child) and we need not swap elements further:
However, a more common approach is to store the heap in an array. Any binary tree can be stored in an array, but because a heap is always an almost complete binary tree, it can be stored compactly. No space is required for pointers; instead, for each index i, element ais the parent of two children ai+2" target="_blank" >*," target="_blank" >as shown in the figure. (Note that with an implementation starting at 0 for the root, the parent of a*. ) This is a simple example of an implicit data structure.
This approach is particularly useful in the heapsort algorithm, where it allows the space in the input array to be reused to store the heap.
The upheap/downheap operations can be stated then in terms of an array as follows: suppose that the heap property holds for the indices b, b+1, ..., e. The sift-down function extends the heap property to b-1, b, b+1, ..., e. Only index i = b-1 can violate the heap property. Let j be the index of the largest child of a* (for a max-heap, or the smallest child for a min-heap) within the range b, ..., e. (If no such index exists because 2i > e then the heap property holds for the newly extended range and nothing needs to be done.) By swapping the values aand a[j the heap property for position i is established. At this point, the only problem is that the heap property might not hold for index j. The sift-down function is applied tail-recursively to index j until the heap property is established for all elements.
The sift-down function is fast. In each step it only needs two comparisons and one swap. The index value where it is working doubles in each iteration, so that at most log2 e steps are required.
The merge operation in a binary heap takes Θ(n) for equal-sized heaps. When merging is a common task, a different heap implementation is recommended, such as a binomial heap.
Binärer Heap | 二分ヒープ | Kopiec binarny | 堆
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Binary heap".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world