Postfix Expression from Expression Tree

<< Previous - Prefix Traversal

Next - Infix 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


Expression Tree 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:

  1. 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 * 
  2. 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 
  3. 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

Next - Infix Traversal >>







Postfix Expression from Expression Tree

<< Previous - Prefix Traversal

Next - Infix 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


Expression Tree 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:

  1. 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 * 
  2. 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 
  3. 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

Next - Infix Traversal >>