8 Lecture

CS301

Midterm & Final Term Short Notes

Conversion from infix to postfix

Infix notation is a commonly used mathematical notation where operators are written between their operands. Converting infix notation to postfix notation involves rearranging the operators and operands so that operators appear after their operan


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which of the following data structures is commonly used for converting infix expressions to postfix expressions? a. Stack b. Queue c. Linked List d. Heap Answer: a. Stack

  2. In infix notation, where are operators written in relation to their operands? a. Before b. After c. Between d. Both a and c Answer: d. Both a and c

  3. What is the first step in converting an infix expression to postfix notation? a. Scan the expression from left to right b. Initialize an empty stack and postfix expression c. Check for balanced parentheses d. Add operands to the postfix expression Answer: b. Initialize an empty stack and postfix expression

  4. Which of the following operators has the highest precedence in mathematical expressions? a. Multiplication b. Division c. Addition d. Subtraction Answer: a. Multiplication

  5. What is the role of a stack in converting infix to postfix notation? a. To keep track of operators and their precedence levels b. To keep track of operands and their positions c. To perform the necessary calculations d. To check for errors in the expression Answer: a. To keep track of operators and their precedence levels

  6. What happens to a left parenthesis when converting from infix to postfix notation? a. It is added to the postfix expression b. It is pushed onto the stack c. It is discarded d. It is popped off the stack Answer: b. It is pushed onto the stack

  7. How can errors be handled while converting infix to postfix notation? a. By checking for balanced parentheses b. By checking for errors during scanning c. By using a queue data structure d. Both a and b Answer: d. Both a and b

  8. Which of the following expressions is equivalent to "a + b * c" in postfix notation? a. "a b c * +" b. "a b + c *" c. "a + b c *" d. "a b c + *" Answer: a. "a b c * +"

  9. What is the final step in converting an infix expression to postfix notation? a. Pop any remaining operators off the stack and add them to the postfix expression b. Add operands to the postfix expression c. Discard the left parenthesis d. Push the right parenthesis onto the stack Answer: a. Pop any remaining operators off the stack and add them to the postfix expression

  10. Which of the following is an advantage of postfix notation over infix notation? a. It is easier to read and write b. It eliminates the need for parentheses to indicate the order of operations c. It is more commonly used in mathematical expressions d. Both a and b Answer: b. It eliminates the need for parentheses to indicate the order of operations.



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the main advantage of postfix notation over infix notation? Answer: Postfix notation eliminates the need for parentheses to indicate the order of operations.

  2. What is the role of a stack in converting an infix expression to postfix notation? Answer: The stack is used to keep track of operators and their precedence levels.

  3. How do you handle errors while converting an infix expression to postfix notation? Answer: By checking for balanced parentheses and errors during scanning.

  4. What is the first step in converting an infix expression to postfix notation? Answer: Initializing an empty stack and postfix expression.

  5. How do you handle operators with equal precedence levels while converting infix to postfix notation? Answer: Operators with equal precedence levels are added to the postfix expression based on the associativity rules (left to right or right to left).

  6. What happens to a left parenthesis when converting infix to postfix notation? Answer: The left parenthesis is pushed onto the stack.

  7. What is the final step in converting an infix expression to postfix notation? Answer: Pop any remaining operators off the stack and add them to the postfix expression.

  8. What is the difference between infix notation and postfix notation? Answer: In infix notation, operators are written between their operands, while in postfix notation, operators are written after their operands.

  9. What are the advantages of using a postfix notation over infix notation? Answer: Postfix notation eliminates the need for parentheses to indicate the order of operations, and it is easier to evaluate expressions using a stack-based algorithm.

  10. How can you convert an infix expression with nested parentheses to postfix notation? Answer: By using a stack-based algorithm to remove the nested parentheses and convert the expression to postfix notation.

In computer science, infix notation is a standard mathematical notation in which arithmetic operations are written between their operands. However, it can be difficult to evaluate an infix expression using a computer program because of the need to keep track of parentheses and operator precedence. To simplify this process, postfix notation can be used. Postfix notation, also known as Reverse Polish Notation (RPN), is a way of writing arithmetic expressions where operators are written after their operands. This notation is easier to evaluate using a stack-based algorithm. The conversion from infix to postfix notation involves the following steps:
  1. Initialize an empty stack and postfix expression.
  2. Scan the infix expression from left to right.
  3. If the current character is an operand, add it to the postfix expression.
  4. If the current character is an operator, then: a. Pop operators from the stack and add them to the postfix expression until an operator with lower precedence is encountered, or the stack is empty. b. Push the current operator onto the stack.
  5. If the current character is a left parenthesis, push it onto the stack.
  6. If the current character is a right parenthesis, then: a. Pop operators from the stack and add them to the postfix expression until a left parenthesis is encountered. b. Discard the left parenthesis.
  7. Repeat steps 3-6 until the end of the infix expression is reached.
  8. Pop any remaining operators off the stack and add them to the postfix expression.
For example, let's consider the infix expression "2 + 3 * 4 / 2". Using the above algorithm, we can convert it to postfix notation as follows: Step 1: Initialize an empty stack and postfix expression. Stack: [] Postfix: [] Step 2: Scan the infix expression from left to right. Character: 2 Stack: [] Postfix: [2] Character: + Stack: [+] Postfix: [2] Character: 3 Stack: [+] Postfix: [2, 3] Character: * Stack: [*, +] Postfix: [2, 3] Character: 4 Stack: [*, +] Postfix: [2, 3, 4] Character: / Stack: [/, *, +] Postfix: [2, 3, 4] Character: 2 Stack: [/, *, +] Postfix: [2, 3, 4, 2] Step 8: Pop any remaining operators off the stack and add them to the postfix expression. Stack: [/, *] Postfix: [2, 3, 4, 2, /, *] Thus, the postfix notation for the infix expression "2 + 3 * 4 / 2" is "2 3 4 * 2 / +".