Talk:Data Structures/Min and Max Heaps

The algorithm for removing an extreme element is just wrong, as far as I can see. It "percolates" the hole down the tree, but not necessarily to the last element in the array, which is where it has to be for subsequent truncation of that array to work. The algorithm as it stands will remove whatever is last in the array, regardless of where the hole is. When the hole has not percolated to the last element of the array, an element will be lost, and another will be duplicated.

The correct algorithm is to start by put the last array element at the first position in the array and then percolate that down. This guarantees the last element has been vacated, ready for truncation of the array.

Now the above problem seems to have been corrected, but the current pseudo-code for "percolating" elements downward on deletion is also completely wrong as of 2010/05/27. The LEFT and RIGHT macros and the indexing in the loop assume an array indexed from 1 (as LEFT(0) = 0, i.e. itself) but the pseudo-code begins by assuming that the first entry is 0 as in C. Additionally it will attempt to access elements with invalid indices in the case where the size after removal is 0.

I haven't examined the code for addition, but would not be confident about that either.

I agree that the extreme value removal algorithm is incorrect. See correct implementation here: http://webdocs.cs.ualberta.ca/~holte/T26/heap-del.html Efadae (discuss • contribs) 19:15, 14 October 2011 (UTC)