My test is tomorrow morning. Here is that question i was having trouble with.
Suppose that Integers 9, 5, 3, 7, 1 are added, in the order given, to an initially empty queue, q, and that Integers 2, 4, 6, and 8 are added, in the order given, to an initially empty stack, s. Also suppose that hp is an initially empty min-heap and that the min-hip method addToHeap adds an element to the min-heap using the standard algorithm for inserting into a heap thus maintaining the min-heap properties after each insertion into the heap. After the following code is executed,
while(!q.isEmpty())
{
hp.addToHeap(q.dequeue());
}
while(!s.isEmpty())
{
hp.addToHeap(s.pop());
}
which of the following min-heaps is a representation of hp?
A. 1
/ \
2 3
/ \ / \
6 9 5 4
/ \
7 8
B. 1
/ \
2 5
/ \ / \
3 7 8 6
/ \
9 4
C. 1
/ \
2 3
/ \ / \
4 7 8 6
/ \
9 5
D. 9
/ \
7 4
/ \ / \
8 1 2 3
/ \
5 6
E. 9
/ \
8 7
/ \ / \
6 5 4 3
/ \
2 1
-----------------------------------------------------------
There's the question. Now, as far as i know, the root of the min-heap is going to be the smallest number, so 1. So i've ruled out D and E. But to be honest, I don't know whether it's A, B, or C. I'm going to say B.
Someone please help me out, tonight, because my test is tomorrow and there are going to be Heap questions!