Tutorial 12, CPSC 331, Fall 2008

home page -  news -  syllabus -  schedule -  assignments -  tutorials -  tests -  java -  references -  Mike Jacobson


Tutorial 1 -  Tutorial 2 -  Tutorial 3 -  Tutorial 4 -  Tutorial 5 -  Tutorial 6 -  Tutorial 7 -  Tutorial 8 -  Tutorial 9 -  Tutorial 10 -  Tutorial 11 -  Tutorial 12 -  Tutorial 13 -  Tutorial 14 -  Tutorial 15 -  Tutorial 16

 Heaps and Heap Sort

About This Exercise

This exercise is based on the information on binary heaps, operations on binary heaps, heap sort, and priority queues that was presented during lectures on November 5–14.

The objectives of this exercise are

Please read through and try to solve the problems on this exercises before attending the tutorials on November 17. These problems will be discussed during these tutorial.

Problems

  1. Give an array of length 10 that represents the following heap.

    Example of a Heap

     

  2. Draw the heap that is represented by the following array A, assuming that heap-size(A)=6.

    Array-Representation of a Heap

     

  3. Suppose A is the array you produced when you answered Question 1.

    Draw the heap, and its array representation, that you could obtain by performing each of the following operations.

    1. Delete-Min(A)
    2. Increase-Key(A, 3, 14)
    3. Insert(A, 28)
  4. Repeat the previous question, using the heap A that was given for Question #2 (assuming again that heap-size(A)=6).

  5. Use Heap Sort to sort the following array. Draw the binary trees that correspond the array as Build-Max-Heap is applied at the beginning of this operation, as well as the heaps and arrays that are generated using Delete-Max after that.

    Input Array for Sorting Problem

     

  6. Write a Java program that implements the Heap Sort algorithm to sort a given array. Try to make your program as general as you can.

  7. Consider the following algorithm. Its inputs are an array A that represents a nonempty binary tree with the shape of a heap, and an integer i such that 0 ≤ i < heap-size(A). Its output should be the largest element stored in the subtree of A with root i.

    Find-Maximum(A, i) largest = A[i] if left(i) < heap-size(A) then largest = max(largest, Find-Maximum(A, left(i))) end if if right(i) < heap-size(A) then largest = max(largest, Find-Maximum(A, right(i))) end if

    1. Sketch a proof that this algorithm is correct.
    2. Sketch a proof that the number of steps is linear in the size of the subtree whose root is the value stored at A[i] in the worst case.
  8. This question, and the question that follows, concerns two different recursive algorithms that can be used to rearrange the elements of a binary tree (with the same shape as a heap) in order to produce a max-heap.

    1. Modify the above “Find-Maxiumum” procedure so that it also returns the index j in A of the largest value that it found. The new algorithm not be significantly more expensive than the algorithm given above.

    2. Consider a recursive algorithm “Build-Max-Heap2” that receives the same inputs as the above algorithm “Find-Maximum.” This recursive algorithms should rearrange the values stored in the subtree with root A[i] in order to produce a max-heap.

      The algorithm includes the following three steps.

      1. Use the algorithm described in the first part of the question to find the largest value stored in the subtree with root A[i] along with its index j. Swap the values stored at indices i and j, so the largest values in the subtree is stored at the root.

      2. if left(i) < heap-size(A) then apply the Build-Max-Heap2 algorithm recursively to convert the left subtree into a max-heap.

      3. if right(i) < heap-size(A) then apply the Build-Max-Heap2 algorithm recursively to convert the right subtree into a max-heap.

      Sketch a proof that this recursive algorithm is correct. Then state a bound on the worst-case running time of this algorithm and prove that your bound is correct.

  9. Finally, consider yet another recursive algorithm, “Build-Max-Heap3,” which should solve the same problem as Build-Max-Heap2. This algorithm should use the following steps.

    1. if left(i) < heap-size(A) then apply the Build-Max-Heap3 algorithm recursively to convert the left subtree into a max-heap.

    2. if right(i) < heap-size(A) then apply the Build-Max-Heap3 algorithm recursively to convert the right subtree into a max-heap.

    3. Apply the Max-Heapify algorithm (discussed in class) with inputs A and i.

    Sketch a proof that this recursive algorithm is also correct. Then state a bound on the worst-case running time of this algorithm and prove that your bound is correct.

Note: One of the recursive algorithms “Build-Max-Heap2” and “Build-Max-Heap3” is asymptotically as efficient as the “Build-Max-Heap” algorithm discussed in class, but the other is not.

You should be able to analyze both recursive algorithms (and discover which is better for large inputs) by studying and modifying the analyses that are included in the lecture notes on heap operations and heap sort.


This page last modified:
http://www.cpsc.ucalgary.ca/~jacobs/cpsc331/F08/tutorials/tutorial12.html