You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/lectures/data/priority_queue.md
+57-1Lines changed: 57 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,8 @@ Letting a greater priority means "more important" is called a *max-priority queu
27
27
In both cases, a decision must be made if multiple elements have the same priority: we can decide arbitrarily, using the element value, take the "first one" in the structure, etc.
28
28
29
29
Exactly like a people waiting **at the ER**, priority queues implement a **"most-important-in-first-out"** principle.
30
+
Our examples will use the [Emergency Severity Index](https://en.wikipedia.org/wiki/Emergency_Severity_Index), which ranges from 1 (most urgent) to 5 (less urgent).
31
+
30
32
31
33
## Possible Implementation
32
34
@@ -37,7 +39,6 @@ Here is an implementation of priority queues using arrays:
-`Add` is $O(n)$, it may take $n$ steps to find an empty slot,
@@ -57,11 +58,66 @@ A maximally efficient implementation of priority queues is given by [heaps](http
57
58
58
59
Note that this is different from being a binary search tree.
59
60
61
+
#### Representing complete binary trees using arrays
62
+
63
+
A [binary heap](https://en.wikipedia.org/wiki/Binary_heap) is often [implemented as an array](https://en.wikipedia.org/wiki/Binary_heap#Heap_implementation).
64
+
Consider the following binary tree (we will take the priority of the node to be its value in the following):
65
+
66
+
!include diag/gra/heap_example_1.md
67
+
68
+
It is a heap, but not a binary search tree. It can be represented as the following array:
The reason why we start at index 1 and not 0 is because it makes the following calculation easier^[The difference can be looked up [on wikipedia](https://en.wikipedia.org/wiki/Binary_heap#Heap_implementation), it is mostly a matter of substracting or adding 1 at various places.].
75
+
Indeed, each element at index $i$ has
76
+
77
+
- its children at indices $2 \times i$ and $(2 \times i) + 1$,
78
+
- its parent at index $\lfloor i/2 \rfloor$ for $\lfloor \cdot \rfloor$ the floor function (i.e., $\lfloor 1.5 \rfloor = 1$).
79
+
80
+
#### Inserting into a heap
81
+
82
+
To insert an element to a heap, we perform the following steps:
83
+
84
+
1. Add the element to the bottom level of the heap at the leftmost open space (or "start" a new level if the last level is full).
85
+
2. Compare the added element with its parent; if they are in the correct order, stop.
86
+
3. If not, swap the element with its parent and return to the previous step.
87
+
88
+
Imagine we want to insert $-1$ in our previous example, we would:
89
+
90
+
- Add $-1$ as the right child of $2$,
91
+
- Swap $-1$ and $2$,
92
+
- Swap $-1$ and $1$,
93
+
- Be done.
94
+
95
+
In terms of array representation, we would obtain (with the values that changed **in bold**):
Extracting the element with the lowest priority is easy: it is located at the root of the tree, or at index 1 in the array representation.
105
+
The challenge is to restore "the heap property", which is done as follows:
106
+
107
+
1. Replace the root of the heap with the last element on the last level.
108
+
2. Compare the new root with its children; if they are in the correct order, stop.
109
+
3. If not, swap the element with one of its children and return to the previous step. (Swap with its smaller child in a min-heap and its larger child in a max-heap.)
110
+
111
+
The 2nd and 3rd steps are called "percolate-down".
0 commit comments