## Expression tree in data structureThe 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 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:
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 ## Use of Expression tree- 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 also used to find out the associativity of each operator in the expression.
- It is also used to solve the postfix, prefix, and infix expression evaluation.
## Implementation of an Expression treeTo 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, - If a scanned character is an operand, we will apply the push operation and push it into the stack.
- 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
X + Y * Z / W ## Implementation of Expression tree in C++ Programming language
X + Y * Z / W ## Implementation of Expression tree in Java Programming language
X + Y * Z / W Next TopicGarbage Collection in Data Structure |