简体   繁体   中英

Python Expression Trees

I'm having trouble with a question:

Draw the expression tree obtained from the following fully parenthesised algebraic expression:

"( ( ( 4 + ( 9 * 3 ) ) - ( ( 6 * ( 2 + 4 ) ) / 3 ) ) + 1 )"

Write the elements of the expression tree which are at level 4 (the root node is level 0). Write the level four elements left to right, separated by a single space.

So far I have the following expression tree:

                                  -
                   +                             +
              4           *             /               1
                        9   3        *    3
                                  6    +
                                      2 4

and then as far as I know the 4th level elements are:

9 3 * 3   

but it is wrong. Am I doing something wrong?

The mistake is in root , so it must be + with 1 and - as its childes. attend to your parenthesis !

                              +
                 -                       1
          +           /       
        4   *      *    3          
           9  3  6   +       
                   2   4  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM