Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions? Comments? Queue Stack Priority Queue

3 Queues and Stacks Last time we implemented the Radix Sort with an array of queues. Anyone care to explain how we did that?

4 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)‏ –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.

5 Queues and Stacks Radix Sort. –use an array of 10 queues each index to the array corresponds to the digit in whatever place we're working on

6 Queues and Stacks Applications of stacks –balancing symbols. e.g. curly braces (left { ) and (right })‏ compilers need to figure out if there are the same number of lefts as there are rights. a stack is handy for this how?

7 Queues and Stacks Applications of stacks –balancing symbols. e.g. curly braces (left { ) and (right })‏ compilers need to figure out if there are the same number of lefts as there are rights. a stack is handy for this –when see a left curly, push it on the stack –when see a right curly try to pop the stack --- there should be a left curly to pop, otherwise it's an error –when get to the end of the source code, if the stack is empty (and we haven't run into the error above) then the curly braces are balanced.

8 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 to 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

9 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

10 Queues and Stacks converting infix to postfix –when an operand is read write it to the output –when a ) is read, pop the stack and write to output until a ( is on top of the stack --- at which point we pop the ( but do not output it. –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, keep doing this until stack is empty or if top is of lower precedence than the one just read, then push the operator –when a ( is read push it –When get to end of input, pop stack, write output until empty stack –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 * +

11 Queues and Stacks converting infix to postfix –what changes will need to be made if we add in – and /

12 Queues and Stacks converting infix to postfix –what changes will need to be made if we add in – and / make – and + be of equal precedence make / and * be of equal precedence

13 Queues and Stacks converting infix to postfix –what changes will need to be made if we add in exponentiation?

14 Queues and Stacks converting infix to postfix –what changes will need to be made if we add in exponentiation? 2^2 = 4 2^3 = 8 2^8 = 256 what about this: 2^2^3 = 2^8 = 256 this means that exponentiation is right-to-left associative

15 Queues and Stacks converting infix to postfix –what changes will need to be made if we add in exponentiation? 2^2 = 4 2^3 = 8 2^8 = 256 what about this: 2^2^3 = 2^8 = 256 this means that exponentiation is right-to-left associative –Change operator rule to: –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, keep doing this until stack is empty or if top is of lower precedence than the one just read (or if it is right-associative and of equal precedence), then push the operator

16 Priority Queues Read from page 239 in text book.

17 Priority Queues Priority queues have the following characteristics –Each item placed into a priority queue has a priority value associated with it –When a remove is requested from a priority queue, we remove the highest priority item –We should also have 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).

18 Priority Queues It is also a wise thing to maintain the following values for a priority queue –current size (how many items are in the queue)‏ –the priority of the highest priority item in the queue to make remove more efficient however it causes (some) additional work –after the remove happens we need to possibly change this value –after an add we just need to check if the one we added is higher than the highest, if so, change it.

19 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.

20 Priority Queues If we don't want to limit the priority values to integers or the range of priorities is large --- an array of queues is impractical. What alternate implementation(s) could handle a large range of floating point priorities?


Download ppt "CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann."

Similar presentations


Ads by Google