|
36 | 36 | <summary>Solution</summary> |
37 | 37 | We get: (end ->) 30 ; 20 (<- front). |
38 | 38 | </details> |
| 39 | + |
| 40 | +## Problem |
| 41 | + |
| 42 | +#. This problem is about min-priority queue, i.e., queues where the most important element is the one with the lowest priority. Draw the priority queue after |
| 43 | + |
| 44 | + #. Inserting values with priority 10, 2, 5, 7, |
| 45 | + #. Removing the most important element, |
| 46 | + #. Inserting values with priority 3, 12, |
| 47 | + |
| 48 | + (you can either draw the queue after each step, or only at the very end) |
| 49 | + |
| 50 | + #. If the queue is implemented using an array. |
| 51 | + |
| 52 | + <details> |
| 53 | + <summary>Solution</summary> |
| 54 | + |
| 55 | + This is supposing an implementation [like the one shared in class](./lectures/data/priority_queue#using-arrays). |
| 56 | + We decompose each step: |
| 57 | + |
| 58 | + 1. Insert value with priority 10 |
| 59 | + 2. Insert value with priority 2 |
| 60 | + 3. Insert value with priority 5 |
| 61 | + 4. Insert value with priority 7 |
| 62 | + 5. Remove value with priority 2 (the lowest in the queue) |
| 63 | + 6. Insert value with priority 3 |
| 64 | + 7. Insert value with priority 12 |
| 65 | + |
| 66 | + <summary>Solution</summary> |
| 67 | + Step \\ Index | 0 | 1 | 2 | 3 | 4 | 5 | |
| 68 | + :--- | --- | --- | --- | --- | --- | --- | |
| 69 | + 1 | 10 | `null` | `null` | `null` | `null` | `null` | |
| 70 | + 2 | 10 | 2 | `null` | `null` | `null` | `null` | |
| 71 | + 3 | 10 | 2 | 5 | `null` | `null` | `null` | |
| 72 | + 4 | 10 | 2 | 5 | 7 | `null` | `null` | |
| 73 | + 5 | 10 | `null` | 5 | 7 | `null` | `null` | |
| 74 | + 6 | 10 | 3 | 5 | 7 | `null` | `null` | |
| 75 | + 7 | 10 | 3 | 5 | 7 | 12 | `null` | |
| 76 | + </details> |
| 77 | + |
| 78 | + #. If the queue is implemented using a binary heap. |
| 79 | + |
| 80 | + <details> |
| 81 | + <summary>Solution</summary> |
| 82 | + |
| 83 | + This is supposing an implementation [like the one shared in class](./lectures/data/priority_queue#using-heaps). |
| 84 | + We decompose each step: |
| 85 | + |
| 86 | + 1. Insert value with priority 10 |
| 87 | + 2. Insert value with priority 2 |
| 88 | + 3. Insert value with priority 5 |
| 89 | + 4. Insert value with priority 7 |
| 90 | + 5. Remove value with priority 2 (the lowest in the queue) |
| 91 | + 6. Insert value with priority 3 |
| 92 | + 7. Insert value with priority 12 |
| 93 | + |
| 94 | + <summary>Solution</summary> |
| 95 | + Step \\ Index | 0 | 1 | 2 | 3 | 4 | 5 | |
| 96 | + :--- | --- | --- | --- | --- | --- | --- | |
| 97 | + 1 | 10 | `null` | `null` | `null` | `null` | `null` | |
| 98 | + 2 | 2 | 10 | `null` | `null` | `null` | `null` | |
| 99 | + 3 | 2 | 10 | 5 | `null` | `null` | `null` | |
| 100 | + 4 | 2 | 7 | 5 | 10 | `null` | `null` | |
| 101 | + 5 | 5 | 7 | 10 | `null` | `null` | `null` | |
| 102 | + 6 | 3 | 5 | 10 | 7 | `null` | `null` | |
| 103 | + 7 | 3 | 5 | 10 | 7 | 12 | `null` | |
| 104 | + </details> |
0 commit comments