Home » Expression tree in data structure

Expression tree in data structure

by Online Tutorials Library

Expression tree in data structure

The expression tree is a tree used to represent the various expressions. The tree data structure is used to represent the expressional statements. In this tree, the internal node always denotes the operators.

  • The leaf nodes always denote the operands.
  • The operations are always performed on these operands.
  • The operator present in the depth of the tree is always at the highest priority.
  • The operator, which is not much at the depth in the tree, is always at the lowest priority compared to the operators lying at the depth.
  • The operand will always present at a depth of the tree; hence it is considered the highest priority among all the operators.
  • In short, we can summarize it as the value present at a depth of the tree is at the highest priority compared with the other operators present at the top of the tree.
  • The main use of these expression trees is that it is used to evaluate, analyze and modify the various expressions.
  • It is also used to find out the associativity of each operator in the expression.
  • For example, the + operator is the left-associative and / is the right-associative.
  • The dilemma of this associativity has been cleared by using the expression trees.
  • These expression trees are formed by using a context-free grammar.
  • We have associated a rule in context-free grammars in front of each grammar production.
  • These rules are also known as semantic rules, and by using these semantic rules, we can be easily able to construct the expression trees.
  • It is one of the major parts of compiler design and belongs to the semantic analysis phase.
  • In this semantic analysis, we will use the syntax-directed translations, and in the form of output, we will produce the annotated parse tree.
  • An annotated parse tree is nothing but the simple parse associated with the type attribute and each production rule.
  • The main objective of using the expression trees is to make complex expressions and can be easily be evaluated using these expression trees.
  • It is immutable, and once we have created an expression tree, we can not change it or modify it further.
  • To make more modifications, it is required to construct the new expression tree wholly.
  • It is also used to solve the postfix, prefix, and infix expression evaluation.

Expression trees play a very important role in representing the language-level code in the form of the data, which is mainly stored in the tree-like structure. It is also used in the memory representation of the lambda expression. Using the tree data structure, we can express the lambda expression more transparently and explicitly. It is first created to convert the code segment onto the data segment so that the expression can easily be evaluated.

The expression tree is a binary tree in which each external or leaf node corresponds to the operand and each internal or parent node corresponds to the operators so for example expression tree for 7 + ((1+8)*3) would be:

Expression tree in data structure

Let S be the expression tree

If S is not null, then

If S.value is an operand, then

Return S.value

x = solve(S.left)

y = solve(S.right)

Return calculate(x, y, S.value)

Here in the above example, the expression tree used context-free grammar.

We have some productions associated with some production rules in this grammar, mainly known as semantic rules. We can define the result-producing from the corresponding production rules using these semantic rules. Here we have used the value parameter, which will calculate the result and return it to the grammar’s start symbol. S.left will calculate the left child of the node, and similarly, the right child of the node can be calculated using the S.right parameter.

Use of Expression tree

  1. The main objective of using the expression trees is to make complex expressions and can be easily be evaluated using these expression trees.
  2. It is also used to find out the associativity of each operator in the expression.
  3. It is also used to solve the postfix, prefix, and infix expression evaluation.

Implementation of an Expression tree

To implement the expression tree and write its program, we will be required to use a stack data structure.

As we know that the stack is based on the last in first out LIFO principle, the data element pushed recently into the stack has been popped out whenever required. For its implementation, the main two operations of the stack, push and pop, are used. Using the push operation, we will push the data element into the stack, and by using the pop operation, we will remove the data element from the stack.

Main functions of the stack in the expression tree implementation:

First of all, we will do scanning of the given expression into left to the right manner, then one by one check the identified character,

  1. If a scanned character is an operand, we will apply the push operation and push it into the stack.
  2. If a scanned character is an operator, we will apply the pop operation into it to remove the two values from the stack to make them its child, and after then we will push back the current parent node into the stack.

Implementation of Expression tree in C Programming language

The output of the above program is:

X + Y * Z / W  

Implementation of Expression tree in C++ Programming language

The output of the above program is:

X + Y * Z / W  

Implementation of Expression tree in Java Programming language

The output of the above program is:

X + Y * Z / W  

You may also like