1 / 7

Infix, Postfix, Prefix

Infix, Postfix, Prefix. Postfix. Postfix notation is a way of writing algebraic expressions without the use of parentheses or rules of operator precedence. The expression (A+B)/(C-D) would be written as AB+CD-/ in postfix notation. Prefix.

Download Presentation

Infix, Postfix, Prefix

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Infix, Postfix, Prefix

  2. Postfix • Postfix notation is a way of writing algebraic expressions without the use of parentheses or rules of operator precedence. • The expression (A+B)/(C-D) would be written as AB+CD-/ in postfix notation.

  3. Prefix • In prefix notation  the operator is written before its operands without the use of parentheses or rules of operator precedence. • The expression (A+B)/(C-D) would be written as /+AB-CD in prefix notation.

  4. Infix->postfix • Variables (numbers) are copied to the output • Left parentheses are always pushed onto the stack • When a right parenthesis is encountered, the symbol at the top of the stack is popped off the stack and copied to the output until the symbol at the top of the stack is a left parenthesis. When that occurs, both parentheses are discarded. • If the symbol being scanned has a higher precedence than the symbol at the top of the stack, the symbol being scanned is pushed onto the stack. • If the precedence of the symbol being scanned is lower than or equal to the precedence of the symbol at the top of the stack, the stack is popped to the output. • When the terminating symbol is reached on the input scan, the stack is popped to the output until the terminating symbol is also reached on the stack. Then the algorithm terminates

  5. Infix->prefix 1) Reverse the input string. 2) Examine the next element in the input. 3) If it is operand, add it to output string. 4) If it is closing parenthesis, push it on stack. 5) If it is an operator, then i) If stack is empty, push operator on stack. ii) If the top of stack is closing parenthesis, push operator on stack. iii) If it has same or higher priority than the top of stack, push operator on stack. iv) Else pop the operator from the stack and add it to output string, repeat step 5.

  6. Prefix -> postfix 1. Initialize the input queue, the output queue, and the operators stack. 2. Repeat steps 3-5 until there are no more input symbols. 3. Get the next input symbol. 4. If it is an operator, put it on the stack with its associated flag off. 5. If it is an operand, append it to the output queue. Furthermore, the operand must correspond to the top operator on the stack. (Why?) Of course, the stack cannot be empty at this point. (Why?) Finally, here is the important question: is the operand just processed the first or the second operand associated with the top operator? If it is the second, it is time to append the top operator to the output queue. (If not?)

  7. Prefix -> postfix Prefix: + * A B / C D Postfix: A B * C D / +

More Related