Vigenère cipher: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Jc3s5h
Undid revision 509622524 by 90.199.116.9 (talk). Unreliable source
 
en>Soham
m Reverted 3 edits by 91.62.255.201 identified as test/vandalism using STiki
Line 1: Line 1:
When in search of the appropriate hemorrhoid treatment you'll need to consider a few important factors such as, which one we think you'd like, if there is a wonderful amount of healing needed, plus how lengthy it takes to get results. In this particular article we will learn the answers to all of these concerns, giving you the answer we have to discover the proper hemorrhoid treatment.<br><br>If you have been seeing blood on the toilet paper, you are possibly inside the late stages of this condition and want a surprisingly efficient [http://hemorrhoidtreatmentfix.com hemorrhoid treatment].<br><br>Take a "sitz" bathtub with warm water. Do this 3 or 4 time a day for 15 minutes each time. This treatment is quite effective at reducing pain and swelling and is actually effortless to do.<br><br>There are many people that are making a research about the cause of hemorrhoid. Actually there are many items that could result you hemorrhoid. However when you are going to ask those wellness services and practitioners, they usually tell you that hemorrhoid is due to the straining throughout bowel movement. They also stated which it caused by irregularity, bad diet, pregnancy, diarrhea, genetics plus incorrect bathroom practices. There are furthermore instances it is caused by liver cirrhosis.<br><br>There is also the matter of the "sitting time limit." Some practitioners suggest which people with hemorrhoids avoid sitting for lengthy periods of time. If it happens to be possible to receive your hands on an air doughnut, or any seat that refuses to cause discomfort whenever sitting, then do so.<br><br>Witch Hazel, you are able to carry this with we and employ it whenever you may be out. Pour a little of it on a toilet paper plus use it to wash yourself. Not only might you be cleaner, and this really is important to heal the hemorrhoids, however, Witch Hazel may actively shrink the hemorrhoids.<br><br>Undergoing with these choices usually surely cost you pricey. And for sure not all individuals will afford to pay such surgery. Then there are additionally hemorrhoids treatment which could be found at house. With these hemorrhoid treatments you can make sure that you'll not spend too much. In most instances, persons prefer to have natural treatment whilst the hemorrhoid continues to be on its mild stage. These natural treatments normally assist we inside reducing the pain and swelling. We never have to be concerned as we apply or employ them because they are easy plus affordable.
{{Infobox data structure
|name=Binary Heap
|type=tree
|
<!-- NOTE:
    Base of logarithms doesn't matter in big O notation. O(log n) is the same as O(lg n) or O(ln n) or O(log_2 n). A change of base is just a constant factor. So don't change these O(log n) complexities to O(lg n) or something else just to indicate a base-2 log. The base doesn't matter.
-->
|space_avg=O(n)
|space_worst=O(n)
|search_avg=Not supported
|search_worst=Not supported
|insert_avg=O(1)
|insert_worst=O(log n)
|delete_avg=O(log n)
|delete_worst=O(log n)
|peek_avg=O(1)
|peek_worst=O(1)
}}
[[File:Max-Heap.svg|thumb|right|240px|Example of a complete binary max heap]]
[[File:Min-heap.png|thumb|right|240px|Example of a complete binary min heap]]
A '''binary heap''' is a [[heap (data structure)|heap]] [[data structure]] created using a [[binary tree]]. It can be seen as a binary tree with two additional constraints:
 
;Shape property: The tree is a ''[[Complete Binary Tree|complete binary tree]]''; that is, 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.
;Heap property: All nodes are ''either ''[greater than or equal to] ''or ''[less than or equal to] each of its children, according to a comparison [[predicate (mathematical logic)|predicate]] defined for the heap.
 
Heaps with a mathematical "greater than or equal to" (≥) comparison predicate are called ''max-heaps''; those with a mathematical "less than or equal to" (≤) comparison predicate are called ''min-heaps''.  Min-heaps are often used to implement [[priority queue]]s.<ref>{{cite web
|url=http://docs.python.org/library/heapq.html
|title=heapq – Heap queue algorithm
|work=Python Standard Library
}}</ref><ref>{{cite web
|url=http://download.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html
|title=Class PriorityQueue
|work=Java™ Platform Standard Ed. 6
}}</ref>
 
Since the ordering of siblings in a heap is not specified by the heap property, a single node's two children can be freely interchanged unless doing so violates the shape property (compare with [[treap]]).
 
The binary heap is a special case of the [[d-ary heap]] in which d = 2.
 
== Heap operations ==
Both the insert and remove operations modify the heap to conform to the shape property first, by adding or removing from the end of the heap. Then the heap property is restored by traversing up or down the heap. Both operations take O(log ''n'') time.
 
=== Insert ===
To add an element to a heap we must perform an ''up-heap'' operation (also known as ''bubble-up'', ''percolate-up'', ''sift-up'', ''trickle up'', ''heapify-up'', or ''cascade-up''), by following this algorithm:
 
#Add the element to the bottom level of the heap.
#Compare the added element with its parent; if they are in the correct order, stop.
#If not, swap the element with its parent and return to the previous step.
 
The number of operations required is dependent on the number of levels the new element must rise to satisfy the heap property, thus the insertion operation has a time complexity of O(log ''n''). However, in 1974, Thomas Porter and Istvan Simon proved that the function for the average number of levels an inserted node moves up is upper bounded by the constant 1.6067.<ref>{{cite web|title=Random Insertion into a Priority Queue Structure|url=ftp://reports.stanford.edu/pub/cstr/reports/cs/tr/74/460/CS-TR-74-460.pdf|work=Stanford University Reports|publisher=Stanford University|accessdate=31 January 2014|author=Thomas Porter|coauthors=Istvan Simon|page=13|year=1974}}</ref> The average number of operations required for an insertion into a binary heap is 2.6067 since one additional comparison is made that does not result in the inserted node moving up a level. Thus, on average, binary heap insertion has a constant, O(1), time complexity.  Intuitively, this makes sense since approximately 50% of the elements are leaves and approximately 75% of the elements 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.
 
As an example of binary heap insertion, say we have a max-heap
 
::[[File:Heap add step1.svg|150px]]
 
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:
 
::[[File:Heap add step2.svg|150px]]
 
However the heap property is still violated since 15 is greater than 11, so we need to swap again:
 
::[[File:Heap add step3.svg|150px]]
 
which is a valid max-heap. There is no need to check the children after this. Before we placed 15 on X, the heap was valid, meaning 11 is greater than 5. If 15 is greater than 11, and 11 is greater than 5, then 15 must be greater than 5, because of the [[transitive relation]].
 
=== Delete ===
The procedure for deleting the root from the heap (effectively extracting the maximum element in a max-heap or the minimum element in a min-heap) and restoring the properties is called ''down-heap'' (also known as ''bubble-down'', ''percolate-down'', ''sift-down'', ''trickle down'', ''heapify-down'', ''cascade-down'' and ''extract-min/max'').
 
#Replace the root of the heap with the last element on the last level.
#Compare the new root with its children; if they are in the correct order, stop.
#If not, swap the element with one of its children and return to the previous step. (Swap with its smaller child in a min-heap and its larger child in a max-heap.)
 
So, if we have the same max-heap as before
 
::[[File:Heap delete step0.svg|150px]]
 
We remove the 11 and replace it with the 4.
 
::[[File:Heap remove step1.svg|150px]]
 
Now the heap property is violated since 8 is greater than 4. In this case, swapping the two elements, 4 and 8, is enough to restore the heap property and we need not swap elements further:
 
::[[File:Heap remove step2.svg|150px]]
 
The downward-moving node is swapped with the ''larger'' of its children in a max-heap (in a min-heap it would be swapped with its smaller child), until it satisfies the heap property in its new position. This functionality is achieved by the '''Max-Heapify''' function as defined below in [[pseudocode]] for an [[Array data structure|array]]-backed heap ''A'' of length ''heap_length''[''A''].  Note that "A" is indexed starting at 1, not 0 as is common in many real programming languages.
 
'''Max-Heapify'''<ref name="CLRS">{{Citation | last1= Cormen | first1=T. H. | last2=al. | lastauthoramp=yes | title=Introduction to Algorithms | publisher=The MIT Press | place=Cambridge, Massachusetts | edition=2nd | year=2001 | isbn=0-07-013151-1}}</ref> (''A'', ''i''):<br/>
{{pad|2em}}''left'' ← 2''i''<br/>
{{pad|2em}}''right'' ← 2''i'' + 1<br/>
{{pad|2em}}''largest'' ← ''i''<br/>
{{pad|2em}}'''if''' ''left'' ≤ ''heap_length''[''A''] '''and''' ''A''[''left''] > A[''largest''] '''then''':<br/>
{{pad|4em}}''largest'' ← ''left''<br/>
{{pad|2em}}'''if''' ''right'' ≤ ''heap_length''[''A''] '''and''' ''A''[''right''] > ''A''[''largest''] '''then''':<br/>
{{pad|4em}}''largest'' ← ''right''<br/>
{{pad|2em}}'''if''' ''largest'' ≠ ''i'' '''then''':<br/>
{{pad|4em}}'''swap''' ''A[''i''] ↔ ''A''[''largest'']<br/>
{{pad|4em}}Max-Heapify(''A'', ''largest'')
 
For the above algorithm to correctly re-heapify the array, the node at index ''i'' and its two direct children must violate the heap property. If they do not, the algorithm will fall through with no change to the array. The down-heap operation (without the preceding swap) can also be used to modify the value of the root, even when an element is not being deleted.
 
In the worst case, the new root has to be swapped with its child on each level until it reaches the bottom level of the heap, meaning that the delete operation has a time complexity relative to the height of the tree, or O(log ''n'').
 
==Building a heap==
A heap could be built by successive insertions. This approach requires <math>O(n \log n)</math> time because each insertion takes <math>O(\log n)</math> time and there are <math>n</math> elements. However this is not the optimal method. The optimal method starts by arbitrarily putting the elements on a binary tree, respecting the shape property (the tree could be represented by an array, see below). Then starting from the lowest level and moving upwards, shift the root of each subtree downward as in the deletion algorithm until the heap property is restored. More specifically if all the subtrees starting at some height <math>h</math> (measured from the bottom) have already been "heapified", the trees at height <math>h+1</math> can be heapified by sending their root down along the path of maximum valued children when building a max-heap, or minimum valued children when building a min-heap. This process takes <math>O(h)</math> operations (swaps) per node. In this method most of the heapification takes place in the lower levels. Since the height of the heap is <math> \left\lfloor \lg (n) \right\rfloor</math>, the number of nodes at height <math>h</math> is <math>\le \left\lceil 2^{\left(\lg n - h\right) - 1} \right\rceil = \left\lceil \frac{2^{\lg n}}{2^{h+1}}\right\rceil = \left\lceil\frac{n}{2^{h+1}}\right\rceil</math>. Therefore, the cost of heapifying all subtrees is:
 
:<math>
\begin{align}
\sum_{h=0}^{\lceil \lg n \rceil} \frac{n}{2^{h+1}}O(h) & =
O\left(n\sum_{h=0}^{\lceil \lg n \rceil} \frac{h}{2^{h + 1}}\right) \\
& \le O\left(n\sum_{h=0}^{\infty} \frac{h}{2^h}\right) \\
& = O(n)
 
\end{align}
</math>
 
This uses the fact that the given infinite [[series (mathematics)|series]] ''h'' / 2<sup>''h''</sup> [[Convergent series|converges]] to 2.
 
The exact value of the above (the worst-case number of comparisons during the heap construction) is known to be equal to:
 
:<math> 2 n - 2 s_2 (n) - e_2 (n) </math>,<ref>{{citation
| last1 = Suchenek | first1 = Marek A.
| title = Elementary Yet Precise Worst-Case Analysis of Floyd's Heap-Construction Program
| doi = 10.3233/FI-2012-751
| pages = 75–92
| publisher = IOS Press
| journal = Fundamenta Informaticae
| volume = 120
| issue = 1
| year = 2012
| url = http://www.deepdyve.com/lp/ios-press/elementary-yet-precise-worst-case-analysis-of-floyd-s-heap-50NW30HMxU}}.</ref>
where s<sub>2</sub>(n) is the sum of all digits of the binary representation of n and e<sub>2</sub>(n) is the exponent of 2 in the prime factorization of n.
 
The '''Build-Max-Heap''' function that follows, converts an array ''A'' which stores a complete
binary tree with n nodes to a max-heap by repeatedly using '''Max-Heapify''' in a bottom up manner.
It is based on the observation that the array elements indexed by
''[[floor function|floor]]''(n/2) + 1, ''floor''(n/2) + 2, ..., n
are all leaves for the tree, thus each is a one-element heap. '''Build-Max-Heap''' runs
'''Max-Heapify''' on each of the remaining tree nodes.
 
'''Build-Max-Heap'''<ref name="CLRS"/> (''A''):<br/>
{{pad|2em}}''heap_length''[''A''] ← ''length''[''A'']<br/>
{{pad|2em}}'''for''' ''i'' ← ''floor''(''length''[''A'']/2) '''downto''' 1 '''do'''<br/>
{{pad|4em}}'''Max-Heapify'''(''A'', ''i'')
 
== Heap implementation ==<!-- This section is linked from [[Heapsort]] -->
[[File:Binary tree in array.svg|300px|right|frame|A small complete binary tree stored in an array]]
[[File:Binary Heap with Array Implementation.JPG|400px|thumb|right|Comparison between a binary heap and an array implementation.]]
 
Heaps are commonly implemented with an [[Array data structure|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 [[pointer (computer programming)|pointer]]s; instead, the parent and children of each node can be found by arithmetic on array indices. These properties make this heap implementation a simple example of an [[implicit data structure]] or [[Binary tree#Ahnentafel list|Ahnentafel list]]. Details depend on the root position, which in turn may depend on constraints of a [[programming language]] used for implementation, or programmer preference. Specifically, sometimes the root is placed at index 1, sacrificing space in order to simplify arithmetic. The ''[[Peek (data type operation)|peek]]'' operation (''find-min'' or ''find-max'') simply returns the value of the root, and is thus O(1).
 
Let ''n'' be the number of elements in the heap and ''i'' be an arbitrary valid index of the array storing the heap. If the tree root is at index 0, with valid indices 0 through ''n − ''1, then each element ''a'' at index ''i'' has
* children at indices 2''i ''+ 1 and 2''i ''+ 2
* its parent ⌊(''i ''&minus; 1) ∕ 2⌋ where ⌊…⌋ is the [[floor function]].
Alternatively, if the tree root is at index 1, with valid indices 1 through ''n'', then each element ''a'' at index ''i'' has
* children at indices 2''i'' and 2''i ''+1
* its parent at index ⌊''i ∕'' 2⌋.
 
This implementation is used in the [[heapsort]] algorithm, where it allows the space in the input array to be reused to store the heap (i.e. the algorithm is done [[In-place algorithm|in-place]]). The implementation is also useful for use as a [[Priority queue]] where use of a [[dynamic array]] allows insertion of an unbounded number of items.
 
The upheap/downheap operations can then be stated 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''[''i''] (for a max-heap, or the smallest child for a min-heap) within the range ''b'', ..., ''e''.
(If no such index exists because 2''i'' > ''e'' then the heap property holds for the newly extended range and nothing needs to be done.)
By swapping the values ''a''[''i''] and ''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 recursion|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 log<sub>2</sub> ''e'' steps are required.
 
For big heaps and using [[virtual memory]], storing elements in an array according to the above scheme is inefficient: (almost) every level is in a different [[Page (computer memory)|page]].  [[B-heap]]s are binary heaps that keep subtrees in a single page, reducing the number of pages accessed by up to a factor of ten.<ref>
Poul-Henning Kamp.
[http://queue.acm.org/detail.cfm?id=1814327 "You're Doing It Wrong"].
ACM Queue.
June 11, 2010.
</ref>
 
The operation of merging two binary heaps takes Θ(''n'') for equal-sized heaps. The best you can do is (in case of array implementation) simply concatenating the two heap arrays and build a heap of the result.<ref>
Chris L. Kuszmaul.
[http://nist.gov/dads/HTML/binaryheap.html "binary heap"].
Dictionary of Algorithms and Data Structures, Paul E. Black, ed., U.S. National Institute of Standards and Technology. 16 November 2009.
</ref> A heap on ''n'' elements can be merged with a heap on ''k'' elements using O(log ''n'' log ''k'') key comparisons, or, in case of a pointer-based implementation, in O(log ''n'' log ''k'') time.<ref>J.-R. Sack and T. Strothotte
[http://www.springerlink.com/content/k24440h5076w013q/ "An Algorithm for Merging Heaps"],
Acta Informatica 22, 171-186 (1985).</ref> An algorithm for splitting a heap on ''n'' elements into two heaps on ''k'' and ''n-k'' elements, respectively, based on a new view
of heaps as an ordered collections of subheaps was presented in.<ref>. J.-R. Sack and  T. Strothotte
[http://www.sciencedirect.com/science/article/pii/089054019090026E "A characterization of heaps and its applications"]
Information and Computation
Volume 86, Issue 1, May 1990, Pages 69–86.</ref> The algorithm requires  O(log ''n'' * log ''n'')  comparisons. The view  also presents a new and conceptually simple algorithm for merging heaps. When merging is a common task, a different heap implementation is recommended, such as  [[binomial heap]]s, which can be merged in O(log ''n'').
 
Additionally, a binary heap can be implemented with a traditional binary tree data structure, but there is an issue with finding the adjacent element on the last level on the binary heap when adding an element. This element can be determined algorithmically or by adding extra data to the nodes, called "threading" the tree—instead of merely storing references to the children, we store the [[inorder]] successor of the node as well.
 
It is possible to modify the heap structure to allow extraction of both the smallest and largest element in  [[Big O notation|<math>O</math>]]<math>(\log n)</math> time.<ref name="sym">{{cite web
| url = http://cg.scs.carleton.ca/~morin/teaching/5408/refs/minmax.pdf
| author = Atkinson, M.D., [[Jörg-Rüdiger Sack|J.-R. Sack]], N. Santoro, and T. Strothotte
| title = Min-max heaps and generalized priority queues.
| publisher = Programming techniques and Data structures. Comm. ACM, 29(10): 996–1000
| date = 1 October 1986
}}</ref> To do this, the rows alternate between min heap and max heap.  The algorithms are roughly the same, but, in each step, one 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.
 
== Derivation of index equations ==
In an array-based heap, the children and parent of a node can be located via simple arithmetic on the node's index. This section derives the relevant equations for heaps with their root at index 0, with additional notes on heaps with their root at index 1.
 
To avoid confusion, we'll define the '''level''' of a node as its distance from the root, such that the root itself occupies level 0.
 
=== Child nodes ===
 
For a general node located at index <math>i</math> (beginning from 0), we will first derive the index of its right child, <math>\text{right} = 2i + 2</math>.
 
Let node <math>i</math> be located in level <math>L</math>, and note that any level <math>l</math> contains exactly <math>2^l</math> nodes. Furthermore, there are exactly <math>2^{l + 1} - 1</math> nodes contained in the layers up to and including layer <math>l</math> (think of binary arithmetic; 0111...111 = 1000...000 - 1). Because the root is stored at 0, the <math>k</math>th node will be stored at index <math>(k - 1)</math>. Putting these observations together yields the following expression for the '''index of the last node in layer l'''.
 
::<math>\text{last}(l) = (2^{l + 1} - 1) - 1 = 2^{l + 1} - 2</math>
 
Let there be <math>j</math> nodes after node <math>i</math> in layer L, such that
 
::<math>\begin{alignat}{2}
i = & \quad \text{last}(L) - j\\
  = & \quad (2^{L + 1} -2) - j\\
\end{alignat}
</math>
 
Each of these <math>j</math> nodes must have exactly 2 children, so there must be <math>2j</math> nodes separating <math>i</math>'s right child from the end of its layer (<math>L + 1</math>).
 
::<math>\begin{alignat}{2}
\text{right} = & \quad \text{last(L + 1)} -2j\\
            = & \quad (2^{L + 2} -2) -2j\\
            = & \quad 2(2^{L + 1} -2 -j) + 2\\
            = & \quad 2i + 2
\end{alignat}
</math>
 
As required.
 
Noting that the left child of any node is always 1 place before its right child, we get <math>\text{left} = 2i + 1</math>.
 
If the root is located at index 1 instead of 0, the last node in each level is instead at index <math>2^{l + 1} - 1</math>. Using this throughout yields <math>\text{left} = 2i</math> and <math>\text{right} = 2i + 1</math> for heaps with their root at 1.
 
=== Parent node ===
 
Every node is either the left or right child of its parent, so we know that either of the following is true.
 
# <math>i = 2 \times (\text{parent}) + 1</math>
# <math>i = 2 \times (\text{parent}) + 2</math>
 
Hence,
::<math>\text{parent} = \frac{i - 1}{2} \textbf{ or } \frac{i - 2}{2}</math>
 
Now consider the expression <math>\left\lfloor \dfrac{i - 1}{2} \right\rfloor</math>.
 
If node <math>i</math> is a left child, this gives the result immediately, however, it also gives the correct result if node <math>i</math> is a right child. In this case, <math>(i - 2)</math> must be even, and hence <math>(i - 1)</math> must be odd.
 
::<math>\begin{alignat}{2}
\left\lfloor \dfrac{i - 1}{2} \right\rfloor = & \quad \left\lfloor \dfrac{i - 2}{2} + \dfrac{1}{2} \right\rfloor\\
= & \quad \frac{i - 2}{2}\\
= & \quad \text{parent}
\end{alignat}
</math>
 
Therefore, irrespective of whether a node is a left or right child, its parent can be found by the expression:
 
::<math>\text{parent} = \left\lfloor \dfrac{i - 1}{2} \right\rfloor</math>
 
==See also==
* [[Heap (data structure)|Heap]]
* [[Heapsort]]
 
==Notes==
{{notelist}}
 
==References==
{{reflist}}
 
==External links==
*[http://people.ksp.sk/~kuko/bak/index.html Binary Heap Applet] by Kubo Kovac
*[http://www.policyalmanac.org/games/binaryHeaps.htm Using Binary Heaps in A* Pathfinding]
*[http://opendatastructures.org/versions/edition-0.1e/ods-java/10_1_BinaryHeap_Implicit_Bi.html Open Data Structures - Section 10.1 - BinaryHeap: An Implicit Binary Tree]
 
{{Data structures}}
 
{{DEFAULTSORT:Binary Heap}}
[[Category:Heaps (data structures)]]

Revision as of 14:54, 14 January 2014

Template:Infobox data structure

Example of a complete binary max heap
Example of a complete binary min heap

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:

Shape property
The tree is a complete binary tree; that is, 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.
Heap property
All nodes are either [greater than or equal to] or [less than or equal to] each of its children, according to a comparison predicate defined for the heap.

Heaps with a mathematical "greater than or equal to" (≥) comparison predicate are called max-heaps; those with a mathematical "less than or equal to" (≤) comparison predicate are called min-heaps. Min-heaps are often used to implement priority queues.[1][2]

Since the ordering of siblings in a heap is not specified by the heap property, a single node's two children can be freely interchanged unless doing so violates the shape property (compare with treap).

The binary heap is a special case of the d-ary heap in which d = 2.

Heap operations

Both the insert and remove operations modify the heap to conform to the shape property first, by adding or removing from the end of the heap. Then the heap property is restored by traversing up or down the heap. Both operations take O(log n) time.

Insert

To add an element to a heap we must perform an up-heap operation (also known as bubble-up, percolate-up, sift-up, trickle up, heapify-up, or cascade-up), by following this algorithm:

  1. Add the element to the bottom level of the heap.
  2. Compare the added element with its parent; if they are in the correct order, stop.
  3. If not, swap the element with its parent and return to the previous step.

The number of operations required is dependent on the number of levels the new element must rise to satisfy the heap property, thus the insertion operation has a time complexity of O(log n). However, in 1974, Thomas Porter and Istvan Simon proved that the function for the average number of levels an inserted node moves up is upper bounded by the constant 1.6067.[3] The average number of operations required for an insertion into a binary heap is 2.6067 since one additional comparison is made that does not result in the inserted node moving up a level. Thus, on average, binary heap insertion has a constant, O(1), time complexity. Intuitively, this makes sense since approximately 50% of the elements are leaves and approximately 75% of the elements 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.

As an example of binary heap insertion, 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. There is no need to check the children after this. Before we placed 15 on X, the heap was valid, meaning 11 is greater than 5. If 15 is greater than 11, and 11 is greater than 5, then 15 must be greater than 5, because of the transitive relation.

Delete

The procedure for deleting the root from the heap (effectively extracting the maximum element in a max-heap or the minimum element in a min-heap) and restoring the properties is called down-heap (also known as bubble-down, percolate-down, sift-down, trickle down, heapify-down, cascade-down and extract-min/max).

  1. Replace the root of the heap with the last element on the last level.
  2. Compare the new root with its children; if they are in the correct order, stop.
  3. If not, swap the element with one of its children and return to the previous step. (Swap with its smaller child in a min-heap and its larger child in a max-heap.)

So, if we have the same max-heap as before

We remove the 11 and replace it with the 4.

Now the heap property is violated since 8 is greater than 4. In this case, swapping the two elements, 4 and 8, is enough to restore the heap property and we need not swap elements further:

The downward-moving node is swapped with the larger of its children in a max-heap (in a min-heap it would be swapped with its smaller child), until it satisfies the heap property in its new position. This functionality is achieved by the Max-Heapify function as defined below in pseudocode for an array-backed heap A of length heap_length[A]. Note that "A" is indexed starting at 1, not 0 as is common in many real programming languages.

Max-Heapify[4] (A, i):
Template:Padleft ← 2i
Template:Padright ← 2i + 1
Template:Padlargesti
Template:Padif leftheap_length[A] and A[left] > A[largest] then:
Template:Padlargestleft
Template:Padif rightheap_length[A] and A[right] > A[largest] then:
Template:Padlargestright
Template:Padif largesti then:
Template:Padswap A[i] ↔ A[largest]
Template:PadMax-Heapify(A, largest)

For the above algorithm to correctly re-heapify the array, the node at index i and its two direct children must violate the heap property. If they do not, the algorithm will fall through with no change to the array. The down-heap operation (without the preceding swap) can also be used to modify the value of the root, even when an element is not being deleted.

In the worst case, the new root has to be swapped with its child on each level until it reaches the bottom level of the heap, meaning that the delete operation has a time complexity relative to the height of the tree, or O(log n).

Building a heap

A heap could be built by successive insertions. This approach requires O(nlogn) time because each insertion takes O(logn) time and there are n elements. However this is not the optimal method. The optimal method starts by arbitrarily putting the elements on a binary tree, respecting the shape property (the tree could be represented by an array, see below). Then starting from the lowest level and moving upwards, shift the root of each subtree downward as in the deletion algorithm until the heap property is restored. More specifically if all the subtrees starting at some height h (measured from the bottom) have already been "heapified", the trees at height h+1 can be heapified by sending their root down along the path of maximum valued children when building a max-heap, or minimum valued children when building a min-heap. This process takes O(h) operations (swaps) per node. In this method most of the heapification takes place in the lower levels. Since the height of the heap is lg(n), the number of nodes at height h is 2(lgnh)1=2lgn2h+1=n2h+1. Therefore, the cost of heapifying all subtrees is:

h=0lgnn2h+1O(h)=O(nh=0lgnh2h+1)O(nh=0h2h)=O(n)

This uses the fact that the given infinite series h / 2h converges to 2.

The exact value of the above (the worst-case number of comparisons during the heap construction) is known to be equal to:

2n2s2(n)e2(n),[5]

where s2(n) is the sum of all digits of the binary representation of n and e2(n) is the exponent of 2 in the prime factorization of n.

The Build-Max-Heap function that follows, converts an array A which stores a complete binary tree with n nodes to a max-heap by repeatedly using Max-Heapify in a bottom up manner. It is based on the observation that the array elements indexed by floor(n/2) + 1, floor(n/2) + 2, ..., n are all leaves for the tree, thus each is a one-element heap. Build-Max-Heap runs Max-Heapify on each of the remaining tree nodes.

Build-Max-Heap[4] (A):
Template:Padheap_length[A] ← length[A]
Template:Padfor ifloor(length[A]/2) downto 1 do
Template:PadMax-Heapify(A, i)

Heap implementation

A small complete binary tree stored in an array
Comparison between a binary heap and an array implementation.

Heaps are commonly implemented with 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, the parent and children of each node can be found by arithmetic on array indices. These properties make this heap implementation a simple example of an implicit data structure or Ahnentafel list. Details depend on the root position, which in turn may depend on constraints of a programming language used for implementation, or programmer preference. Specifically, sometimes the root is placed at index 1, sacrificing space in order to simplify arithmetic. The peek operation (find-min or find-max) simply returns the value of the root, and is thus O(1).

Let n be the number of elements in the heap and i be an arbitrary valid index of the array storing the heap. If the tree root is at index 0, with valid indices 0 through n − 1, then each element a at index i has

  • children at indices 2i + 1 and 2i + 2
  • its parent ⌊(i − 1) ∕ 2⌋ where ⌊…⌋ is the floor function.

Alternatively, if the tree root is at index 1, with valid indices 1 through n, then each element a at index i has

  • children at indices 2i and 2i +1
  • its parent at index ⌊i ∕ 2⌋.

This implementation is used in the heapsort algorithm, where it allows the space in the input array to be reused to store the heap (i.e. the algorithm is done in-place). The implementation is also useful for use as a Priority queue where use of a dynamic array allows insertion of an unbounded number of items.

The upheap/downheap operations can then be stated 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[i] (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 a[i] and 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.

For big heaps and using virtual memory, storing elements in an array according to the above scheme is inefficient: (almost) every level is in a different page. B-heaps are binary heaps that keep subtrees in a single page, reducing the number of pages accessed by up to a factor of ten.[6]

The operation of merging two binary heaps takes Θ(n) for equal-sized heaps. The best you can do is (in case of array implementation) simply concatenating the two heap arrays and build a heap of the result.[7] A heap on n elements can be merged with a heap on k elements using O(log n log k) key comparisons, or, in case of a pointer-based implementation, in O(log n log k) time.[8] An algorithm for splitting a heap on n elements into two heaps on k and n-k elements, respectively, based on a new view of heaps as an ordered collections of subheaps was presented in.[9] The algorithm requires O(log n * log n) comparisons. The view also presents a new and conceptually simple algorithm for merging heaps. When merging is a common task, a different heap implementation is recommended, such as binomial heaps, which can be merged in O(log n).

Additionally, a binary heap can be implemented with a traditional binary tree data structure, but there is an issue with finding the adjacent element on the last level on the binary heap when adding an element. This element can be determined algorithmically or by adding extra data to the nodes, called "threading" the tree—instead of merely storing references to the children, we store the inorder successor of the node as well.

It is possible to modify the heap structure to allow extraction of both the smallest and largest element in O(logn) time.[10] To do this, the rows alternate between min heap and max heap. The algorithms are roughly the same, but, in each step, one 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.

Derivation of index equations

In an array-based heap, the children and parent of a node can be located via simple arithmetic on the node's index. This section derives the relevant equations for heaps with their root at index 0, with additional notes on heaps with their root at index 1.

To avoid confusion, we'll define the level of a node as its distance from the root, such that the root itself occupies level 0.

Child nodes

For a general node located at index i (beginning from 0), we will first derive the index of its right child, right=2i+2.

Let node i be located in level L, and note that any level l contains exactly 2l nodes. Furthermore, there are exactly 2l+11 nodes contained in the layers up to and including layer l (think of binary arithmetic; 0111...111 = 1000...000 - 1). Because the root is stored at 0, the kth node will be stored at index (k1). Putting these observations together yields the following expression for the index of the last node in layer l.

last(l)=(2l+11)1=2l+12

Let there be j nodes after node i in layer L, such that

i=last(L)j=(2L+12)j

Each of these j nodes must have exactly 2 children, so there must be 2j nodes separating i's right child from the end of its layer (L+1).

right=last(L + 1)2j=(2L+22)2j=2(2L+12j)+2=2i+2

As required.

Noting that the left child of any node is always 1 place before its right child, we get left=2i+1.

If the root is located at index 1 instead of 0, the last node in each level is instead at index 2l+11. Using this throughout yields left=2i and right=2i+1 for heaps with their root at 1.

Parent node

Every node is either the left or right child of its parent, so we know that either of the following is true.

  1. i=2×(parent)+1
  2. i=2×(parent)+2

Hence,

parent=i12𝐨𝐫i22

Now consider the expression i12.

If node i is a left child, this gives the result immediately, however, it also gives the correct result if node i is a right child. In this case, (i2) must be even, and hence (i1) must be odd.

i12=i22+12=i22=parent

Therefore, irrespective of whether a node is a left or right child, its parent can be found by the expression:

parent=i12

See also

Notes

Template:Notelist

References

43 year old Petroleum Engineer Harry from Deep River, usually spends time with hobbies and interests like renting movies, property developers in singapore new condominium and vehicle racing. Constantly enjoys going to destinations like Camino Real de Tierra Adentro.

Template:Data structures

  1. Template:Cite web
  2. Template:Cite web
  3. Template:Cite web
  4. 4.0 4.1 Many property agents need to declare for the PIC grant in Singapore. However, not all of them know find out how to do the correct process for getting this PIC scheme from the IRAS. There are a number of steps that you need to do before your software can be approved.

    Naturally, you will have to pay a safety deposit and that is usually one month rent for annually of the settlement. That is the place your good religion deposit will likely be taken into account and will kind part or all of your security deposit. Anticipate to have a proportionate amount deducted out of your deposit if something is discovered to be damaged if you move out. It's best to you'll want to test the inventory drawn up by the owner, which can detail all objects in the property and their condition. If you happen to fail to notice any harm not already mentioned within the inventory before transferring in, you danger having to pay for it yourself.

    In case you are in search of an actual estate or Singapore property agent on-line, you simply should belief your intuition. It's because you do not know which agent is nice and which agent will not be. Carry out research on several brokers by looking out the internet. As soon as if you end up positive that a selected agent is dependable and reliable, you can choose to utilize his partnerise in finding you a home in Singapore. Most of the time, a property agent is taken into account to be good if he or she locations the contact data on his website. This may mean that the agent does not mind you calling them and asking them any questions relating to new properties in singapore in Singapore. After chatting with them you too can see them in their office after taking an appointment.

    Have handed an trade examination i.e Widespread Examination for House Brokers (CEHA) or Actual Property Agency (REA) examination, or equal; Exclusive brokers are extra keen to share listing information thus making certain the widest doable coverage inside the real estate community via Multiple Listings and Networking. Accepting a severe provide is simpler since your agent is totally conscious of all advertising activity related with your property. This reduces your having to check with a number of agents for some other offers. Price control is easily achieved. Paint work in good restore-discuss with your Property Marketing consultant if main works are still to be done. Softening in residential property prices proceed, led by 2.8 per cent decline within the index for Remainder of Central Region

    Once you place down the one per cent choice price to carry down a non-public property, it's important to accept its situation as it is whenever you move in – faulty air-con, choked rest room and all. Get round this by asking your agent to incorporate a ultimate inspection clause within the possibility-to-buy letter. HDB flat patrons routinely take pleasure in this security net. "There's a ultimate inspection of the property two days before the completion of all HDB transactions. If the air-con is defective, you can request the seller to repair it," says Kelvin.

    15.6.1 As the agent is an intermediary, generally, as soon as the principal and third party are introduced right into a contractual relationship, the agent drops out of the image, subject to any problems with remuneration or indemnification that he could have against the principal, and extra exceptionally, against the third occasion. Generally, agents are entitled to be indemnified for all liabilities reasonably incurred within the execution of the brokers´ authority.

    To achieve the very best outcomes, you must be always updated on market situations, including past transaction information and reliable projections. You could review and examine comparable homes that are currently available in the market, especially these which have been sold or not bought up to now six months. You'll be able to see a pattern of such report by clicking here It's essential to defend yourself in opposition to unscrupulous patrons. They are often very skilled in using highly unethical and manipulative techniques to try and lure you into a lure. That you must also protect your self, your loved ones, and personal belongings as you'll be serving many strangers in your home. Sign a listing itemizing of all of the objects provided by the proprietor, together with their situation. HSR Prime Recruiter 2010
  5. Many property agents need to declare for the PIC grant in Singapore. However, not all of them know find out how to do the correct process for getting this PIC scheme from the IRAS. There are a number of steps that you need to do before your software can be approved.

    Naturally, you will have to pay a safety deposit and that is usually one month rent for annually of the settlement. That is the place your good religion deposit will likely be taken into account and will kind part or all of your security deposit. Anticipate to have a proportionate amount deducted out of your deposit if something is discovered to be damaged if you move out. It's best to you'll want to test the inventory drawn up by the owner, which can detail all objects in the property and their condition. If you happen to fail to notice any harm not already mentioned within the inventory before transferring in, you danger having to pay for it yourself.

    In case you are in search of an actual estate or Singapore property agent on-line, you simply should belief your intuition. It's because you do not know which agent is nice and which agent will not be. Carry out research on several brokers by looking out the internet. As soon as if you end up positive that a selected agent is dependable and reliable, you can choose to utilize his partnerise in finding you a home in Singapore. Most of the time, a property agent is taken into account to be good if he or she locations the contact data on his website. This may mean that the agent does not mind you calling them and asking them any questions relating to new properties in singapore in Singapore. After chatting with them you too can see them in their office after taking an appointment.

    Have handed an trade examination i.e Widespread Examination for House Brokers (CEHA) or Actual Property Agency (REA) examination, or equal; Exclusive brokers are extra keen to share listing information thus making certain the widest doable coverage inside the real estate community via Multiple Listings and Networking. Accepting a severe provide is simpler since your agent is totally conscious of all advertising activity related with your property. This reduces your having to check with a number of agents for some other offers. Price control is easily achieved. Paint work in good restore-discuss with your Property Marketing consultant if main works are still to be done. Softening in residential property prices proceed, led by 2.8 per cent decline within the index for Remainder of Central Region

    Once you place down the one per cent choice price to carry down a non-public property, it's important to accept its situation as it is whenever you move in – faulty air-con, choked rest room and all. Get round this by asking your agent to incorporate a ultimate inspection clause within the possibility-to-buy letter. HDB flat patrons routinely take pleasure in this security net. "There's a ultimate inspection of the property two days before the completion of all HDB transactions. If the air-con is defective, you can request the seller to repair it," says Kelvin.

    15.6.1 As the agent is an intermediary, generally, as soon as the principal and third party are introduced right into a contractual relationship, the agent drops out of the image, subject to any problems with remuneration or indemnification that he could have against the principal, and extra exceptionally, against the third occasion. Generally, agents are entitled to be indemnified for all liabilities reasonably incurred within the execution of the brokers´ authority.

    To achieve the very best outcomes, you must be always updated on market situations, including past transaction information and reliable projections. You could review and examine comparable homes that are currently available in the market, especially these which have been sold or not bought up to now six months. You'll be able to see a pattern of such report by clicking here It's essential to defend yourself in opposition to unscrupulous patrons. They are often very skilled in using highly unethical and manipulative techniques to try and lure you into a lure. That you must also protect your self, your loved ones, and personal belongings as you'll be serving many strangers in your home. Sign a listing itemizing of all of the objects provided by the proprietor, together with their situation. HSR Prime Recruiter 2010.
  6. Poul-Henning Kamp. "You're Doing It Wrong". ACM Queue. June 11, 2010.
  7. Chris L. Kuszmaul. "binary heap". Dictionary of Algorithms and Data Structures, Paul E. Black, ed., U.S. National Institute of Standards and Technology. 16 November 2009.
  8. J.-R. Sack and T. Strothotte "An Algorithm for Merging Heaps", Acta Informatica 22, 171-186 (1985).
  9. . J.-R. Sack and T. Strothotte "A characterization of heaps and its applications" Information and Computation Volume 86, Issue 1, May 1990, Pages 69–86.
  10. Template:Cite web