Prefix Traversal to form Expression from Expression Tree

<< Previous - Expression Tree Examples

Postfix Expression from Expression Tree >>





Expression Tree is used to represent expressions. Let us look at how to form prefix expressions from an expression tree

Steps to Write Prefix Expressions from Expression Tree for a*b+c


Expression Tree for a*b+c

Writing Prefix Expression From Expression Tree

Let us how to generate the prefix expression from the tree. A prefix expression is generated from the tree as follows:

  1. Consider the root + .
    Prefix expression created so far = + 
  2. Next consider the left subtree a * b.
    • For a * b, Consider the root of subtree *.
      Prefix expression created so far = + *  
    • For a * b, Consider the left subtree a. Left subtree has only one node a, Hence, first write the same.
      Prefix expression created so far = + * a 
    • For a * b, Consider the right subtree b. Right subtree b is just a node.
      Prefix expression created so far = + * a b
  3. Consider the right subtree c . Since the right subtree is just a simple node, there is no more subtrees to be checked
    Prefix expression created so far = + * a b c

How to write Infix Expression >>

How to write Postfix Expression >>




<< Previous - Expression Tree Examples

Next - Postfix Traversal >>














Prefix Traversal to form Expression from Expression Tree

<< Previous - Expression Tree Examples

Postfix Expression from Expression Tree >>





Expression Tree is used to represent expressions. Let us look at how to form prefix expressions from an expression tree

Steps to Write Prefix Expressions from Expression Tree for a*b+c


Expression Tree for a*b+c

Writing Prefix Expression From Expression Tree

Let us how to generate the prefix expression from the tree. A prefix expression is generated from the tree as follows:

  1. Consider the root + .
    Prefix expression created so far = + 
  2. Next consider the left subtree a * b.
    • For a * b, Consider the root of subtree *.
      Prefix expression created so far = + *  
    • For a * b, Consider the left subtree a. Left subtree has only one node a, Hence, first write the same.
      Prefix expression created so far = + * a 
    • For a * b, Consider the right subtree b. Right subtree b is just a node.
      Prefix expression created so far = + * a b
  3. Consider the right subtree c . Since the right subtree is just a simple node, there is no more subtrees to be checked
    Prefix expression created so far = + * a b c

How to write Infix Expression >>

How to write Postfix Expression >>




<< Previous - Expression Tree Examples

Next - Postfix Traversal >>