Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question No:2 a) We can use a heapto implement a priority queue. The Serve( ) fu

ID: 3612071 • Letter: Q

Question

Question No:2                                  

a)      We can use a heapto implement a priority queue.

The Serve( )function returns the highest priority item that will be the maximumheap element .The Enqueue ( ) function inserts aninput value into heap. “Enqueue ( )” function first adda new node to the tree. Then it traverses a path from this leadtoward the root to find a proper place for this new element.

Write pseudo code forServe( ) and Enqueue ( )function.

b)     Suppose H is a min heapcontaining n keys (elements). We want to write a functionprintSmaller() that prints all the keys in H thatare smaller than a given input key x .

Explanation / Answer

ENQUEUE () function:

STEP 1: Insert an element (e) at the end of an array.

STEP 2: Find out the parent of this node [ R=ceil(i/2) ]

STEP 3: Find out its two children 2*R and 2*R+1

STEP 4: If 2*R== e then S=2*R+1 else S=2*R

STEP 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)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote