Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Binary heap
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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!
=== 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'').
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Binary heap
(section)
Add topic