A min heap H is a binary tree having the following property:\
$\forall N \in H, \forall \ LN \in LEFT(N), \forall \ RN \in RIGHT(N), data(N) \leq data(LN) $ and $ data(N) \leq data(RN)$
Jobs arriving to a computer system for processing are given a priority $Pr$ and a number $Id$ (two integers). Jobs of higher priority are processed first. Jobs with the same priority follow a FIFO policy.
We suppose jobs are sequentially numbered so that the job number $i$ arrives before the job number $j$ if $i < j$. Jobs with the same priority are processed using the FIFO policy (the job with the lowest number processed first). We suppose that the priority $Pr_i$ is higher than the priority $Pr_j$ if $Pr_i < Pr_j$.
We represent such a structure using a Min heap (i.e. a binary tree) $H$ where the root corresponds to the job with the highest priority (to be processed first). Jobs with lower priorities are located on lower levels. Note that for any subtree in $H$, its root corresponds to the job that must be processed before all nodes in that subtree. This will lead to a heap not necessarily balanced i.e. not all nodes in $H$ have two children.
- Write the corresponding declaration of such data structure using \textbf{dynamic implementation} to represent a priority queue ($\texttt{PQ}$).
- Draw the priority queue ($\texttt{PQ}$) obtained after adding the following processes $(Id,Pr)$: (1,4), (2,2), (3,1), (4,2), (5,4), (6,3), (7,2), (8,1).
- Write a function that deletes the process with the highest priority from a priority queue.
- Write a function $\texttt{int Nth(PQ P, int n)}$ that returns the number of the n$^{th}$ process to be served, but without removing it from the priority queue.
For $n=5$, the function must return 7 which is the $Id$ of the 5$^{th}$ process to be served.
Difficulty level
This exercise is mostly suitable for students
This question was part of a project!
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
UVA 1112 - Mice and Maze