Binary Heaps
A binary heap data structure is a binary tree that is
completely filled on all levels, except possibly the lowest, which
will be filled from the left up to a point. Due to these
characteristics, it is easy to represent the tree in an array.
Figure 1 shows the logical structure (top) of
the heap and also how it can be stored in an array (bottom). Notice
how each layer of the tree occupies consecutive slots in the array.
The root of the tree is
, and given the index
of a node,
the indices of its parent PARENT
, left child
LEFT
and right child RIGHT
can be
computed simply.
PARENT
1 return
LEFT
1 return
RIGHT
1 return
Binary heaps can either be a maximum heap or minimum heap. As a
maximum heap, every node indexed by
, other than the
root (i.e.
), has
.
Conversely in a minimum heap, every node indexed by
,
other than the root, has
. The
remainder of this lecture assumes a maximum heap.
Note: This property makes a binary heap different from a
binary search tree. In a binary tree,
. However, in a maximum
heap,
and
. So a binary search
tree can be viewed as sorted, while a heap cannot.
Figure 1: The
structure of a maximum heap. The top diagram shows the logical
heap, the bottom diagram shows how it can be stored in an array,
still showing the heap structure. Numbers in the circles/boxes
are keys and those outside are indices. Notice how each layer of
the tree occupies consecutive slots in the array.
1 Maintaining the heap property
Assuming that we already have a maximum heap, operations on a
maximum heap, for example inserting or deleting, may cause the heap
to lose its maximum heap property. The MAX-HEAPIFY
routine can be used to rectify this. It takes an array
and
index
, and assumes that
is a maximum heap except
that
may be less than its children.
MAX-HEAPIFY
1
LEFT
2
RIGHT
3 if
and
4 then
5 else
6 if
and
7 then
8 if
9 then Swap
and
10 MAX-HEAPIFY
Figure 2: In (a), 5 has been
added to position 2 in a maximum heap. The first iteration of
MAX-HEAPIFY swaps 5 with 14, in (b), and then swaps 5 with
12 in (c).
2 Building a heap
The BUILD-MAX-HEAP routine takes any array
, and, working
successively from the bottom of tree to the top, uses
MAX-HEAPIFY to build the maximum heap.
BUILD-MAX-HEAP
1
2 for
downto
3 do MAX-HEAPIFY
Figure 3: (a) The
initial tree. BUILD-MAX-HEAP works successively from the
bottom. 18 is greater than its children, so nothing is done. (b)
MAX-HEAPIFY is called on 3, and so is swapped with 12.
(c) 1 will be swapped with 8. (d) 2 will be sent to the bottom of
the tree, first swapped with 18, then with 4. (e) When
MAX-HEAPIFY is called on 5, it is sent to the bottom of
tree. (f) The resulting maximum heap.
3 Priority Queues
A priority queue is a data structure for maintaining a set
of elements, each with an associated value called a
key.
Priority queues are often used to efficiently extract an element
with a particular property out of a dynamic set. They do this by
maintaining the position of element in the set that holds that
property, and keeping the rest of the set ordered in such a way that
modifying the set will lead to easily finding the new element with
the given property.
A max-priority queue supports the following operations.
- INSERT (
) inserts the element
into the set
.,
i.e.,
.
-
MAXIMUM(
) returns the element of
with the largest
key.
-
EXTRACT-MAX(
) removes and returns the element of
with the largest key.
-
INCREASE-KEY(
) increases the value of element
's
key to the new value
, which is assumed to be at least as large
as
s current key value.
A maximum heap can be used as a max-priority queue, as
it always maintains that the first element is the maxmimum. Using
MAX-HEAPIFY, it can easily ensure that certain changes, for
example removing the maximum element, will maintain the first
element as the maximum.
HEAP-MAXIMUM
1 return
HEAP-EXTRACT-MAX
1 if
2 then error "Heap underflow"
3
4
5
6 MAX-HEAPIFY
7 return
HEAP-SIFT-UP
1 while
and
(i)
2 do Swap
with
(i)
3
PARENT
HEAP-INCREASE-KEY
1 if
2 then error "New key is smaller than current key"
3
4 HEAP-SIFT-UP
MAX-HEAP-INSERT
1
2
3 HEAP-SIFT-UP
Figure 4:
HEAP-INCREASE-KEY (a) The original maximum heap with the
key to be increased shaded in yellow. (b) The key is increased to
17. (c) 17 is swapped with its parent. (d) The maximum heap
property is restored, swapping 17 with its parent again.
File translated from
TEX
by
TTM,
version 3.67.
On 31 Mar 2006, 18:12.