Quiz 10
Important notes:
- Do not start until you are instructed to do so!
- You will have 20 minutes once the proctor says to start.
- You will have brief additional time after we stop to scan and submit your solutions.
- Just before the quiz...
- Have a fully-charged smartphone and laptop, and still plug both in if possible
- Log into Gradescope on your phone
- Change the screen timeout setting on your phone to never, so your screen doesn't go
black if you don't interact with your screen for a while.
- iPhones: Settings / Display & Brightness / Auto-Lock / Never
- Android: Settings / Display / Screen timeout / 10 minutes (or the maximum amount of time)
- Turn on Do Not Disturb (or otherwise turn off all notifications).
- Position your webcam so we can see:
- Your desk
- The paper you are working on
- Your writing utensil(s)
- Both of your hands
- Your phone
- During the quiz:
- You may not ask questions during the exam.
- If you are unsure how to interpret a problem, take your best guess.
- If you feel a question is unfair because you could not ask a question about it, please email the instructor after the quiz and describe your concern. (We are working hard to make the questions extra clear this year, but we want to know when we fail.)
- You may not touch your laptop or webcam.
- This includes muting yourself at any point; the proctors may mute you though.
- All of these must be visible at all times:
- Your desk
- The paper you are working on
- Your writing utensil(s)
- Both of your hands
- Your phone, with the quiz webpage
- You may not ask questions during the exam.
- For any tech fails (laptop or internet stops working, etc.):
- Stop taking the quiz
- Email the instructor (rileyrd@cmu.edu) right away
- We will reply soon to set up a 1-on-1 oral quiz with the course faculty
- Follow all proctor instructions on how to end the quiz.
- If you finish early, wait patiently until the end of the quiz. (Note: Don't use your phone...)
- Keep everything in view (as noted above) until the proctor calls "time".
- When instructed, use your phone to scan your quiz and submit the PDF to Gradescope.
- After submitting to Gradescope, hold your phone up to the webcam to show the receipt.
- Even then, remain in quiz mode until the proctor calls "all clear"
Question 1 - Don’t Get All Heapy on Me
Consider the following array representation of a binary heap:
90 | 84 | 66 | 55 | 69 | 9 | 44 | 27 | 36 | 47 | 59 | 8 |
---|
Part A [4 pts]
Draw the heap represented by this array.
Part B [3 pts]
Give the array after adding 70 to the heap. Show your work, and be sure to write the entire array.
Part C [3 pts]
Building on your answer from part b, give the array after calling removeMax()
on the heap. Show your work, and be sure to write the entire array.
Question 2 - Gettin’ Heapy With It
Consider the following partial implementation of a max-heap:
public class Heap<DataType extends Comparable<DataType>> {
ArrayList<DataType> h = new ArrayList<DataType>();
public Heap() {
// To make sure the 0th element isn't used.
h.add(null);
}
public void add(DataType item) {
h.add(item);
for (int idx = h.size() - 1; idx > 1; idx = idx / 2) {
if (h.get(idx).compareTo(h.get(idx / 2)) > 0) {
DataType t = h.get(idx);
h.set(idx, h.get(idx / 2));
h.set(idx / 2, t);
}
}
}
}
Part A [2 pts]
What changes, exactly, would need to be made to this code so that it implements a min-heap instead?
Part B [8 pts]
Note: This problem is independent of Part A, so assume a max-heap.
Write the method removeMax()
for the Heap
class.
To assist you, here is a skeleton with some code missing. If you’d like, you can use it as a base:
public DataType removeMax() {
// Save a copy of the largest item
DataType ret =
// Remove the appropriate node from the bottom
// and set its value to be at the root of the tree
// Heapify-down
int idx = 1;
while (idx * 2 + 1 < h.size()) {
DataType self =
DataType left =
DataType right =
if (self.compareTo(left) < 0 && left.compareTo(right) > 0) {
} else if (self.compareTo(right) < 0 && right.compareTo(left) > 0) {
}
else {
break;
}
}
return ret;
}
Notes:
- Part B is independent of Part A, so assume a max-heap.
- You can remove a node at a specific index from an
ArrayList
by usingremove(int index)
.