# What Is Expression Tree in Data Structure With Example?

//

Scott Campbell

What Is Expression Tree in Data Structure With Example?

An expression tree is a binary tree representation of an arithmetic expression. It is a data structure that helps in evaluating mathematical expressions efficiently.

Each node in the tree represents an operator or an operand, and the leaves of the tree are operands. The operators are stored in internal nodes of the tree, and each internal node has exactly two children.

## Example:

Let’s consider the following arithmetic expression: (5 + 3) * 2. We can represent this expression using an expression tree as shown below:

In the above example, the operators ‘+’ and ‘*’ are represented by internal nodes, while the operands ‘5’, ‘3’, and ‘2’ are represented by leaf nodes. The root node of the tree represents the overall expression.

## Creating an Expression Tree:

To create an expression tree, we follow these steps:

• Create a stack to store the nodes of the expression tree.
• Start scanning from left to right through the given arithmetic expression.
• If an operand is encountered, create a new node with its value and push it onto the stack.
• If an operator is encountered, create a new node with its value and pop two nodes from the stack to make them its children. Then push this new node onto the stack.
• Repeat steps 3-4 until all elements of the arithmetic expression are scanned.
• The top of the stack will contain the root of the expression tree.

### Example:

Let’s create an expression tree for the arithmetic expression: (4 + 2) * 3.

1. Scan the expression from left to right.
2. Encounter ‘(‘ – Ignore it and move to the next element.
3. Encounter ‘4’ – Create a node and push it onto the stack.
4. Encounter ‘+’ – Create a node, pop two nodes from the stack (4 and NULL), and make them its children. Push this new node onto the stack.
5. Encounter ‘2’ – Create a node and push it onto the stack.
6. Encounter ‘)’ – Ignore it and move to the next element.
7. Encounter ‘*’ – Create a node, pop two nodes from the stack (+ and 2), and make them its children.

Push this new node onto the stack.

8. Encounter ‘3’ – Create a node and push it onto the stack.
9. No more elements to scan. The top of the stack contains the root of the expression tree (*) with children (+ and 2) on its left, and ‘3’ on its right.

The resulting expression tree for ‘(4 + 2) * 3’ would look like this:

## Evaluating an Expression Tree:

To evaluate an expression tree, we recursively traverse through each node of the tree in postorder (left subtree, right subtree, root). When we encounter an operator, we evaluate its children and perform the corresponding arithmetic operation.

Finally, we return the result obtained at the root node.

Using the above expression tree example, let’s evaluate the arithmetic expression ‘(4 + 2) * 3’.

1. Traverse through the left subtree: ‘4’.
2. Traverse through the right subtree: ‘2’.
3. Evaluate operator ‘+’: ‘4 + 2 = 6’.
4. Traverse through the right subtree: ‘3’.
5. Evaluate operator ‘*’: ‘6 * 3 = 18’.

The evaluation of ‘(4 + 2) * 3′ using the expression tree is equal to ’18’.

Expression trees are useful in evaluating mathematical expressions efficiently and can be applied in various applications like compilers, calculators, and symbolic mathematics systems. Understanding how to construct and evaluate expression trees is essential for anyone working with data structures or algorithms related to arithmetic expressions.