# heap removal algorithm

## Definition

Let $H$ be a heap storing $n$ elements, and let $\preceq$ be an operator that defines a total order over all of the values in $H$. If $H$ is non-empty, then there is a value $x$ in $H$ such that $x\preceq y$ holds for all values in $H$. The heap removal algorithm removes $x$ from $H$ and returns it, while maintaining the heap property of $H$.

The process of this algorithm is similar in nature to that of the heap insertion algorithm. First, if $H$ only holds one value, then that value is $x$, which can simply be removed and returned. Otherwise, let $z$ be the value stored in the right-most leaf of $H$. Since $x$ is defined by the heap property to be the root of the tree, the value of $x$ is saved, $z$ is removed, and the value at the root of the tree is set to $z$. Then $z$ is sifted downwards into the tree until the heap property is regained.

The sifting process is similar to that of the heap insertion algorithm, only in reverse. First, if $z$ is a leaf, the process ends. Otherwise, let $a,b$ be the two children of $z$, chosen such that $a\preceq b$ holds. If $z\preceq a$ holds, the process ends. Otherwise, $a$ and $z$ are swapped and the process repeats for $z$.

## Analysis

Since $H$ is a balanced binary tree, it has a maximum depth of $\lceil\log_{2}n\rceil+1$. Since the maximum number of times the sift operation can occur is constrained by the depth of the tree, the worst-case time complexity for heap insertion is $\mathcal{O}(\log n)$.

## Pseudocode

What follows is the pseudocode for implementing heap removal. For the given pseudocode, we presume that the heap is actually represented implicitly in an array (see the binary tree entry for details), and that the heap contains at least one value.

• \@startfield

$\mathrm{HeapRemove}(H,n,\preceq)$\@stopfield\@addfield\@startfieldInput. A heap $(H,\preceq)$ (represented as an array) containing $n>0$ values.\@stopfield\@addfield\@startfieldOutput. Removes and returns a value $x$ from $H$ such that $x\preceq y$ holds for all $y$ in $H$.\@stopfield\@addfield\@startfieldProcedure:\@stopfield\@addfield\@startfield$top\leftarrow H[0]$\@stopfield\@addfield\@startfield$n\leftarrow n-1$\@stopfield\@addfield\@startfield$H[0]\leftarrow H[n]$\@stopfield\@addfield\@startfield$parent\leftarrow 0$\@stopfield\@addfield\@startfield$child\leftarrow 1$\@stopfield\@addfield\@startfield$\mbox{\rm\bf\sf while}\$\=\+$child\@stopfield\@addfield\@startfield$\@ifatmargin\ \mbox{\rm\bf\sf do}\$\@stopfield\@addfield\@startfield$\mbox{\rm\bf\sf if}\$\=\+$child+1\@stopfield\@addfield\@startfield\@stopfield\@addfield\@startfield$\@ifatmargin\ \mbox{\rm\bf\sf then}\$\=\+\@stopfield\@addfield\@startfield$\mbox{\rm\bf\sf if}\$\=\+$H[child+1]\preceq H[child]$\@stopfield\@addfield\@startfield\@stopfield\@addfield\@startfield$\@ifatmargin\ \mbox{\rm\bf\sf then}\$\=\+\@stopfield\@addfield\@startfield$child\leftarrow child+1$\@stopfield\@addfield\@startfield\@stopfield\@addfield\@startfield$\@ifatmargin\$\@stopfield\@addfield\@startfield\@ifatmargin\@ltab\-\@stopfield\@addfield\@startfield\@ifatmargin\@ltab\-fi\@stopfield\@addfield\@startfield\@stopfield\@addfield\@startfield$\@ifatmargin\$\@stopfield\@addfield\@startfield\@ifatmargin\@ltab\-\@stopfield\@addfield\@startfield\@ifatmargin\@ltab\-fi\@stopfield\@addfield\@startfield$\mbox{\rm\bf\sf if}\$\=\+$H[parent]\not\preceq H[child]$\@stopfield\@addfield\@startfield\@stopfield\@addfield\@startfield$\@ifatmargin\ \mbox{\rm\bf\sf then}\$\=\+$\mathrm{swap}(H[parent],H[child])$\@stopfield\@addfield\@startfield$parent\leftarrow child$\@stopfield\@addfield\@startfield$child\leftarrow 2\cdot child+1$\@stopfield\@addfield\@startfield\@stopfield\@addfield\@startfield$\@ifatmargin\ \@ifatmargin\hbox to 0.0pt{\mbox{\rm\bf\sf else}\ }$$\mbox{\rm\bf\sf else}\ child\leftarrow n$\@stopfield\@addfield\@startfield\@stopfield\@addfield\@startfield$\@ifatmargin\$\@stopfield\@addfield\@startfield\@ifatmargin\@ltab\-\@stopfield\@addfield\@startfield\@ifatmargin\@ltab\-fi\@stopfield\@addfield\@startfield\@stopfield\@addfield\@startfield$\@ifatmargin\$\@stopfield\@addfield\@startfield\@ifatmargin\@ltab\-od\@stopfield\@addfield\@startfield\@stopfield\@addfield

Title heap removal algorithm HeapRemovalAlgorithm 2013-03-22 12:29:30 2013-03-22 12:29:30 mps (409) mps (409) 11 mps (409) Algorithm msc 68P20 msc 68P10 msc 68P05 Heap HeapInsertionAlgorithm Heapsort