Discussion 13 handout

Group members (names & NetIDs)

Objectives

Binary heaps

Push

Consider a min-heap of integers (where each element’s priority is its own value). The elements are stored in int[] b, where b.length is 13. Here are the current values in b (a ? indicates an element whose value is not logically contained in the heap; the heap currently has size 11):

{ 4, 5, 7, 6, 8, 8, 9, 7, 7, 8, 9, ?, ? }

Draw the complete binary tree representation of this heap. Label each node with its index in the array.

Add the value 3 to the end of the heap, then bubble it up to its appropriate location. Draw the resulting tree.

Write the sequence of values contained in b representing the above tree.

Pop

Consider a min-heap of integers (where each element’s priority is its own value). The elements are stored in int[] b, where b.length is 15. Here are the current values in b (a ? indicates an element whose value is not logically contained in the heap; the heap currently has size 12):

{ 2, 3, 4, 7, 6, 5, 6, 8, 9, 8, 9, 7, ?, ?, ? }

Draw the complete binary tree representation of this heap. Label each node with its index in the array.

Pop the next element from the heap, then perform the necessary operations to restore the heap invariants. Draw the resulting tree.

Write the sequence of values contained in b representing the above tree.

What value should be returned by pop() after completing the above operations?

Further practice

For homework, open the dis13 project in IDEA and study its implementation of a heap that separates values from priorities. There are three TODOs in the code; based on the specifications and invariants, uncomment the appropriate line for each TODO, then run the accompanying test suite to check your work. We recommend reading the JavaHyperText entry on heaps with priorities.

Challenge problem

Modify the Heap class to support an updatePriority(e, p) operation that changes the priority of e to p and restores the heap’s class invariants. Your implementation should have a time complexity in O(lg N), where N is the size of the priority queue (you may assume O(1) lookups from a standard library hash table in this analysis). Be sure to document any fields and class invariants you add to the class.

Optimizing graph navigation

Seek

Discuss and propose at least two ways one could optimize the “seek” logic in A6 to find the ring in fewer steps in most cases. For a baseline, assume a depth-first search with strict backtracking.

Some questions you might consider include:

Scram

Discuss and propose at least two ways one could optimize the “scram” logic in A6 to gather more coins on the way out.

Some questions you might consider include:

Submission

  1. Open the assignment page for “Discussion activity 13” in CMSX
  2. [Recorder] Find the “Group Management” section and invite each group member
  3. [Others] Refresh the page and accept your invitation
  4. [Recorder] Take a picture of your work and save as either a JPEG or a PDF file named “discussion_responses”. After all invitations have been accepted, upload your picture along with your code as your group’s submission.
    • Recommended scanning apps: Microsoft Office Lens, Adobe Scan, Genius Scan, Evernote Scannable

Ensure that your group is formed and your work submitted before the Friday evening deadline.

Tips and reminders