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

Expression Trees: Data Structures & Algorithms

Expression trees are binary trees used to represent arithmetic expressions, with operands at the leaves and operators at internal nodes. They can be traversed in different orders like inorder, preorder and postfix to evaluate the expression or convert between infix, prefix and postfix notations. Postfix expressions can be converted to expression trees by pushing operands and operators on a stack according to their order.

Uploaded by

Nouman Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Expression Trees: Data Structures & Algorithms

Expression trees are binary trees used to represent arithmetic expressions, with operands at the leaves and operators at internal nodes. They can be traversed in different orders like inorder, preorder and postfix to evaluate the expression or convert between infix, prefix and postfix notations. Postfix expressions can be converted to expression trees by pushing operands and operators on a stack according to their order.

Uploaded by

Nouman Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Expression Trees

Data structures & Algorithms


Expression Trees
• Is a Binary Tree.
• Leaves of an expression tree are operand,
such as constants or variable names.
• Other nodes contain the operators.
Expression Trees
• We can evaluate and expression tree by
applying the operator at the root to the values
obtained by recursively evaluating the left and
right sub-trees.
+
(a + b * c) +((d * e + f) *g) *

+ + g

a * * f

b c d e
Expression Trees
• Inorder Traversal (left, Root, Right)
+

+ + g

a * * f

b c d e
Expression Trees
• Inorder Traversal (left, Root, Right)
– a+ b * c + d * e + f * g (infix notation)
+
*

+ + g

a * * f

b c d e
Expression Trees
• Preorder Traversal (Root, Left, Right)
+

+ + g

a * * f

b c d e
Expression Trees
• Preorder Traversal (Root, Left, Right)
– + + a * b c * + * d e f g (Prefix Notation)
+
*

+ + g

a * * f

b c d e
Expression Trees
• Postorder Traversal (Left, Right, Root)
+

+ + g

a * * f

b c d e
Expression Trees
• Postorder Traversal (Left, Right, Root)
– a b c * + d e * f + g * + (Postfix Notation)

+
*

+ + g

a * * f

b c d e
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+**
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** a, b are operands push pointers to
their nodes in the stack

b
a
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** + operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.

a b
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** push pointers to the nodes of the
operands c, d & e

e
d
c
+

a b
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** + operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.

c
+
+
d e
a b
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** * operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.

c +
+
d e
a b
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** * operator again, pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.

*
*

c +
+
d e
a b

You might also like