Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Infix and postfix expressions, Exercises of Data Structures and Algorithms

Infix and postfix expressions. In a postfix expression,. • an operator is written after its operands. • the infix expression 2+3 is 23+ in postfix notation.

Typology: Exercises

2021/2022

Uploaded on 09/27/2022

robinhood05
robinhood05 🇬🇧

4.8

(16)

229 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Infix and postfix expressions
In a postfix expression,
an operator is written after its operands.
the infix expression 2+3 is 23+ in postfix notation.
For postfix expressions, operations are performed in the order in which they are
written (left to right).
No parentheses are necessary. ‘
the infix expression 2+3*4 is 234*+ in postfix notation
the infix expression 3*4+2*5 translates to 34*25*+ in postfix notation.
the infix expression 3*(4+2)*5 translates to 342+*5*
Evaluation of postfix expressions.
2+3*4 (infix) / 234*+ (postfix) expression. Notice:
the operands (2,3,and 4) appear in the same order in both expressions.
in the postfix version the operators ( * and +) appear in the order in which they
are performed -- the multiplication before the addition
writing the operators in the order in which they are performed makes postfix
expressions easy to evaluate using the following algorithm:
1. scan the expression, left to right, until you encounter an operator, @
(@ means + - * or /)
2. Perform the operation @. The operands precede the operator
3 4 + = 3+4= 7
3. In the expression, replace @ and its operands with the computed value
4. repeat 1-3 the process until no more operators exist.
Look at 234*+.
Here is the sequence of operations:
2 3 4 * + * is the first operator. Perform the operation 34*
2 12 + 3 4*is replaced by 12 , the value of 3*4
+ is next operator, perform 2 12+
replace 2 12+ with 14. Done
The value of the expression is 14. Another example, 3 4 * 2 5 * + which in infix
notation is 3*4 + 2*5.
3 4 * 2 5 * + * is the first operator 3 4 * is replaced by 12
12 2 5 * + 2 5 * is replaced by 10
12 10 + 12 10 + is replaced by 22
22
Postfix notation does not require parentheses.
Evaluation of postfix with a stack"
Scan the string left to right.
When you encounter an operand push it on the stack;
when you encounter an operator, pop the corresponding operands off the stack,
perform the operation, and push the result back on the stack.
pf3
pf4

Partial preview of the text

Download Infix and postfix expressions and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Infix and postfix expressions In a postfix expression,

  • an operator is written after its operands.
  • the infix expression 2+3 is 23+ in postfix notation.
  • For postfix expressions, operations are performed in the order in which they are written (left to right).
  • No parentheses are necessary. ‘
  • the infix expression 2+3*4 is 234*+ in postfix notation
  • the infix expression 34+2 5 translates to 3425+ in postfix notation.
  • the infix expression 3(4+2)5 translates to 342+5 Evaluation of postfix expressions. 2+34 (infix) / 234+ (postfix) expression. Notice:
  • the operands ( 2,3,and 4) appear in the same order in both expressions.
  • in the postfix version the operators ( * and + ) appear in the order in which they are performed -- the multiplication before the addition
  • writing the operators in the order in which they are performed makes postfix expressions easy to evaluate using the following algorithm:
  1. scan the expression, left to right, until you encounter an operator, @ (@ means + - * or /)
  2. Perform the operation @. The operands precede the operator 3 4 + = 3+4= 7
  3. In the expression, replace @ and its operands with the computed value
  4. repeat 1-3 the process until no more operators exist. Look at 234*+. Here is the sequence of operations:
  • 2 *3 4 *** + * is the first operator. Perform the operation 34
  • 2 12 + 3 4is replaced by 12 , the value of 3
  • is next operator, perform 2 12+
  • replace 2 12+ with 14. Done The value of the expression is 14. Another example, 3 4 * 2 5 * + which in infix notation is 34 + 2. **3 4 *** 2 5 * + * is the first operator 3 4 * is replaced by 12 12 **2 5 *** + 2 5 * is replaced by 10 12 10 + 12 10 + is replaced by 22 22 Postfix notation does not require parentheses. Evaluation of postfix with a stack"
  • Scan the string left to right.
  • When you encounter an operand push it on the stack;
  • when you encounter an operator, pop the corresponding operands off the stack,
  • perform the operation, and push the result back on the stack.
  • When you are finished scanning the expression, the final value remains on the stack. For example, consider the postfix expression 234*+ Input Stack (top is on the left) 2 3 4 * + empty Push 2 3 4 * + 2 Push 3 4 * + 3 2 Push 4
      • 4 3 2 Pop 4, pop 3, do 3 *4 , push 12
    • 12 2 Pop 12, Pop 2, do 2 + 12, push 14 14 Input Stack 3 4 * 2 5 * + empty Push 3 4 * 2 5 * + 3 Push 4
    • 2 5 * + 4 3 Pop 4, pop 3, do 3*4, Push 12 2 5 * + 12 Push 2 5 * + 2 12 Push 5
      • 5 2 12 Pop 5, Pop 2, do 2*5, Push 10
    • 10 12 Pop 10, Pop 12 do 12 + 10, push 22 22 Here is an algorithm to evaluate postfix expressions. To eliminate some unnecessary and non-instructive details make a few simplifying assumptions:
  1. all input numbers are in the form of single digits 0.. There is no whitespace in the input string. Thus 345+* is valid but 3 4 5 +* is not.
  2. the only operators allowed are the binary operators +,-,*, and /, where / signifies integer division.
  3. all input data is correct. Thus a typical input string is 2373/+, which in infix notation is 23 + 7/3 (value is 8). Making these assumptions, the algorithm for postfix evaluation is **while characters remain in the postfix string
  4. read a character
  5. if the character is a digit, convert to int and push
  6. if the character is an operator pop the stack twice obtaining the two operands perform the operation push the result Pop the final value from the stack.**

Examples. Input Stack Postfix 23 + 45 empty 3+45 empty 2 3+45 * 2 +45 * 23 45 + 23 5 + 23 5 + 23 + 23

  • 2345 empty 2345+ Input Stack Postfix 2 - 3+4-5*6 empty
  • 3+4-56 empty 2 3+4-56 - 2 +4-56 - 23 4 - 56 + 23 -
  • 56 + 23 - 4 56 - 23 - 4+ *6 - 23 - 4+ 6 *- 23 - 4+ *- 23 - 4+
    • 23 - 4+56* empty 23 - 4+56- Input Stack Postfix (2-3+4)(5+67) empty 2 - 3+4)(5+6*7) (
  • 3+4)(5+67) ( 2 3+4)(5+67) (- 2 +4)(5+67) (- 23 4)(5+67) (+ 23 - )(5+67) (+ 23 - 4 (5+67) empty 23 - 4+ (5+67) * 23 - 4+ 5+67) (* 23 - 4+ +67) ( 23 - 4+ 67) +( 23 - 4+ 7) +( 23 - 4+
  1. +( 23 - 4+ ) +( 23 - 4+
    • 23 - 4+567+ empty 23 - 4+567+*