Computer Science 245: Data Structures and Algorithms

Computer Science 245
Spring 2015
Homework 5: Heaps
Due Wednesday, Wednesday, March 4th
  1. (2 points) The following elements are inserted into an empty Min-Heap in the following order:

    5, 11, 22, 15, 12, 6, 4, 1, 3, 2

    Draw the resulting heap (use the logical (tree) representation)

  2. (1 point) Given the following values in an array:

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

    Show the result of calling Build-Heap (the bottom-up heap building procedure) on this array

  3. (3 points) Draw all legal min-heaps containing the 4 elements 1,2,3,4.

  4. (3 points) Draw all legal min-heaps containing the 5 elements 1,2,3,4,5.

  5. Largest in a heap
    1. (4 points) Write function that takes as input an array representing a heap, and the number of elements in the heap, and returns (but does not remove!) the largest element in the heap. You do not need to turn this in electronically, paper only is fine. For full credit, your function should only examine the elements that have a chance of being the largest element. You may assume that the size is at least 1.
      // data contains a min-heap, with elements at indices 1 .. size
      // Return the largest element in the heap.  Only look at elements
      /  that have a chance of being the largest element
      int largestInHeap(int data[], int size)
      
    2. (1 point) What is the Θ() running time of your function, in terms of the size of the heap?