Data structure and algorithm--stack implementation suffix expression and infix expression conversion

Source: Internet
Author: User

Calculation: The use of postfix expression to calculate the specific approach: to build a stack s. Read the expression from left to right, if the operand is read into the stack s, if you read the N-ary operator (that is, the operator that requires the number of arguments N), the top-down N-items are fetched by the operand, and the result of the operation is replaced by the N of the top of the stack and pressed into the stack s. If the suffix expression is not read, the above procedure is repeated, and the value at the top of the last output stack is the end. Conversion: Computer Implementation transformation: The algorithm idea of converting infix expression to suffix expression: • Start scanning; • When numbers are added, the suffix expression is appended; operator: A. If ' (', into the stack; b. If ') ', then add the operator in the stack to the suffix expression, until ' (', delete from stack ' ('; c. If it is an operator other than parentheses, it is directly into the stack when it has precedence over the top operator except ' ('). Otherwise, starting at the top of the stack, the operator that pops higher than the currently processed precedence and priority is popped, until a lower priority is reached or an opening parenthesis is encountered. • When the infix expression of the scan ends, all the operators in the stack are out of the stack; Manually implemented conversions here I give an infix expression: a+b* C-(D+e) First step: parentheses all operands according to the precedence of the Operator: ((A + (B*C))-(d+e)) Step two: convert prefix to suffix expression prefix: Move the operation symbol to the corresponding parenthesis before it becomes:-(+ (A * (BC)) + (DE)) Remove the brackets:-+a*bc+de prefix suffix: Move the operation symbol to the corresponding parenthesis after it becomes: ((A (BC) *) + (DE) +)-The parentheses are removed: abc*+de+-suffix expression found no, prefix, Suffixes are not required to be prioritized with parentheses. such as expression: 2-5) *6/3 suffix expression stack 3_________________+3 ________________+ (3 2 _______________+ (-3 2 5-_____________ +3 2 5-________ _____+*3 2 5-6 * ___________+/3 2 5-6 * __________+/3 2 5-6 */+________ ("_____" is used to separate the suffix expression and stack) Another person thinks the correct conversion method: ergodic infix expression For each node, if: 1, the node is an operand: a direct copy into the suffix expression 2, the node is an operator, in the following cases: A, for "(" Operator: Press into the temporary stack B, the ")" operator: The top operator of the temporary stack is continuously ejected until the top operator is "("), delete ' (') from the stack. and add the pop-up operators to the suffix expression. C, for other operators, has the following steps: Compare the operator's precedence to the operator on the top pointer of the temporary stack, and if the top pointer of the temporary stack has a priority greater than or equal to the precedence of the operator, pops up and adds to the suffix expression, repeatedly performing the previous comparison work, Until you encounter a stack-top pointer that has a lower priority than the operator's priority, stop the pop-up and press the operator into the stack. At this point the comparison process if the pointer to the top of the stack is ' (', then stop the loop and put the operator into the stack, note: ' (' Do not bounce out. After iterating through the infix expression, check the temporary stack, and if there are operators, all pop up and add to the suffix expression.  java Code:
1     /**    2 * Sequential traversal recursive implementation3     *    4 * Right and left.5     * @paramnode6     */    7  Public voidNextorder (node node) {8     if(node = =NULL) {    9         return; Ten     }     One Nextorder (Node.left);  A Nextorder (node.right);  - System.out.println (Node.data);  - }     the   -     /**     - * post-sequential traversal non-recursive implementation -     * @paramnode +     */     -  Public voidNextOrder2 (node node) { +     if(node = =NULL) {     A         return;  at     }     -stack<node> stack =NewStack<>();  -Node p =node;  -      while(Node! =NULL) {     -          while(Node.left! =NULL) {     - Stack.push (node);  innode =Node.left;  -         }     to          while(Node! =NULL&& (Node.right = =NULL|| Node.right = =p)) { + System.out.println (Node.data);  -p =node;  the             if(Stack.isempty ()) { *                 return;  $             }    Panax Notoginsengnode =Stack.pop ();  -         }     the Stack.push (node);  +node =Node.right;  A     }     the}

Data structure and algorithm--stack implementation suffix expression and infix expression conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.