Content deleted Content added
m Reverted edits by 202.127.8.250 to last version by Dysprosia |
first pass at cleaning this up |
||
Line 1:
[[Category:Trees (structure)]]
'''Binary heaps''' are a particular kind of [[heap]] [[data structure]] such that each node only has two children
== Min-heaps and max-heaps ==
Heaps can be organized so that the largest element in the heap is at the root of the tree always, or that the smallest element of the heap is at the root. These heaps are termed ''max-heaps'' and ''min-heaps'' respectively. Conventionally, max-heaps are used, since they are readily applicable for use in [[priority queue]]s.
1 11
Line 12 ⟶ 13:
8 9 10 11 1 2 3 4
Binary heaps are also commonly represented by [[array]]s of values. For example, the root of the tree could be item 0 in the array, and the children of item ''n'' are the items at 2''n''+1 and 2''n''+2; so item 0's children are items 1 and 2, 1's children are items 3 and 4, etc. This allows you to regard any 1-based array as a possibly-screwed-up heap.
The fact that
== Heap maintenance ==
If we have a heap, and we add an element, we can perform an operation known as ''up-heap'', ''bubble-up'', or ''sift-up'' in order to restore the heap property. We can do this in [[Big O notation|O]](log ''n'') time, using a binary heap, by adding the element on the bottom level of the heap regardless, then considering the added element and it's parent and swapping the element and its parent if need be until we are assured the heap property remains. We do this at maximum for each level in the tree - the height of the tree, which is O(log ''n'').
==External links==
|