heap removal algorithm

heap removal algorithm

Definition

Let

H

be a heap storing

n

elements

, and let

⪯

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

⪯

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

⪯

b

holds. If

z

⪯

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

⌈

log

2

⁡

n

⌉

+

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

𝒪

⁢

(

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

HeapRemove

⁢

(

H

,

n

,

⪯

)

\@stopfield

\@addfield

\@startfield

Input

. A heap

(

H

,

⪯

)

(represented as an array) containing

n

>

0

values.

\@stopfield

\@addfield

\@startfield

Output

. Removes and returns a value

x

from

H

such that

x

⪯

y

holds for all

y

in

H

.

\@stopfield

\@addfield

\@startfield

Procedure

:

\@stopfield

\@addfield

\@startfield

t

⁢

o

⁢

p

←

H

⁢

[

0

]

\@stopfield

\@addfield

\@startfield

n

←

n

-

1

\@stopfield

\@addfield

\@startfield

H

⁢

[

0

]

←

H

⁢

[

n

]

\@stopfield

\@addfield

\@startfield

p

⁢

a

⁢

r

⁢

e

⁢

n

⁢

t

←

0

\@stopfield

\@addfield

\@startfield

c

⁢

h

⁢

i

⁢

l

⁢

d

←

1

\@stopfield

\@addfield

\@startfield

𝗐𝗁𝗂𝗅𝖾

\=

\+

c

⁢

h

⁢

i

⁢

l

⁢

d

<

n

\@stopfield

\@addfield

\@startfield

\@ifatmargin

⁢

𝖽𝗈

\@stopfield

\@addfield

\@startfield

𝗂𝖿

\=

\+

c

⁢

h

⁢

i

⁢

l

⁢

d

+

1

<

n

\@stopfield

\@addfield

\@startfield

\@stopfield

\@addfield

\@startfield

\@ifatmargin

⁢

𝗍𝗁𝖾𝗇

\=

\+

\@stopfield

\@addfield

\@startfield

𝗂𝖿

\=

\+

H

⁢

[

c

⁢

h

⁢

i

⁢

l

⁢

d

+

1

]

⪯

H

⁢

[

c

⁢

h

⁢

i

⁢

l

⁢

d

]

\@stopfield

\@addfield

\@startfield

\@stopfield

\@addfield

\@startfield

\@ifatmargin

⁢

𝗍𝗁𝖾𝗇

\=

\+

\@stopfield

\@addfield

\@startfield

c

⁢

h

⁢

i

⁢

l

⁢

d

←

c

⁢

h

⁢

i

⁢

l

⁢

d

+

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

𝗂𝖿

\=

\+

H

⁢

[

p

⁢

a

⁢

r

⁢

e

⁢

n

⁢

t

]

⋠

H

⁢

[

c

⁢

h

⁢

i

⁢

l

⁢

d

]

\@stopfield

\@addfield

\@startfield

\@stopfield

\@addfield

\@startfield

\@ifatmargin

⁢

𝗍𝗁𝖾𝗇

\=

\+

swap

⁢

(

H

⁢

[

p

⁢

a

⁢

r

⁢

e

⁢

n

⁢

t

]

,

H

⁢

[

c

⁢

h

⁢

i

⁢

l

⁢

d

]

)

\@stopfield

\@addfield

\@startfield

p

⁢

a

⁢

r

⁢

e

⁢

n

⁢

t

←

c

⁢

h

⁢

i

⁢

l

⁢

d

\@stopfield

\@addfield

\@startfield

c

⁢

h

⁢

i

⁢

l

⁢

d

←

2

⋅

c

⁢

h

⁢

i

⁢

l

⁢

d

+

1

\@stopfield

\@addfield

\@startfield

\@stopfield

\@addfield

\@startfield

\@ifatmargin

⁢

\@ifatmargin

⁢

𝖾𝗅𝗌𝖾

𝖾𝗅𝗌𝖾

⁢

c

⁢

h

⁢

i

⁢

l

⁢

d

←

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

Canonical name

HeapRemovalAlgorithm

Date of creation

2013-03-22 12:29:30

Last modified on

2013-03-22 12:29:30

Owner

mps (409)

Last modified by

mps (409)

Numerical id

11

Author

mps (409)

Entry type

Algorithm

Classification

msc 68P20

Classification

msc 68P10

Classification

msc 68P05

Related topic

Heap

Related topic

HeapInsertionAlgorithm

Related topic

Heapsort

Generated on Sat Feb 10 12:38:56 2018 by

LaTeXML