Binary Heaps

A binary is dened to b e a binary with a key

in each no de such that

All leaves are on at most twoadjacent levels

All leaves on the lowest level occur to the left

and all levels except the lowest one are completely

lled

The key in ro ot is all its children and the left

and right subtrees are again binary heaps

Conditions and sp ecify shap e of the tree and con dition the lab eling of the tree

1/1

7/4 2/2

12/7 10/30 12/25

The ancestor relation in a heap denes a partial or

der on its elements which means it is reexive anti

symmetric and transitive

Reexive x is an ancestorof itself

Antisymmetric if x is an ancestor of y and y is

an ancestorof xthenx y

Transitive if x is an ancestor of y and y is an

ancestorofz x is an ancestorofz

Partial orders can be used to mo del heirarchies with

incomplete information or equalvalued elements One

of myfavorite games with myparents is eshing out

the partial order of big oldtimemovie stars

The partial order dened by the heap structure is weaker

than that of the total order which explains

Why it is easier to build

Why it is less useful than sorting but still very

important

Constructing Heaps

Heaps can be constructed incrementally by inserting

new elements into the leftmost op en sp ot in the array

If the new element is greater than its parent swap their

p ositions and recur

Since at each step wereplace the ro ot of a subtree by

alarger one wepreserve the heap order

Since all but the last level is always lled the height h

of an n element heap is b ounded b ecause

h

X

i h

n

i

so h blg nc

Doing n such insertions takes n log n since the last

n insertions require O log N time each

Heapify

The b ottom up insertion algorithm gives a good way

to build a heap but Rob ert Floyd found a b etter way

using a merge pro cedure called heapify

Given two heaps and a fresh element they can be

merged into one bymaking the new one the ro ot and

trickling down

BuildheapA

n jAj

For i bnc todo

HeapifyAi

HeapifyAi

left i

right i

if lef t nandAlef t Ai then

max left

else max i

if right n and Aright Amax then

max right

if max ithen

swapAiAmax HeapifyAmax

Rough Analysis of Heapify

Heapify on a subtree containing n no des takes

T n T n O

The comes from merging heaps whose levels dif

fer by one The last row could be exactly half lled

Besides the asymptotic answer wont change so long

the fraction is less than one

Solve the recurrence using the Master Theorem

Let a b and f n

log

Note that n since log

Thus Case of the Master theorem applies

The Master Theorem Let a andbbeconstants let f n

be a function and let T nbedened on the nonnegative integers

by the recurrence

T naT nbf n

where we interpret nb to mean either bnbc or dnbe Then T n

can b e b ounded asymptotically as follows

log a

b

If f nO n forsomeconstant then T n

log a

b

n

log a log a

b b

If f nn then T nn lg n

log a

b

If f n n for some constant and if

af nb cf nforsomeconstant c and all suciently

large n then T nf n

Exact Analysis of Heapify

In fact Heapify performs b etter than O n log n be

cause most of the heaps wemerge are extremely small

A

Z T

M Y RJ

CDBX LPFH

In a full on n no des there are n no des

which are leaves ie height nnodes which are

height nnodes which are height

h

In general there are at most dn e no des of height

hso the cost of building a heap is

blg nc blg nc

X X

h h

h dn eO hO n

h h

Since this sum is not quite a geometric series we cant

apply the usual identitytogetthe sum But it should

be clear that the series converges

Pro of of Convergence

Series convergence is the free lunch of algorithm

analysis

The identify for the sum of a geometric series is

X

k

x

x

k

If we takethe derivative of b oth sides

X

k

kx

x

k

Multiplying both sides of the equation by x gives the

identitywe need

X

x

k

kx

x

k

Substituting x gives a sum of so Buildheap

uses at most n comparisons and thus linear time

The Lessons of I

Are we doing a careful analysis Might our algorithm

be faster than it seems

Typically in our analysis wewillsaythat since weare

doing at most x op erations of at most y timeeach the

total timeis O xy

However if we overestimate to o much our b ound may not b e as tight as it should b e

Heapsort

Heapify can be used to construct a heap using the

observation that an isolated element formsa heap of

size

HeapsortA

BuildheapA

for i n todo

swapAAi

n n

HeapifyA

If we construct our heap from bottom to top using

Heapify we do not have to do anything with the last

nelements

With the implicit tree dened byarraypositions ie

the ith p osition is the parent of the ith and i st

p ositions the leaves start out as heaps

Exchanging the maximum element with the last ele

ment and calling heapify rep eatedly gives an O n lg n

named Heapsort

Heapsort Animations

The Lessons of Heapsort II

Always ask yourself Can weuse a dierent data struc

ture

Selection scans throught the entire array rep eat

edly nding the smallest remaining element

For i to n

A Find the smallest of the rst n i items

B Pull it out of the arrayand put it rst

Using arrays orunsorted linked lists as the data struc

ture op eration A takes O n time and op eration B

takes O

Using heaps both of these op erations can be done

within O lg ntime balancing the work and achieving

abetter tradeo

Priority Queues

A is a on sets of keys

supp orting the following op erations

InsertS x insert x into S

MaximumS return the largest key in S

ExtractMaxS return and remove the largest key

in S

These op erations can b e easily supp orted using a heap

Insert use the trickle up insertion in O log n

Maximum read the rst element in the arrayin

O

ExtractMax delete rst element replace it with

the last decrement the element counter then heapify

in O log n

Applications of Priority Queues

Heaps as stacks or queues

In a stack push inserts a new item and pop re

moves the most recently pushed item

In a queue enqueue inserts a new item and de

queue removes the least recently enqueued item

Both stacks and queues can be simulated by using a

heap when weaddanew time eld to each item and

order the heap according it this timeeld

To simulate the stack increment the time with

each insertion and put the maximum on top of

the heap

To simulate the queue decrement the time with

each insertion and put the maximum on top of the

heap or increment times and keep the minimum

on top

This simulation is not as ecient as a normalstackqueue

implementation but it is a cute demonstration of the

exibilityofapriorityqueue

Discrete Event Simulations

In simulations of airp orts parking lots and jaialai

priority queues can b e used to maintain who go es next

The stack and queue orders are just sp ecial cases of

orderings In real life certain p eople cut in line

Sweepline Algorithms in

Computational Geometry

In the priority queue wewill store the p oints wehave

not yet encountered ordered by x co ordinate and

push the line forward one stop at a time