script

Saturday, January 12, 2013

6.5-7


6.5-7 Show how to implement a first-in, first-out queue with a priority queue. Show how to implement a stack with a priority queue. (Queues and stacks are defined in Section 10.1.).

We can implement a first-in,first-out queue with a priority queue We need two array one to hold values which are inserted as in normal array and second array that hold the keys in the order of insertion and apply MIN-HEAPIFY on the array and when EXTRACT-MINIMUM can return the index and using index we can return the value from source array.
Similarly we can implement a stack with a priority queue by creating a new array that hold the order of insertion and apply MAX-HEAPIFY on the array. This will in efficient and not recommended. This will cause waste memory and computing time.

No comments:

Post a Comment