How do i answer this Show how to implement First in First ou
How do i answer this?
Show how to implement First in First out with a priority Queue. Show also how to implement a stack in a priority Queue.Solution
First-in-first-out Queue can be implemented by using a min-priority Queue.
Running time theta(1), since, the newly added element has the maximum priority and thus stay at the last position.
Worst case running time theta(log (k-1))
Since one min-heapify required.
Reduce the priority for each element in the Queue from x to x-1
Running time theta(k-1)
K leftarrow k-1
1leftarrow 0
Last –in-first-out Queue can implemented by using a max-priority Queue.
Notice that (k-1) is the maximum priority in the Queue.
K leftarrow k+1
Worst case running time theta(log k),
Since, the newly added element has the maximum priority and thus to be pop up the root.
Running time theta (log(k-1)),
Since one max - heapify required.
