Expression Tree
Expression Tree
CSE2001
By
Dr. Manomita Chakraborty
Assistant Professor
VIT-AP, Amaravati, AP, India
The operands are always represented by the leaf nodes. These operands are always used in the operations.
The operator at the root of the tree is always given top priority.
When compared to the operators at the bottom of the tree, the operator at the bottom is always given the lowest priority.
The expression tree can be traversed to evaluate prefix expressions, postfix expressions, and infix expressions.
Expression trees are used to evaluate mathematical expressions by traversing the tree in a specific order (such as in-order,
post-order or pre-order). This approach allows the tree to be evaluated recursively and efficiently. Additionally, an
expression tree can be used to convert the expressions from infix notation to postfix or prefix notation and vice
versa.
Expression trees are also used in compilers and interpreters to represent the syntax tree of a program. This allows for easy
Expression trees are also used in computer algebra systems and symbolic computation software to manipulate algebraic
a+b*c+d*e+f
Let us consider a postfix expression is given as an input for constructing an expression tree. Following are the step to construct an
expression tree:
3. If the symbol is an operand, create a one node tree and push a pointer onto a stack
4. If the symbol is an operator, pop two pointers from the stack namely T1 & T2 and form a new tree with root as the operator, T1 & T2
The first two symbols are operands, we create one- Next, read a'+' symbol, so two pointers to tree are
node tree and push a pointer to them onto the stack. popped, a new tree is formed and push a pointer to it
onto the stack.
Next, 'c' is read, we create one node tree and push a Finally, the last symbol is read ' * ', we pop two tree
pointer to it onto the stack. pointers and form a new tree with a, ' * ' as root, and a
pointer to the final tree remains on the stack..
* *
+ +
+ a
a b
11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 12
Expression Tree
Construction of expression tree from prefix expression:
Question : Create the expression tree from the given prefix expression: * + a b + c d
* * *
+ + +
+ + +
a b a b c d
a b c
5) ‘+’ is an operator. Insert + to right 7) ‘d’ is an operand. Insert ‘d’ to right child of current node. Revised
6) ‘c’ is an operand. Insert ‘c’ to left child of current node. Revised
child of current node. Revised current current pointer is at node = d. It is an operand so it will be a leaf node.
current pointer is at node = c. It is an operand so it will be a leaf node.
pointer is at node = +. It is an operator Back track to parent node of c. Revised current pointer is at node =+.
so it should have left and right child. No more Symbol. Stop the process.
Right child of current node is empty.
11/28/2023
By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 13
Expression Tree
Construction of expression tree from infix expression:
Example: a+b-c
Solution: + and – having same precedence. Parse from left to right according to rule. So, + will be the
root node.
+ -
a b
+ c
a b
e f
Example: a+b*c$e^f
Solution:
a *
b $
c ^
e f
Example: a$b*c-d^e
$
^
^
a d e
d e b
Example: a$b*c-d^e
Step 3) Next * is having higher precedence Step 4) create node for ‘-’
*
*
* ^
$ c
^
$ c d e
d e
a b
a b