Expression Tree
Expression Tree
The operators, constants, and variables are arranged in such a way that an inorder traversal of the tree produces the original expression without parentheses.
+
3+(4*5-(9+6)) 3 3+4*5-9+6 + 5 log 9 6
*
4
log(x)
x ! n! n
log x
n!
* 3
a b
* d c
a - (b * c)
-a*bc
a-b*c
abc*-
log (x)
log x
log x
x log
n!
!n
n!
n!
+
2 3 * *
5
4 +
2-3*4+5
(2 - 3) * (4 + 5)
2-3*4+5
2
2
3
-
4
+ * 5 4
5
2-3*4+5
2 - (3 * 4 + 5)
a/b^cd*ea*c^3^4
Not using normal mathematical operator precedence
abc^/de*ac34^^*--
a/b^cd*ea*c^3^4
Not using normal mathematical operator precedence
abc^/de*ac34^^*--
( (a / (b ^ c)) ( (d * e) (a * (c ^ (3 ^ 4) ) ) ) )
1 while(not the end of the expression) 2 { 3 if(the next symbol in the expression is an operand) 4 { 5 create a node for the operand ; 6 push the reference to the created node onto the stack ; 7 } 8 if(the next symbol in the expression is a binary operator) 9 { 10 create a node for the operator ; 11 pop from the stack a reference to an operand ; 12 make the operand the right subtree of the operator node ; 13 pop from the stack a reference to an operand ; 14 make the operand the left subtree of the operator node ; 15 push the reference to the operator node onto the stack ; 16 } 17 }
INFIX EXPRESSION (1+3)*(6-4) PREFIX EXPRESSION *+13-64 Read the next arithmetic operator or numeric value. Create a node containing the operator or numeric value. If the node contains an operator Recursively build the sub trees corresponding to the operators operand Else The node is a leaf node