Queue Applications Lecture 31 Tue, Apr 11, 2006
Topics Evaluating infix expressions 4/18/2019 Queues
Infix Expression Evaluation An infix expression with one (binary) operator is written in the order: left-operand, operator, right-operand. Example: 3 + 4 4/18/2019 Queues
Disadvantages of Infix Notation Parentheses are often needed to indicate order of operation. Example: (3 + 4) * (5 + 6) Operators follow a precedence hierarchy. Example: 3 + 4 * 5 – 6 / 7 Operators have left or right associativity. Example: 100 – 50 – 10 – 5 – 1 4/18/2019 Queues
Infix Expression Evaluation To evaluate an infix expression, we first convert it to postfix. Then begin with an empty stack and an empty queue. Process the tokens from left to right according to the following rules. If the token is a number, Enqueue the token. If the token is a left parenthesis, Push the token onto the stack. 4/18/2019 Queues
Infix Expression Evaluation If the token is a right parenthesis, Pop tokens off the stack and enqueue them until a left parenthesis is popped. Discard the right and left parentheses. If the token is an operator, Pop tokens off the stack and enqueue them until An operator of lower precedence is on top of the stack, or A left parenthesis is on top of the stack, or The stack is empty. Push the operator onto the stack. 4/18/2019 Queues
Infix Expression Evaluation After processing the last token Pop all tokens off the stack and enqueue them. The queue now contains the expression in post-fix notation. Process the queue as a post-fix expression. 4/18/2019 Queues
Example Convert the expression 1 + 2*(3*4 + 5)/6 – 7 from infix to postfix notation. 4/18/2019 Queues
Example Read 1 : Enqueue 1. Read + : Push +. Read 2 : Enqueue 2. Stack Queue 1 Read + : Push +. Stack + Queue 1 Read 2 : Enqueue 2. Stack + Queue 2 1 Read * : Push *. Stack + * Queue 2 1 Read ( : Push (. Stack + * ( Queue 2 1 4/18/2019 Queues
Example Read 3 : Enqueue 3. Read * : Push *. Read 4 : Enqueue 4. Stack + * ( Queue 3 2 1 Read * : Push *. Stack + * ( * Queue 3 2 1 Read 4 : Enqueue 4. Stack + * ( * Queue 4 3 2 1 Read + : Pop and enqueue *, push +. Stack + * ( + Queue * 4 3 2 1 Read 5 : Enqueue 5. Stack + * ( + Queue 5 * 4 3 2 1 4/18/2019 Queues
Example Read ) : Pop and enqueue +, pop (. Stack + * Queue + 5 * 4 3 2 1 Read / : Pop and enqueue *, push /. Stack + / Queue * + 5 * 4 3 2 1 Read 6 : Enqueue 6. Stack + / Queue 6 * + 5 * 4 3 2 1 Read – : Pop and enqueue / and +, push –. Stack – Queue + / 6 * + 5 * 4 3 2 1 Read 7 : Enqueue 7, pop and enqueue –. Stack Queue – 7 + / 6 * + 5 * 4 3 2 1 4/18/2019 Queues
Example The queue contains – 7 + / 6 * + 5 * 4 3 2 1. These tokens will be dequeued in the order 1 2 3 4 * 5 + * 6 / + 7 –, which is the postfix order for the original expression. Read 1 : Push 1. Stack 1 Read 2 : Push 2. Stack 2 4/18/2019 Queues
Example Read 3 : Push 3. Read 4 : Push 4. Stack 1 2 3 Read 4 : Push 4. Stack 1 2 3 4 Read * : Pop 4 and 3, push 12. Stack 1 2 12 Read 5 : Push 5. Stack 1 2 12 5 Read + : Pop 5 and 12, push 17. Stack 1 2 17 4/18/2019 Queues
Example Read * : Pop 17 and 2, push 34. Read 6 : Push 6. Stack 1 34 Read 6 : Push 6. Stack 1 34 6 Read / : Pop 6 and 34, push 5.667. Stack 1 5.667 Read + : Pop 5.667 and 1, push 6.667. Stack 6.667 Read 7 : Push 7. Stack 6.667 7 4/18/2019 Queues
Example Read – : Pop 7 and 6.667, push -0.333. Stack -0.333. 4/18/2019 Queues