Postfix Expression from Expression Tree
<< Previous - Prefix Traversal
Expression Tree is used to represent expressions. Let us look at how to traverse an expression tree to produce an postfix expression from expression tree:
Steps to Traverse Expression Tree to produce Postfix Expression for a*b+c
Generate Postfix Expression From Expression Tree
Let us how to generate the Postfix expression from tree. A postfix expression is generated from the tree as follows:
- First consider the left subtree a * b.
- For a * b, consider the left subtree a. Left subtree has only one node a, Hence, first write the same.
Postfix expression created so far = a
- Consider the right subtree. Right subtree b is just a node.
Postfix expression created so far = a b
- Consider the root of subtree *.
Postfix expression created so far = a b *
- For a * b, consider the left subtree a. Left subtree has only one node a, Hence, first write the same.
- Consider the right subtree c . Since the right subtree is just a simple node, there is no more subtrees to be checked
Postfix expression created so far = a b * c
- Consider the root + .
Postfix expression created so far = a b * c +
Postfix expression
a b * c +
How to write Prefix Expression >>
How to write Infix Expression >>
<< Previous - Prefix Traversal