We can use a heap to implement a priority queue. The Serve( ) function returns t
ID: 3612072 • Letter: W
Question
We can use a heap to implement a priority queue. The Serve( ) function returns the highest priority item that will be the maximum heap element .The Enqueue ( ) function inserts an input value into heap. "Enqueue ( )" function first add a new node to the tree. Then it traverses a path from this lead toward the root to find a proper place for this new element. Write pseudo code for Serve( ) and Enqueue ( ) function. Suppose H is a min heap containing n keys (elements). We want to write a function printSmalter0 that prints 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
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.