The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+12 votes
1.1k views

A priority queue is implemented as a Max-Heap. Initially, it has $5$ elements. The level-order traversal of the heap is: $10, 8, 5, 3, 2$. Two new elements $1$ and $7$ are inserted into the heap in that order. The level-order traversal of the heap after the insertion of the elements is: 

  1. $10, 8, 7, 5, 3, 2, 1$
  2. $10, 8, 7, 2, 3, 1, 5$
  3. $10, 8, 7, 1, 2, 3, 5$
  4. $10, 8, 7, 3, 2, 1, 5$
asked in DS by Veteran (59.6k points)
edited by | 1.1k views

2 Answers

+18 votes
Best answer

ans is (D)....whenever we insert an element in heap,it will always inserted in last level from left to right..so here we insert element $1$ and $7$ as a child of node $5$.then we perform heapify algorithm until we get the min/max heap..so here finally in above question we get the heap whose level order traversal is $10,8,7,3,2,1,5$

Initial heap:

After insert of $1$

After insert of $7$

answered by Active (4.1k points)
edited by
–1

"A priority queue is implemented as a Max-Heap"  has no use ??? dont you think A option is correct since after insertion queue will remain be priority queue .

0
How did you deduce from the question that Heapify wasn't performed?

Don't you have to adjust the heap after each insertion?
+1 vote
answer is D
answered by Boss (11.5k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

40,903 questions
47,558 answers
146,288 comments
62,305 users