CS 206 Introduction to Computer Science II 03 / 31 / 2017 Instructor: Michael Eckmann
Today s Topics Questions? Comments? finish RadixSort implementation some applications of stack Priority Queues Michael Eckmann - Skidmore College - CS 206 - Spring 2017
Queues and Stacks Radix Sort. consider the job of sorting (base 10) integers let's limit them to 0-99 for now (all >=0 and <= 100) we handled this in several ways already a totally different way is the following way start with an unsorted list separate the numbers to be sorted into 10 different bins based on their 1's digit then, get the numbers out of the bins and make a new list (take numbers from bin 0, then add on to the end of the list the numbers in bin 1,... and so on, finally adding to the end of the list the numbers from bin 9) then separate this list into 10 bins based on the 10's digit. get the numbers out of the bins like before to get a sorted list.
Queues and Stacks Applications of stacks postfix expressions (reverse Polish notation) infix (what we're used to) is like: 1.07*3.85 + 14.05 + 1.07*8.75 postfix is like: 1.07, 8.75, *, 14.05, +, 1.07, 3.85, *, + infix result requires us to do the multiplies first and then store the intermediate values and then do the 2 adds. postfix operates in the following way: when see a number, push it when see an operator, perform it to the two top numbers on the stack and push the result final result in our example is: 27.532
Queues and Stacks Applications of stacks converting from infix (with only +, *, (, ) ) to postfix expressions infix example (assume normal precedence): a + b*c + ( d*e + f ) * g postfix for our example could be: a b c * + d e * f + g * + let's look at the algorithm to do this using a stack
Queues and Stacks converting infix to postfix when an operand (a, b, c, etc.) is read, write it to the output if we see a ) then we pop the stack and write to output until a ( is on top of the stack --- at which point we pop ( but do not output it. when a + or * is read, examine the top of the stack if it is empty or if top is of lower precedence than the one just read, or if top is (, then push the operator when a + or * is read if top is of the stack does not have lower precedence than the one just read, then pop the operator and write to output when a ( is read push it When finished reading input, pop stack, write to output until stack is empty Let's try this algorithm with our example: infix: a + b*c + ( d*e + f ) * g postfix (expected output): a b c * + d e * f + g * +
Priority Queues The main difference between a Queue and a Priority Queue is that In a Queue, an item will be dequeued based ONLY on the order in which it was enqueued In a Priority Queue Every item is enqueued with a priority value Higher priority items have preference when dequeuing Items with the same priority can be enqueued / dequeued in any order
Priority Queues Priority queues have the following characteristics Each item placed into a priority queue has a priority value associated with it When a dequeue is requested from a priority queue, we dequeue the highest priority item We need a way to determine if the priority queue is empty We can also have a peek to see what item has the highest priority without removing it It is best to limit priority values to be integers (reason to be seen shortly).
Priority Queues It is also wise to maintain the following values for a priority queue current size (how many items are in the whole priority queue) the priority of the highest priority item in the queue to make dequeue easier however it causes (some) additional work after the dequeue happens we need to possibly change this value after an enqueue we just need to check if the one we added is higher than the highest, if so, change it.
Priority Queues Implementation of a priority queue could be an array of queues. The index of the array is the priority value (see how we want our priority values to be integers?) The range of priority values (all integers) determines how many queues we're storing --- that is, how many elements of the array there will be. Let's implement a priority queue in this way now.