Lecture 21: March 17 21.1 Binary Heaps

advertisement
CS 124: Data Structures & Algorithms
Spring 2014
Lecture 21: March 17
Lecturer: Robert Snapp
Scribe: Grace Ahmed
Note: LaTeX template courtesy of UC Berkeley EECS dept.
Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications.
They may be distributed outside this class only with the permission of the Instructor.
21.1
Binary Heaps
Here follows a graphical representation of a complete (uniform) binary tree of height 3. (Note the height of
the tree equals the number of links that you have to climb to get from lowest leaf node to the root of the
tree.)
The node at the top is called the root of the tree. The bottom eight nodes are called leaves or leaf nodes.
The nodes that are not leaves are called interior nodes. Note that in a complete binary tree, each interior
node has exactly two children. Thus, the number of nodes at each level below the root node is twice as large
as the size of the level above.
The number of nodes in a complete binary tree of height h is thus,
n = 20 + 21 + 22 + · · · + 2h =
h
X
k=0
2k =
2h+1 − 1
= 2h+1 − 1.
2−1
We used the formula for computing the sum of a truncated geometric series in the above. See formula (A.5)
in Appendix A in CLRS.
For the tree illustrated above, h = 3, so n = 24 − 1 = 15. (Count the blue dots, to make sure this is correct.)
In Chapter 6, we will study binary heaps which mirror the structure of a complete binary tree with one
proviso: the deepest layer of leaf nodes need not be complete. We will also use a heap as a data structure,
which means that we will use it to store records, each indexed by a numerical value, called the key. Here is
an example of a heap that has height 3:
21-1
21-2
Lecture 21: March 17
1
19
2
3
12
4
8
14
5
6
9
5
10
2
6
9
11
1
7
3
14
12
8
1
The above is called a max heap, as it satisfies what is know as the max-heap property: The value stored
in any node (the black number within each blue circle) cannot be less than the value stored in any of its
descendants.
Alternatively, on occasion one might choose to arrange records in a min heap, which must adhere to the
min-heap property: The value stored in any node cannot exceed the value stored in any of its descendants.
Although it is possible to implement a heap using structs and pointers, it is easiest to implement a max-heap
using a sequential array. Thus, the heap shown above can be implemented in a computer program using a
unit-index based array A with 12 locations:
i
A[i]
1
2
3
4
5
6
7
8
9
10
11
12
19
12
14
6
9
3
14
5
2
1
8
1
Note how each entry in the array corresponds to a node in the max heap in the illustration: The red numerals
represent the index of each node in both the heap and in the array.
During the next few lectures we will introduce and analyze several simple algorithms that create and manipulate max heaps. Three utility functions will be valuable: Left(i), Right(i), and Parent(i), which
return, respectively, the index of left child of node i, the index of right child of node i, and the index of the
parent of node i (for i > 1). By inspection of the red indices in the binary tree, one sees that the index of
the left child is always twice the value of the node index i. Thus,
Left(i)
return 2 ∗ i
Since index of the right child is always one more than that of the left child:
Right(i)
return 2 ∗ i + 1
Finally, viewing the relationship in reverse:
Parent(i)
return bi/2c
Lecture 21: March 17
21.2
21-3
Introduction to Heaps and Max-Heapify
The following utility will also be very useful for maintaining the max-heap property as we build and manipulate a max-heap. Please study it well. Note that here we assume that every heap A maintains the size
of the heap, as the meta data A. heap-size. This number is distinct from the length of the array, A. length.
Indeed it is frequently set to be smaller.
Max-Heapify(A, i)
1 l = LEFT(i) // 2i
2 r = RIGHT(i) // 2i + 1
3 if l ≤ A.heap-size and A[l] > A[i]
4
largest = l
5 else
6
largest = i
7 if r ≤ A. heap-size and A[r] > A[largest]
8
largest = r
9 if i 6= largest
10
swap(A[largest], A[i])
11
Max-Heapify(A, largest)
Download