answer these two options please these are part of a one questionnot different qu
ID: 3611913 • Letter: A
Question
answer these two options please these are part of a one questionnot different questions
a) a) We can use a heap toimplement a priority queue.
The Serve() function returns the highest priority item thatwill be the maximum heap element .The Enqueue( ) function inserts an input value into heap.“Enqueue ( )” function first add a new node to thetree. Then it traverses a path from this lead toward the root tofind a proper place for this new element.
Write pseudo code for Serve() and Enqueue( ) function.
b_) b) SupposeH is a min heap containing n keys (elements). We want to write afunction printSmaller() thatprints all the keys in H that are smaller than a given input key x.
Explanation / Answer
ENQUEUE () function:
1) Insert an element (e) at the end of an array.
2) Find out the parent of this node [ R=ceil(i/2) ]
3) Find out its two children 2*R and 2*R+1
4) If 2*R== e then S=2*R+1 else S=2*R
5) If (e>S)
{
if (e>R)
Swap e & R;
Goto step 2
}
STOP otherwise
SERVE function:
Heapify the array
Return first element of the array (Heap)
b)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.