Practical Assigment 6 expression tree
Practical Assigment 6 expression tree
Problem Statement: Construct an Expression Tree from postfix expression. Perform recursive
and nonrecursive In-order, pre-order and post-order traversals.
Objectives:
1. To construct expression trees from given postfix expressions.
2. To traverse expression trees recursively and non recursively
Theory:
Expression Tree:
Expression trees are the binary trees that are used to express various expressions like infix,
postfix, and prefix expressions. The internal nodes of this expression tree consist of operators
like +, -, \*,/,^, etc. And the external nodes(leaf nodes) of the Expression tree contain the
operands, on which operations are performed. The leaves of an Expression tree always
contain the operands that can be of any data type like character, integer, double, etc.
● After understanding the basic definition of Expression trees, let's understand the steps
needed for the construction of the Expression tree using the given input expression.
During the construction of the Expression tree, we use Stack (Stack is a data structure
based on the Last In First Out Principle).
● The given expression can be of any type like infix, postfix, etc. The most interesting
fact about the Expression tree is that when we perform the postorder traversal on the
Expression tree, we get the postfix expression, for the preorder traversal on the
Expression tree we get the prefix expression, and similarly, inorder traversal gives the
infix expression.
● For the construction of the Expression tree from the given input expression we use
stack data structure which handles elements in a Last In First Out manner. Stack helps
in storing the root of the Expression tree until that point and adding nodes to the
Expression tree once it goes into the next character of the given expression.
Algorithm:
Below is the algorithm to be followed for the construction of the Expression tree from the
given expression using stack data structure:
We need to traverse through the given expression using a for loop on it, then follow the
below steps for every character in the given expression:
● If the character is an operand then we need to push that character into the stack.
● If the character is an operator then pop two nodes from the stack and make them the
children of the present operator, first popped node should be on the left side and the
second popped node should be on the right side of the operator.
● At last, we need to push this modified node into the stack again. The below image
clearly explains the above step:
At any instant, the stack stores the root nodes of the subtrees of the final Expression tree.
These nodes are combined as an operator arrives in the expression and the root node of a
larger subtree is pushed into the stack. This continues till the expression is over.
At the end of traversal on the given expression, the stack contains only one node which is the
root node of the Expression tree formed from the given expression. The root of the
Expression tree is always an operator, and generally, the operator with the least precedence is
present as the root of the Expression tree.
Let us understand this above process using a postfix expression. The implementation of the
expression tree is described for the below expression.
ab+c*
Step1 : The first two symbols are operands, for which we generate a one-node tree and push
a reference to the stack.
Step2 : Next, read a’+’ symbol to pop two pointers to the tree, form a new tree, and push a
pointer to it onto the stack.
Step3 : In the next stage ‘c’ is read, we build a single node tree and store a pointer to it on the
stack