This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Binary heap From Wikipedia, the free encyclopedia A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints: ¡ The shape property : all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right. ¡ The heap property : each node is greater than or equal to each of its children according to some comparison predicate which is fixed for the entire data structure. “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 and heap property, compare with treap). The binary heap is a special case of the d-ary heap in which each node can have d children (rather than just 2). It is possible to modify the heap structure to allow extraction of both the smallest and largest element in O (lg n ) time  . To do this the rows alternate between min heap and max heap. The algorithms are roughly the same, but in each step must consider the alternating rows with alternating comparisons. The performance is roughly the same as a normal single direction heap. This idea can be generalised to a min-max-median heap. Heap operations Adding to the heap If we have a heap, and we add an element, we can perform an operation known as up-heap , bubble-up , percolate-up , or sift-up in order to restore the heap property. We can do this in O(log n ) time, using a binary heap, by following this algorithm: 1. Add the element on the bottom level of the heap....
View Full Document
This note was uploaded on 04/20/2010 for the course CS CS206 taught by Professor Lee during the Fall '09 term at 카이스트, 한국과학기술원.
- Fall '09