Docsity

Heap Exercise-Complexity of Algorithms-Assignment Solution, Exercises of Advanced Algorithms

Toggle sidebar
bg1
A Heap(ing) Portion
1. Insert the items into a heap (that is initially empty) in the order given
below:
12,6,20,11,7,9,15,10,3
Draw the heap after each insertion. Give a vector representation of the
heap after the last item has been inserted.
2. Consider the heap given below.
First perform two DeleteMin operations on this heap. Then insert the
element 14 into the heap. Finally perform one more DeleteMin opera-
tion. As above, draw the heap after each step and give the final vector
representation after all the operations have been performed.
1
docsity.com
pf3

Partial preview of the text

Download Heap Exercise-Complexity of Algorithms-Assignment Solution and more Exercises Advanced Algorithms in PDF only on Docsity!

A Heap(ing) Portion

  1. Insert the items into a heap (that is initially empty) in the order given below: 12 , 6 , 20 , 11 , 7 , 9 , 15 , 10 , 3 Draw the heap after each insertion. Give a vector representation of the heap after the last item has been inserted.
  2. Consider the heap given below.

First perform two DeleteMin operations on this heap. Then insert the element 14 into the heap. Finally perform one more DeleteMin opera- tion. As above, draw the heap after each step and give the final vector representation after all the operations have been performed.

Solution:

  1. Here are the diagrams.

Remember that to insert an element, you put it in the right-most empty child on the bottom level, then it “bubbles up” to its proper level by swapping with its parent (if necessary, i.e. if it’s less than its parent). The final vector representation of this heap is the vector (3, 6 , 9 , 7 , 11 , 20 , 15 , 12 , 10),