This preview shows pages 1–2. Sign up to view the full content.
Lecture Notes
CMSC 251
A heap is represented as an
leftcomplete
binary tree. This means that all the levels of the tree are full
except the bottommost level, which is ﬁlled from left to right. An example is shown below. The keys of
a heap are stored in something called
heap order
. This means that for each node
u
, other than the root,
key
(
Parent
(
u
))
≥
key
(
u
)
. This implies that as you follow any path from a leaf to the root the keys
appear in (nonstrict) increasing order. Notice that this implies that the root is necessarily the largest
element.
4
Heap ordering
Leftcomplete Binary Tree
14
3
16
9
10
87
1
2
Figure 11: Heap.
Next time we will show how the priority queue operations are implemented for a heap.
Lecture 13: HeapSort
(Tuesday, Mar 10, 1998)
Read:
Chapt 7 in CLR.
Heaps:
Recall that a heap is a data structure that supports the main priority queue operations (insert and
extract max) in
Θ(log
n
)
time each. It consists of a leftcomplete binary tree (meaning that all levels of
the tree except possibly the bottommost) are full, and the bottommost level is ﬁlled from left to right.
As a consequence, it follows that the depth of the tree is
Θ(log
n
)
where
n
is the number of elements
stored in the tree. The keys of the heap are stored in the tree in what is called
heap order
. This means
that for each (nonroot) node its parent’s key is at least as large as its key. From this it follows that the
largest key in the heap appears at the root.
Array Storage:
Last time we mentioned that one of the clever aspects of heaps is that they can be stored in
arrays, without the need for using pointers (as would normally be needed for storing binary trees). The
reason for this is the leftcomplete nature of the tree.
This is done by storing the heap in an array
A
[1
..n
]
. Generally we will not be using all of the array,
since only a portion of the keys may be part of the current heap. For this reason, we maintain a variable
m
≤
n
which keeps track of the current number of elements that are actually stored actively in the
heap. Thus the heap will consist of the elements stored in elements
A
[1
..m
]
.
We store the heap in the array by simply unraveling it level by level. Because the binary tree is left
complete, we know exactly how many elements each level will supply. The root level supplies 1 node,
the next level 2, then 4, then 8, and so on. Only the bottommost level may supply fewer than the
appropriate power of 2, but then we can use the value of
m
to determine where the last element is. This
is illustrated below.
We should emphasize that this
only works
because the tree is leftcomplete. This cannot be used for
general trees.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 01/13/2012 for the course CMSC 351 taught by Professor Staff during the Fall '11 term at University of Louisville.
 Fall '11
 Staff

Click to edit the document details