0% found this document useful (0 votes)
91 views

Expression Tree

vfghfhfghhg

Uploaded by

tonystark44111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Expression Tree

vfghfhfghhg

Uploaded by

tonystark44111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Data Structures & Algorithms

CSE2001

By
Dr. Manomita Chakraborty
Assistant Professor
VIT-AP, Amaravati, AP, India

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 1


Expression Tree
The expression tree is a binary tree in which each internal node corresponds to the operator and each leaf node corresponds to the operand
so for example expression tree for 3 + ((5+9)*2) would be:

*********Brackets are not part of expression tree

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 2


Expression Tree
Properties:

 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.

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 3


Expression Tree
Applications:

 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

manipulation and evaluation of the program's structure and logic.

 Expression trees are also used in computer algebra systems and symbolic computation software to manipulate algebraic

expressions by simplification, differentiation, and integration.

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 4


Expression Tree
Infix expression from expression tree:

Infix expression can be obtained based on inorder traversal.

Infix expression: Do the inorder traversal

a+b*c+d*e+f

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 5


Expression Tree
postfix expression from expression tree:

postfix expression can be obtained based on postorder traversal.

postfix expression: Do the postorder


traversal
abc*+def+*+

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 6


Expression Tree
prefix expression from expression tree:

prefix expression can be obtained based on preorder traversal.

prefix expression: Do the preorder


traversal
++a*bc*d+ef

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 7


Expression Tree
Infix, Prefix and Postfix Expressions from Expression Tree for a+b*c+d

Expression Tree for a + b * c + d can be represented as:

Infix expression--- inorder traversal  a + b * c + d


Prefix expression--- preorder traversal  * + a b + c d
Postfix expression-- postorder traversal  a b + c d + *

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 8


Expression Tree
Construction of expression tree from postfix expression:

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:

1. Read one symbol at a time from the postfix expression.

2. Check if the symbol is an operand or operator.

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

as a left and right child

5. A pointer to this new tree is pushed onto the stack

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 9


Expression Tree
Construction of expression tree from postfix expression:

Example: consider a postfix expression: a b + c *

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.

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 10


Expression Tree
Construction of expression tree from postfix expression:

Example : The input is: a b + c * (contd..)

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..

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 11


Expression Tree
Construction of expression tree from prefix expression:
• 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
Question : Create the expression tree from the given prefix expression: * + a b + c d
Solution: create the tree depth wise in a preorder fashion
4) ‘b’ is an operand. Insert b to right child of
current node. Revised current pointer is at
node = b. It is an operand so it will be a leaf
3) ‘a’ is an operand. Insert ‘a’ to left child node. Back track to parent node of b. Revised
of current node. Revised current pointer is current pointer is at node =+. Already we
at node = a. It is an operand so it will be a have created both the child of ‘+’. So back
2) ‘+’ is an operator. Insert + to left child leaf node. Back track to parent node of a. track to parent of +. Revised current pointer is
1) ‘*’ is an operator. Current at node =*. Right child of current node is
of current node. Revised current pointer Revised current pointer is at node =+. Right
pointer is at node= *. It is an empty.
is at node = +. It is an operator so it child of current node is empty.
operator so it should have left
should have left and right child.
and right child.
* *

* *
+ +

+ 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:

1. Check which operator needs to solved first based on operator precedence.


2. Make that operator as the root node
3. Make the operand to the left of the operator as left child and operand to the right of the operator as right child.

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

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 14


Expression Tree
Construction of expression tree from infix expression:
Example: a+b*c$e^f

Step 3)Remaining operators: * and


Step 1) $ and ^ have higher precedence in Step 2) create node for $ +. * have higher precedence
the expression. They needs to be solved
compared to +. Create node for *.
first. They have same precedence. Parse
from right to left. So, ^ will be the root $ *
node.
^
c b $
^
e f
e f c ^

e f

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 15


Expression Tree
Construction of expression tree from infix expression:

Example: a+b*c$e^f
Solution:

Step 4) Create node for +.

a *

b $

c ^

e f

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 16


Expression Tree
Construction of expression tree from infix expression:

Example: a$b*c-d^e

Step 1) $ and ^ have higher precedence in


the expression. They needs to be solved Step 2) create node for $
first. They have same precedence. Parse
from right to left according to rule. So, ^ will
be the root node.

$
^
^

a d e
d e b

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 17


Expression Tree
Construction of expression tree from infix expression:

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

11/28/2023 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy