Priority Queues
- set S of elements with “keys” (priority) that can be compared
Operations
insert(A, x)
- add an element
x
- add an element
max(A)
- return the highest priority element
extract_max(A)
- remove and return the highest priority element
Complete Binary Trees
- every layer before the last is full
- in the last layer, all nodes are as far to the left as possible
Height of a tree: number of edges in the longest path (other contexts may use number of nodes)
- height of a complete binary tree is $\log_2(n)$
Max-Heap
- List implementation of priority queue always requires a lot of time for some operation, so we use max-heaps
- complete binary tree
- **max-heap property**: every root node has higher priority than its child nodes
- stored in array that is level-order traversal of tree
- consider array with indices starting at 1
- if an element has index $i$, then its children are stored in indices $2i$ and $2i+1$
- (as a result, if an element has index i then its parent has index $\lfloor i/2 \rfloor$)
insert(A, x)
- algorithm:
- add
x
to the leftmost empty place in the bottom layer (i.e. at the end of the list) - if
x
is greater than its parent, swapx
and its parent (i.e. swapA[i]
andA[i/2]
) - repeat step 2 at
x
’s new position, propagating it upwards untilx
is not greater than its parent
- add
- since the maximum number of iterations of the algorithm is the height of the tree (in the worst case,
x
would need to be propagated to the top of the tree), the algorithm takes $\Theta(\log(n))$ time
- algorithm:
max(A)
- algorithm:
- return the first element in the array
- obviously takes constant time
- algorithm:
extract_max(A)
- algorithm:
- store the max
- replace the max with the last element (which we will call
x
) in the array, decrement the array size (getting rid of the last element), akaA[1] = x; A.length -= 1
- if
x
is less than one of its children, swap with that child, ifx
is less than both of its children, swap with the greater child - repeat step 3 until
x
is not less than either of its children, i.e. the max-heap property has been restored
- similarly to insert, the maximum number of iterations of the algorithm is the height of the tree (since in the worst case, the last element has to be swapped until it is at the bottom of the tree), so the algorithm takes $\Theta(\log(n))$
- algorithm:
Heapsort
- algorithm:
- make a heap out of the elements of the list ($\Theta(n)$ time)
extract_max(A)
$n$ times to extract each element - these extractions will be in order of greatest to least element of the list
- each extract_max step takes