How to convert an infix to a prefix or suffix (Poland and reverse Poland) combined with a binary tree (suitable for Data Structure Understanding)

Source: Internet
Author: User

1. Relationship Between Expressions and Binary Trees

Prefix expressions correspond to the forward traversal of Binary trees;

The infix expression corresponds to the ordinal traversal of a binary tree;

Suffix expressions correspond to post-sequential traversal of Binary trees;

 

Ii. Generate a binary tree based on the infix expression

Infix expression: A + B * (c-d)-E/F

The middle-order traversal is: Left son, right son, and root node (kaiwii corrected to: Left son, root node, right son)

Based on the operator priority, the binary tree generation process is as follows:

1. c-d has a high priority. The root is the-operator. C and D are the right and right sons respectively.

-

|

C d

2. The next step is multiplication. The root is the * operator. The content in B and 1 is the left and right sons respectively.

*

|

B-

|

C d

3. Next is the trigger. The root is the/operator. E and F are the right and right sons.

/

|

E F

4. The next step is addition. The root is the + operator. The content in A and 2 is the left and right sons respectively.

+

|

A *

|

B-

|

C d

It also includes the tree in step 3.

5. The next step is subtraction. The root is the-operator. The two operators in 4 are the right and right sons.

-

|

+/

|

A * E F

|

B-

|

C d

 

3. Obtain the prefix expression by traversing the binary tree in the forward order.

The first traversal is: root node, left son, right son

The prefix expression is-+ a * B-CD/EF.

 

4. Obtain the suffix expression based on the Post-order traversal of the Binary Tree

Backward traversal: Left son, right son, and root node

The suffix expression is ABCD-* + EF /-

 

V. Summary

If you can determine a binary tree based on the prefix, infix, or suffix expression, you can generate the corresponding prefix, infix, and suffix expression.

The methods of pre-order traversal, middle-order traversal, and subsequent traversal are as follows:

1. The first order is that the root node is in the front edge, the middle order is that the root node is in the middle, and the root node is in the back

2. always start with the left son and then the right son.

Preface:Root Node, Left son, right son

Central order: Left son,Root NodeRight son

Follow-up: Left son, right son,Root Node

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.