Skip to content

Commit fc66167

Browse files
ClémentClément
authored andcommitted
Adding elements of solution for exam #2$
1 parent 8f660e8 commit fc66167

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

source/code/projects/PQueue_heap/PQueue/Program.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,23 @@ static void Main(string[] args)
4646
Console.WriteLine(myQueue);
4747
myQueue.Add(1, "Cardiac arrest");
4848
Console.WriteLine(myQueue);
49-
}
49+
50+
PQueue<int, string> heapProblem = new PQueue<int, string>(6);
51+
heapProblem.Add(10, null);
52+
Console.WriteLine(heapProblem);
53+
heapProblem.Add(2, null);
54+
Console.WriteLine(heapProblem);
55+
heapProblem.Add(5, null);
56+
Console.WriteLine(heapProblem);
57+
heapProblem.Add(7, null);
58+
Console.WriteLine(heapProblem);
59+
heapProblem.Extract();
60+
Console.WriteLine(heapProblem);
61+
heapProblem.Add(3, null);
62+
Console.WriteLine(heapProblem);
63+
heapProblem.Add(12, null);
64+
Console.WriteLine(heapProblem);
65+
66+
67+
}
5068
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
%%%
2+
3+
A((10))-->B((8))
4+
A-->C((15))
5+
B~~~H1:::hidden
6+
B-->D((9))
7+
C-->E((13))
8+
C-->F((25))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
A((10))-->B((8))
2+
A-->C((12))
3+
B-->D((7))
4+
B-->E((9))
5+
C-->F((11))
6+
C-->G((13))

source/solutions/data/queues.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,69 @@ tags:
3636
<summary>Solution</summary>
3737
We get: (end ->) 30 ; 20 (<- front).
3838
</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

Comments
 (0)