Heaps Exercises
Priority Queues
-
Write down the results of the following priority queue operations and then compare with your neighbor(s):
- Insert 42
- Insert 3
- Insert 7
- Top
- Pop
- Top
- Pop
- Insert 99
- Top
- Pop
What's left in the priority queue (if anything)?
Heaps
- Discuss the following in a group of 3-5 people. Be prepared to share your group's conclusions.
- The differences between priority queues and heaps
- What are the advantages of using a heap data structure?
- Name several scenarios where a heap is advantageous.
- Name some scenarios where a heap is not advantageous.
- Are the following heaps valid? If not, do they violate the structure and/or heap-order property?
D. |
|
E. |
|
- Insertions and Deletions
- Draw the resulting heap after inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13 and 2 into an initially empty binary heap and show its array representation.
- Using the heap from the previous question, what does top() return?
- Using the heap from the previous question, draw the heap and its array after a call to pop().
- Using the heap from the previous question, what does top() return?
- Using the heap from the previous question, draw the heap and its array after a call to pop().
- Using the heap from the previous question, what does top() return?
- Using the heap from the previous question, draw the heap and its array after a call to pop().
BuildHeap
- Write down the code for buildHeap() (feel free to use bubbleDown() or siftdown).
- Show the resulting heap and its corresponding array after each iteration of buildHeap() given the following initial array:
- What is the Big O for buildHeap() and why?
Algorithm Efficiency
What are the following Big Os for Heaps?
Operation |
Average-case |
Worst-case |
Notes |
insert |
|
|
|
top |
|
|
|
pop |
|
|
|
buildHeap |
|
|
|
Last Modified: