Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Binary heap
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Variant of heap data structure}} {{Infobox data structure |name=Binary (min) heap |type=binary tree/heap |invented_by=[[J. W. J. Williams]] |invented_year=1964 <!-- NOTE: For the purposes of "Big O" notation, all bases are equivalent, because changing the base of a log only introduces a constant factor. Since the base of logarithms doesn't matter, please do not write complexity expressions that indicate base-2 (or any other base). DO: O(log n) DON'T: O(lg n), O(log2 n), O(log_2 n), O(ln n), O(log10 n), etc. --> |insert_worst=O(log ''n'') |insert_avg=O(1) |delete_min_avg=O(log ''n'') |delete_min_worst=O(log ''n'') |decrease_key_avg=O(log ''n'') |decrease_key_worst=O(log ''n'') |find_min_avg=O(1) |find_min_worst=O(1) |merge_avg=O(''n'') |merge_worst=O(''n'')}} [[File:Max-Heap.svg|thumb|right|Example of a complete binary max-heap]] [[File:Min-heap.png|thumb|right|Example of a complete binary min heap]] A '''binary heap''' is a [[heap (data structure)|heap]] [[data structure]] that takes the form of a [[binary tree]]. Binary heaps are a common way of implementing [[priority queue]]s.{{r|clrs|pp=162–163}} The binary heap was introduced by [[J. W. J. Williams]] in 1964 as a data structure for implementing [[heapsort]].<ref>{{Citation |first=J. W. J. |last=Williams |author-link=J. W. J. Williams |title=Algorithm 232 - Heapsort |year=1964 |journal=[[Communications of the ACM]] |volume=7 |issue=6 |pages=347–348 |doi= 10.1145/512274.512284}}</ref> A binary heap is defined as a binary tree with two additional constraints:<ref>{{citation | author=Y Narahari | title=Data Structures and Algorithms | chapter=Binary Heaps | url=https://gtl.csa.iisc.ac.in/dsa/ | chapter-url=http://lcm.csa.iisc.ernet.in/dsa/node137.html}}</ref> *Shape property: a binary heap 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: the key stored in each node is either greater than or equal to (≥) or less than or equal to (≤) the keys in the node's children, according to some [[total order]]. Heaps where the parent key is greater than or equal to (≥) the child keys are called ''max-heaps''; those where it is less than or equal to (≤) are called ''min-heaps''. Efficient (that is, [[logarithmic time]]) algorithms are known for the two operations needed to implement a priority queue on a binary heap: *Inserting an element; *Removing the smallest or largest element from (respectively) a min-heap or max-heap. Binary heaps are also commonly employed in the [[heapsort]] [[sorting algorithm]], which is an in-place algorithm as binary heaps can be implemented as an [[implicit data structure]], storing keys in an array and using their relative positions within that array to represent child–parent relationships. ==Heap operations== Both the insert and remove operations modify the heap to preserve 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 {{nowrap|O(log ''n'')}} time. === Insert === To insert an element to a heap, we perform the following steps: # Add the element to the bottom level of the heap at the leftmost open space. # 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. Steps 2 and 3, which restore the heap property by comparing and possibly swapping a node with its parent, are called ''the up-heap'' operation (also known as ''bubble-up'', ''percolate-up'', <!-- not a typo: -->''sift-up'', ''trickle-up'', ''swim-up'', ''heapify-up'', ''cascade-up'', or ''fix-up''). The number of operations required depends only on the number of levels the new element must rise to satisfy the heap property. Thus, the insertion operation has a worst-case time complexity of {{nowrap|O(log ''n'')}}. For a random heap, and for repeated insertions, the insertion operation has an average-case complexity of O(1).<ref>{{Cite journal|last1=Porter|first1=Thomas|last2=Simon|first2=Istvan|date=Sep 1975|title=Random insertion into a priority queue structure|journal=IEEE Transactions on Software Engineering|volume=SE-1|issue=3|pages=292–298|doi=10.1109/TSE.1975.6312854|s2cid=18907513|issn=1939-3520}}</ref><ref>{{Cite journal |last1=Mehlhorn|first1=Kurt|last2=Tsakalidis|first2=A.|date=Feb 1989| title=Data structures |website=Universität des Saarlandes |url=https://publikationen.sulb.uni-saarland.de/handle/20.500.11880/26179 |language=en|page=27|doi=10.22028/D291-26123 |quote=Porter and Simon [171] analyzed the average cost of inserting a random element into a random heap in terms of exchanges. They proved that this average is bounded by the constant 1.61. Their proof docs not generalize to sequences of insertions since random insertions into random heaps do not create random heaps. The repeated insertion problem was solved by Bollobas and Simon [27]; they show that the expected number of exchanges is bounded by 1.7645. The worst-case cost of inserts and deletemins was studied by Gonnet and Munro [84]; they give log log n + O(1) and log n + log n* + O(1) bounds for the number of comparisons respectively.}}</ref> As an example of binary heap insertion, say we have a max-heap [[File:Heap add step1.svg|150px|class=skin-invert-image]] 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 {{nowrap|15 > 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|class=skin-invert-image]] However the heap property is still violated since {{nowrap|15 > 11}}, so we need to swap again: [[File:Heap add step3.svg|150px|class=skin-invert-image]] which is a valid max-heap. There is no need to check the left child after this final step: at the start, the max-heap was valid, meaning the root was already greater than its left child, so replacing the root with an even greater value will maintain the property that each node is greater than its children ({{nowrap|11 > 5}}; if {{nowrap|15 > 11}}, and {{nowrap|11 > 5}}, then {{nowrap|15 > 5}}, because of the [[transitive relation]]). === Extract=== 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) while retaining the heap property is as follows: #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.) Steps 2 and 3, which restore the heap property by comparing and possibly swapping a node with one of its children, are called the ''down-heap'' (also known as ''bubble-down'', ''percolate-down'', <!-- not a typo: -->''sift-down'', ''sink-down'', ''trickle down'', ''heapify-down'', ''cascade-down'', ''fix-down'', ''extract-min'' or ''extract-max'', or simply ''heapify'') operation. So, if we have the same max-heap as before [[File:Heap delete step0.svg|150px|class=skin-invert-image]] We remove the 11 and replace it with the 4. [[File:Heap remove step1.svg|150px|class=skin-invert-image]] 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|class=skin-invert-image]] 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 ''length''(''A''). ''A'' is indexed starting at 1. // Perform a down-heap or heapify-down operation for a max-heap // ''A'': an array representing the heap, indexed starting at 1 // ''i'': the index to start at when heapifying down '''Max-Heapify'''(''A'', ''i''): ''left'' ← 2×''i'' ''right'' ← 2×''i'' + 1 ''largest'' ← ''i'' '''if''' ''left'' ≤ ''length''(''A'') '''and''' ''A''[''left''] > A[''largest''] '''then''': ''largest'' ← ''left''<br /> '''if''' ''right'' ≤ ''length''(''A'') '''and''' ''A''[''right''] > ''A''[''largest''] '''then''': ''largest'' ← ''right'' '''if''' ''largest'' ≠ ''i'' '''then''': '''swap''' ''A''[''i''] and ''A''[''largest''] '''Max-Heapify'''(''A'', ''largest'') For the above algorithm to correctly re-heapify the array, no nodes besides the node at index ''i'' and its two direct children can violate the heap property. 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''). === Insert then extract === Inserting an element then extracting from the heap can be done more efficiently than simply calling the insert and extract functions defined above, which would involve both an <code>upheap</code> and <code>downheap</code> operation. Instead, we can do just a <code>downheap</code> operation, as follows: # Compare whether the item we're pushing or the peeked top of the heap is greater (assuming a max heap) # If the root of the heap is greater: ## Replace the root with the new item ## Down-heapify starting from the root # Else, return the item we're pushing [[Python (programming language)|Python]] provides such a function for insertion then extraction called "heappushpop", which is paraphrased below.<ref>{{Cite web| title=python/cpython/heapq.py| url=https://github.com/python/cpython/blob/master/Lib/heapq.py|access-date=2020-08-07| website=GitHub| language=en}}</ref><ref>{{Cite web|title=heapq — Heap queue algorithm — Python 3.8.5 documentation| url=https://docs.python.org/3/library/heapq.html#heapq.heappushpop|access-date=2020-08-07| website=docs.python.org|quote=heapq.heappushpop(heap, item): Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than heappush() followed by a separate call to heappop().}}</ref> The heap array is assumed to have its first element at index 1. // Push a new item to a (max) heap and then extract the root of the resulting heap. // ''heap'': an array representing the heap, indexed at 1 // ''item'': an element to insert // Returns the greater of the two between ''item'' and the root of ''heap''. '''Push-Pop'''(''heap'': List<T>, ''item'': T) -> T: '''if''' ''heap'' is not empty '''and''' heap[1] > ''item'' '''then''': // < if min heap '''swap''' ''heap''[1] and ''item'' _downheap(''heap'' starting from index 1) '''return''' ''item'' A similar function can be defined for popping and then inserting, which in Python is called "heapreplace": // Extract the root of the heap, and push a new item // ''heap'': an array representing the heap, indexed at 1 // ''item'': an element to insert // Returns the current root of ''heap'' '''Replace'''(''heap'': List<T>, ''item'': T) -> T: '''swap''' ''heap''[1] and ''item'' _downheap(''heap'' starting from index 1) '''return''' ''item'' === Search === Finding an arbitrary element takes O(n) time. === Delete === Deleting an arbitrary element can be done as follows: # Find the index <math>i</math> of the element we want to delete # Swap this element with the last element. Remove the last element after the swap. # Down-heapify or up-heapify to restore the heap property. In a max-heap (min-heap), up-heapify is only required when the new key of element <math>i</math> is greater (smaller) than the previous one because only the heap-property of the parent element might be violated. Assuming that the heap-property was valid between element <math>i</math> and its children before the element swap, it can't be violated by a now larger (smaller) key value. When the new key is less (greater) than the previous one then only a down-heapify is required because the heap-property might only be violated in the child elements. === Decrease or increase key === <!-- section linked from [[Reheapification]] --> The decrease key operation replaces the value of a node with a given value with a lower value, and the increase key operation does the same but with a higher value. This involves finding the node with the given value, changing the value, and then down-heapifying or up-heapifying to restore the heap property. Decrease key can be done as follows: # Find the index of the element we want to modify # Decrease the value of the node # Down-heapify (assuming a max heap) to restore the heap property Increase key can be done as follows: # Find the index of the element we want to modify # Increase the value of the node # Up-heapify (assuming a max heap) to restore the heap property ==Building a heap== Building a heap from an array of {{mvar|n}} input elements can be done by starting with an empty heap, then successively inserting each element. This approach, called Williams' method after the inventor of binary heaps, is easily seen to run in {{math|''O''(''n'' log ''n'')}} time: it performs {{mvar|n}} insertions at {{math|''O''(log ''n'')}} cost each.{{efn|In fact, this procedure can be shown to take {{math|Θ(''n'' log ''n'')}} time [[Best, worst and average case|in the worst case]], meaning that {{math|''n'' log ''n''}} is also an asymptotic lower bound on the complexity.<ref name="clrs">{{Introduction to Algorithms|3}}</ref>{{rp|167}} In the ''average case'' (averaging over all [[permutation]]s of {{mvar|n}} inputs), though, the method takes linear time.<ref name="heapbuildjalg">{{cite journal |title=Average Case Analysis of Heap Building by Repeated Insertion |first1=Ryan |last1=Hayward |first2=Colin |last2=McDiarmid |journal=J. Algorithms |year=1991 |volume=12 |pages=126–153 |url=http://www.stats.ox.ac.uk/__data/assets/pdf_file/0015/4173/heapbuildjalg.pdf |doi=10.1016/0196-6774(91)90027-v |citeseerx=10.1.1.353.7888 |access-date=2016-01-28 |archive-url=https://web.archive.org/web/20160205023201/http://www.stats.ox.ac.uk/__data/assets/pdf_file/0015/4173/heapbuildjalg.pdf |archive-date=2016-02-05 }}</ref>}} However, Williams' method is suboptimal. A faster method (due to [[Robert W. Floyd|Floyd]]{{r|heapbuildjalg}}) 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, sift 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> have already been "heapified" (the bottommost level corresponding to <math>h=0</math>), 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> \lfloor \log n \rfloor</math>, the number of nodes at height <math>h</math> is <math>\le \frac{2^{\lfloor \log n \rfloor}}{2^h} \le \frac{n}{2^h}</math>. Therefore, the cost of heapifying all subtrees is: :<math> \begin{align} \sum_{h=0}^{\lfloor \log n \rfloor} \frac{n}{2^h} O(h) & = O\left(n\sum_{h=0}^{\lfloor \log n \rfloor} \frac{h}{2^h}\right) \\ & = 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]] <math display="inline">\sum_{i=0}^\infty i/2^i</math> [[Convergent series|converges]]. 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 | 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>{{efn|This does not mean that ''sorting'' can be done in linear time since building a heap is only the first step of the [[heapsort]] algorithm.}} where {{math|''s''<sub>2</sub>(''n'')}} is the [[Hamming weight|sum of all digits of the binary representation]] of {{mvar|n}} and {{math|''e''<sub>2</sub>(''n'')}} is the exponent of {{math|2}} in the prime factorization of {{mvar|n}}. The average case is more complex to analyze, but it can be shown to asymptotically approach {{math|1.8814 ''n'' − 2 log<sub>2</sub>''n'' + ''O''(1)}} comparisons.<ref>{{cite journal |title=An Average Case Analysis of Floyd's Algorithm to Construct Heaps |first=Ernst E. |last=Doberkat |journal=Information and Control |volume=6 |issue=2 |pages=114–131 |date=May 1984 |doi=10.1016/S0019-9958(84)80053-4 |url=https://core.ac.uk/download/pdf/82135122.pdf |doi-access=free}}</ref><ref>{{cite tech report |title=Elementary Average Case Analysis of Floyd's Algorithm to Construct Heaps |first=Tomi |last=Pasanen |date=November 1996 |publisher=Turku Centre for Computer Science |id=TUCS Technical Report No. 64 |isbn=951-650-888-X |citeseerx=10.1.1.15.9526 }} Note that this paper uses Floyd's original terminology "siftup" for what is now called sifting ''down''.</ref> 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''' (down-heapify for a max-heap) in a bottom-up manner. The array elements indexed by {{nowrap|''[[floor function|floor]]''(''n''/2) + 1}}, {{nowrap|''floor''(''n''/2) + 2}}, ..., ''n'' are all leaves for the tree (assuming that indices start at 1)—thus each is a one-element heap, and does not need to be down-heapified. '''Build-Max-Heap''' runs '''Max-Heapify''' on each of the remaining tree nodes. '''Build-Max-Heap''' (''A''): '''for each''' index ''i'' '''from''' ''floor''(''length''(''A'')/2) '''downto''' 1 '''do:''' '''Max-Heapify'''(''A'', ''i'') == Heap implementation ==<!-- This section is linked from [[Heapsort]] --> [[File:Binary tree in array.svg|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 binary heap is always a 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 [[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, in order to simplify arithmetic. 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 at index ''[[floor function|floor]]''((''i'' − 1) / 2). 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 ''[[floor function|floor]]''(''i'' / 2). This implementation is used in the [[heapsort]] algorithm which reuses the space allocated to the input array to store the heap (i.e. the algorithm is done [[In-place algorithm|in-place]]). This implementation is also useful as a [[Priority queue]]. When a [[dynamic array]] is used, insertion of an unbounded number of items is possible. The <code>upheap</code> or <code>downheap</code> 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 {{nowrap|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>{{cite magazine|first = Poul-Henning|last= Kamp|url = http://queue.acm.org/detail.cfm?id=1814327 |title = You're Doing It Wrong|magazine= ACM Queue|date = June 11, 2010|volume = 8|issue = 6}} </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"] {{Webarchive| url=https://web.archive.org/web/20080808141408/http://www.nist.gov/dads/HTML/binaryheap.html |date=2008-08-08 }}. 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örg-Rüdiger Sack|J.-R. Sack]] and T. Strothotte [https://doi.org/10.1007%2FBF00264229 "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>{{Cite journal |doi = 10.1016/0890-5401(90)90026-E|title = A characterization of heaps and its applications|journal = Information and Computation|volume = 86|pages = 69–86|year = 1990|last1 = Sack|first1 = Jörg-Rüdiger|author1-link = Jörg-Rüdiger Sack| last2 = Strothotte|first2 = Thomas|doi-access = free}}</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 make the extraction of both the smallest and largest element possible 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 | author1 = Atkinson, M.D. | author1-link = Michael D. Atkinson | author2 = J.-R. Sack | author2-link = Jörg-Rüdiger Sack | author3 = N. Santoro | author4 = T. Strothotte | name-list-style = amp | title = Min-max heaps and generalized priority queues. | publisher = Programming techniques and Data structures. Comm. ACM, 29(10): 996–1000 | date = 1 October 1986 | access-date = 29 April 2008 | archive-date = 27 January 2007 | archive-url = https://web.archive.org/web/20070127093845/http://cg.scs.carleton.ca/%7Emorin/teaching/5408/refs/minmax.pdf | url-status = dead }}</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 generalized 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 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 {{mvar|i}} (beginning from 0), we will first derive the index of its right child, <math>\text{right} = 2i + 2</math>. Let node {{mvar|i}} be located in level {{mvar|L}}, and note that any level {{mvar|l}} 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 {{mvar|l}} (think of binary arithmetic; 0111...111 = 1000...000 - 1). Because the root is stored at 0, the {{mvar|k}}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 {{mvar|l}}'''. ::<math>\text{last}(l) = (2^{l + 1} - 1) - 1 = 2^{l + 1} - 2</math> Let there be {{mvar|j}} nodes after node {{mvar|i}} 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 {{mvar|j}} nodes must have exactly 2 children, so there must be <math>2j</math> nodes separating {{mvar|i}}'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> 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 non-root node is either the left or right child of its parent, so one of the following must hold: * <math>i = 2 \times (\text{parent}) + 1</math> * <math>i = 2 \times (\text{parent}) + 2</math> Hence, ::<math>\text{parent} = \frac{i - 1}{2} \;\textrm{ 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> ==Related structures== 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]]). Note, however, that in the common array-based heap, simply swapping the children might also necessitate moving the children's sub-tree nodes to retain the heap property. The binary heap is a special case of the [[d-ary heap]] in which d = 2. ==Summary of running times== {{Heap Running Times}} ==References== {{reflist}} ==External links== *[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], [[Pat Morin]] *[https://robin-thomas.github.io/max-heap/ Implementation of binary max heap in C] by Robin Thomas *[https://robin-thomas.github.io/min-heap/ Implementation of binary min heap in C] by Robin Thomas {{Data structures}} {{DEFAULTSORT:Binary Heap}} [[Category:Heaps (data structures)]] [[Category:Binary trees]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Citation
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite magazine
(
edit
)
Template:Cite tech report
(
edit
)
Template:Cite web
(
edit
)
Template:Data structures
(
edit
)
Template:Efn
(
edit
)
Template:Heap Running Times
(
edit
)
Template:Infobox data structure
(
edit
)
Template:Math
(
edit
)
Template:Mvar
(
edit
)
Template:Nowrap
(
edit
)
Template:R
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Webarchive
(
edit
)