CS109A Notes For Lecture 3/4/96 Priority Queues: Priorities
CS109A Notes For Lecture 3/4/96 Priority Queues: Priorities
Priority Queues
1. Model = set with priorities associated with
elements.
Priorities are comparable by a < operator, e.g., priorities could be real numbers.
2. Operations:
a) Insert an element with a given priority.
b) Deletemax = nd and remove from the
set the element with highest priority.
Example PQ Implementation
Balanced POT
9
1
3
1
A 1] = root.
A node represented by A i] has parent A i=2].
Bubbleup on Heap
At position i:
1. If i = 1, then done.
2. Otherwise, compare A i] with A i=2]. If A i]
is smaller, done. Else, swap and repeat at i=2.
Bubbledown on Heap
Insert on Heap
Deletemax on Heap
Heapsort