## Introduction to Expression Tree

The expression tree, a crucial concept in data structures, elegantly represents mathematical and logical expressions. It employs a tree-like model, where internal nodes represent operators and leaf nodes signify operands. This structure is pivotal for evaluating expressions efficiently and in order of priority: operators deeper in the tree have higher priority.

Notably, expression trees clarify operator associativity, such as the left-associativity of ‘+’ and right-associativity of ‘/.’ This resolution of ambiguities is essential in various applications, particularly in compiler design, where they form part of the semantic analysis phase. Here, syntax-directed translations yield annotated parse trees, integrating type attributes and production rules for accurate expression handling.

Furthermore, expression trees are immutable; once constructed, they cannot be altered, necessitating the creation of a new tree for any modification. This feature is instrumental in evaluating postfix, prefix, and infix expressions.

Beyond mere expression evaluation, expression trees offer a robust means to represent language-level code as data, essential in the memory representation of lambda expressions. Their binary tree structure, where each leaf corresponds to an operand and each internal node to an operator, streamlines the transformation of code segments into data segments for efficient processing.

**Example:**

The expression tree is a binary tree with the operators represented by the internal or parent nodes and the operands by the external or leaf nodes. For example, the expression tree for **3 + ((5+9)*2)** would be:

### Key Takeaways

- Expression trees organize mathematical expressions into hierarchical structures.
- They use nodes representing operators (+, -, *, /) and operands (numbers).
- They offer potent capabilities for evaluating, simplifying, and manipulating expressions.
- They can represent complex expressions, such as those involving recursion and higher-order functions.
- Expression trees are prevalent in computer science for parsing and evaluating mathematical expressions.

##### Table of Contents

### What is an Expression Tree?

An expression tree is a specialized tree-like data structure designed to represent and manipulate mathematical expressions. In this structure, each node embodies either an operand, such as a number or variable, or an operator, like addition, subtraction, multiplication, or division. Edges interconnect the operands and operators, delineating their hierarchical relationship.

Key aspects of expression trees include:

**Leaf Nodes**: The leaves of the expression tree represent operands. These operands can be constants, variables, or function names, depending on the complexity of the expression.**Internal Nodes**: Internal nodes signify operators. Each operator node typically has two children (in binary operations), reflecting the operands that the operator acts upon.**Hierarchical Structure**: The tree’s hierarchy intuitively captures the precedence of operations, ensuring that operations like multiplication and division (which generally have higher precedence) are evaluated before addition and subtraction.**Versatile Applications**: Expression trees are extensively used in various computational fields. In compilers, they play a crucial role in parsing and evaluating expressions in programming languages. Mathematical software and algorithms facilitate the analysis and simplification of complex mathematical expressions. Interpreters use them to execute code by traversing and evaluating the tree. Analytical engines, particularly in databases, leverage expression trees to process and optimize query expressions.**Traversal for Evaluation**: Traversing an expression tree (commonly in post-order) allows for systematically evaluating the expression. This traversal ensures that operands are combined in the correct order, respecting the rules of operator precedence and associativity.**Transformation and Optimization**: In advanced applications, expression trees can be transformed for optimization purposes, such as simplifying expressions, performing symbolic differentiation, or rewriting queries for efficient database retrieval.

### Construction of Expression Tree

- The postfix expression should be read from left to right.
- If the scanned character is an operand, create a new node with the operand as its value and push it onto the stack.
- If the scanned character is an operator:
- Pop two nodes from the stack.
- Create a new node with the operator as its value and the popped nodes as its left and right children (order matters for subtraction and division).
- Push the newly created node back onto the stack.

- Keep performing steps two and three until the entire expression is scanned.

Let’s use a postfix expression to comprehend the aforementioned process better. For the expression below, the expression tree’s implementation is explained.

**Example**

**d c + e***

**Step 1: **The first two symbols are operands, so we build a one-node tree and push a reference to the stack for them

**Step 2:** Make a new tree, pop two pointers to it, and push a pointer to it into the stack after reading a “+” sign.

**Step 3:** Following the reading of “e,” we build a single node tree and store a pointer in the stack.

**Step 4: **Lastly, we read the last symbol, “*,” pop two tree pointers and construct a new tree with d as the root. A pointer to the previous tree is still there on the stack.

### Traversal Techniques

Using these methods, a tree data structure’s nodes are visited in a predetermined order. Depending on the tree’s structure, they have different uses and produce different results.

**Inorder Traversal:**

- Visits the left subtree, the root node, and the right subtree.

**Algorithm:**

- Traverse the left subtree by recursively calling the inorder function on the left child.
- Visit the current node.
- Call the inorder function on the right child recursively to traverse the right subtree.

**Postorder Traversal:**

- Visits the left subtree, the right subtree, and the root node.

**Algorithm:**

- Traverse the left subtree by recursively calling the postorder function on the left child.
- Traverse the right subtree by recursively calling the postorder function on the right child.
- Visit the current node.

**Preorder Traversal:**

- Visits the root node, the left, and the right.

**Algorithm:**

- Visit the current node.
- Call the left subtree recursively to traverse its preorder function on the left child.
- Call the appropriate subtree recursively to traverse its preorder function on the right child.

**Example**

**In order Traversal:**

**D, B, E, A, F, C, G**

**Pre-order Traversal:**

**A, B, D, E, C, F, G**

**Post Order Traversal:**

**D, E, B, F, G, C, A **

### Implementation of an Expression tree

```
#include
#include
// Node structure for the expression tree
struct node {
char data;
struct node* left;
struct node* right;
};
// Function to create a new node for the expression tree
struct node* create_node(char data) {
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
// Function to construct the expression tree from postfix expression
struct node* construct_tree(char postfix[]) {
struct node* stack[100]; // Stack to hold nodes during construction
int top = -1;
int i = 0;
while (postfix[i] != '\0') {
char ch = postfix[i];
if (ch >= 'A' && ch <= 'Z') { struct node* new_node = create_node(ch); stack[++top] = new_node; } else { struct node* new_node = create_node(ch); new_node->right = stack[top--];
new_node->left = stack[top--];
stack[++top] = new_node;
}
i++;
}
return stack[top--];
}
// Function to perform inorder traversal of the expression tree
void inorder(struct node* root) {
if (root) {
inorder(root->left);
printf("%c ", root->data);
inorder(root->right);
}
}
int main() {
char postfix[] = "dc+e*"; // Postfix expression
struct node* root = construct_tree(postfix); // Constructing the expression tree
printf("Inorder traversal of expression tree:\n");
inorder(root); // Displaying the expression using inorder traversal
return 0;
}
```

**Output:**

```
#include
#include
using namespace std;
// Node structure for the expression tree
struct Node {
char data;
Node* left;
Node* right;
};
// Function to create a new node for the expression tree
Node* createNode(char data) {
Node* newNode = new Node();
newNode->data = data;
newNode->left = nullptr;
newNode->right = nullptr;
return newNode;
}
// Function to construct the expression tree from postfix expression
Node* constructTree(string postfix) {
stack<Node*> st; // Stack to hold nodes during construction
for (char ch : postfix) {
if (isalpha(ch)) {
Node* newNode = createNode(ch);
st.push(newNode);
} else {
Node* newNode = createNode(ch);
newNode->right = st.top(); st.pop();
newNode->left = st.top(); st.pop();
st.push(newNode);
}
}
return st.top();
}
// Function to perform inorder traversal of the expression tree
void inorder(Node* root) {
if (root) {
inorder(root->left);
cout << root->data << " "; inorder(root->right);
}
}
int main() {
string postfix = "dc+e*"; // Postfix expression
Node* root = constructTree(postfix); // Constructing the expression tree
cout << "Inorder traversal of expression tree:" << endl;
inorder(root); // Displaying the expression using inorder traversal
return 0;
}
```

**Output:**

```
import java.util.Stack;
class Node {
char data;
Node left, right;
public Node(char item) {
data = item;
left = right = null;
}
}
public class ExpressionTree {
public static Node createNode(char data) {
return new Node(data);
}
public static Node constructTree(String postfix) {
Stack stack = new Stack<>();
for (char ch : postfix.toCharArray()) {
if (Character.isLetter(ch)) {
Node newNode = createNode(ch);
stack.push(newNode);
} else {
Node newNode = createNode(ch);
newNode.right = stack.pop();
newNode.left = stack.pop();
stack.push(newNode);
}
}
return stack.pop();
}
public static void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
}
public static void main(String[] args) {
String postfix = "ABC*+D/"; // Postfix expression
Node root = constructTree(postfix); // Constructing the expression tree
System.out.println("Inorder traversal of expression tree:");
inorder(root); // Displaying the expression using inorder traversal
}
}
```

**Output:**

```
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def construct_tree(postfix):
stack = []
for ch in postfix:
if ch.isalpha():
new_node = Node(ch)
stack.append(new_node)
else:
new_node = Node(ch)
new_node.right = stack.pop()
new_node.left = stack.pop()
stack.append(new_node)
return stack.pop()
def inorder(root):
if root:
inorder(root.left)
print(root.data, end=' ')
inorder(root.right)
if __name__ == "__main__":
postfix = "dc+e*" # Postfix expression
root = construct_tree(postfix) # Constructing the expression tree
print("Inorder traversal of expression tree:")
inorder(root) # Displaying the expression using inorder traversal
```

**Output:**

```
using System;
using System.Collections.Generic;
public class Node
{
public char Data;
public Node Left, Right;
public Node(char data)
{
Data = data;
Left = Right = null;
}
}
public class ExpressionTree
{
public static Node CreateNode(char data)
{
return new Node(data);
}
public static Node ConstructTree(string postfix)
{
Stack stack = new Stack();
foreach (char ch in postfix)
{
if (char.IsLetter(ch))
{
Node newNode = CreateNode(ch);
stack.Push(newNode);
}
else
{
Node newNode = CreateNode(ch);
newNode.Right = stack.Pop();
newNode.Left = stack.Pop();
stack.Push(newNode);
}
}
return stack.Pop();
}
public static void Inorder(Node root)
{
if (root != null)
{
Inorder(root.Left);
Console.Write(root.Data + " ");
Inorder(root.Right);
}
}
public static void Main(string[] args)
{
string postfix = "dc+e*"; // Postfix expression
Node root = ConstructTree(postfix); // Constructing the expression tree
Console.WriteLine("Inorder traversal of expression tree:");
Inorder(root); // Displaying the expression using inorder traversal
}
}
```

**Output:**

```
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function constructTree(postfix) {
const stack = [];
for (let i = 0; i < postfix.length; i++) {
const ch = postfix[i];
if (/[A-Za-z]/.test(ch)) {
const newNode = new Node(ch);
stack.push(newNode);
} else {
const newNode = new Node(ch);
newNode.right = stack.pop();
newNode.left = stack.pop();
stack.push(newNode);
}
}
return stack.pop();
}
function inorder(root) {
if (root) {
inorder(root.left);
process.stdout.write(root.data + " ");
inorder(root.right);
}
}
const postfix = "dc+e*/"; // Postfix expression
const root = constructTree(postfix); // Constructing the expression tree
console.log("Inorder traversal of expression tree:");
inorder(root); // Displaying the expression using inorder traversal
```

**Output:**

### Applications of the Expression Tree

**Evaluation of Expression:**

- An effective method for evaluating complex expressions is through expression trees.
- The expression can be computed precisely by iteratively exploring the tree with the appropriate traversal techniques (usually postorder or inorder).
- Calculators, spreadsheet applications, programming language interpreters, and compilers use this extensively.

**Compiler Design:**

- Expression trees are essential for the design of compilers used in code creation and semantic analysis.
- They represent the structure of expressions in source code, enabling analysis of operator precedence, associativity, and type checking.
- They also make it easier to generate intermediate code and optimize code.

**Handling of Mathematical Expressions:**

- In many applications, expression trees make managing mathematical expressions easier.
- They help in equation solving, symbolic differentiation and integration, and expression simplification.
- Symbolic mathematical software and computer algebra systems (CAS) employ them.

**Interpreting Syntax in Programming Languages:**

- Expression trees are employed in programming language parsing and syntax analysis.
- They enable syntax verification and code comprehension by illustrating the structure of code expressions.
- Tools for code analysis, interpreters, and compilers need this.

**Processing Regular Expressions:**

- Regular expressions can be represented using expression trees, which makes text processing and pattern matching easier.
- They enable regular expression operations to be implemented effectively in programming languages, text editors, and search engines.

### Advantages of Expression Trees

**Compact Representation:**Expression trees provide a compact and structured way to represent mathematical expressions, making them easier to understand and manipulate.**Efficient Evaluation:**Once constructed, expression trees can be evaluated efficiently, especially in compilers and interpreters, making them valuable for performance-critical applications.**Simplification and Optimization:**Expression trees lend themselves well to simplification and optimization techniques, allowing for the transformation of complex expressions into more efficient forms.**Support for Symbolic Manipulation:**In symbolic mathematics and computer algebra systems, expression trees are essential for symbolically representing and manipulating algebraic expressions.**Parsing and Analysis:**Expression trees play a crucial role in parsing and analyzing mathematical expressions, aiding in developing mathematical software and tools.

### Real-time example:

Consider a finance application that evaluates investment strategies using mathematical formulas in a real-life scenario. Let’s use Python to demonstrate an expression tree for a compound interest formula: **A = P(1 + r/n)^(nt).** In this case, P is the initial amount, r is the annual interest rate, A is the total amount of money accumulated after n years, and **t** is the number of times interest is compounded per year.

Here’s an example using Python to create an expression tree for this formula:

```
import sympy as
from sympy.printing.dot import dotprint
from graphviz import Source
# Define variables
P = sp.Symbol('P')
r = sp.Symbol('r')
n = sp.Symbol('n')
t = sp.Symbol('t')
# Define the compound interest formula
formula = P * (1 + r/n)**(n*t)
# Get DOT representation
dot_representation = dotprint(formula)
# Replace string representations with symbolic representations
dot_representation = dot_representation.replace('Pow', '**')
dot_representation = dot_representation.replace('Mul', '*')
dot_representation = dot_representation.replace('Add', '+')
# Create a Source object and render the graph
graph = Source(dot_representation)
graph.render(filename='expression_tree', format='png', cleanup=True, view=True)
```

**Output:**

If this see in Google Collab, it means your code run successfully, and the mention image name it save on the file and looks like this:

**Code Explanation**

- Libraries
- A Python library for symbolic mathematics is called Sympy.
**printing.dot:**A SymPy module that helps with graph visualization by translating mathematical expressions into the DOT language.**Source:**A Graphviz library class used for DOT graph creation and rendering.

- To represent mathematical symbols, define symbolic variables (P, r, n, t).
- Using the symbolic variables, define a compound interest formula = P*(1+ r/n)**(n*t)
- To get the DOT representation of the mathematical expression, use dotprint.
- Substitute the symbolic equivalents of mathematical operations (‘**,’ ‘*,’ ‘+’) for their string representations (‘Pow,’ ‘Mul,’ ‘Add’).
- Make use of the DOT representation to create a Source object.
- Create a PNG file called “expression_tree.png” by rendering the graph.
- View the image in the Google Collab folder.

### Comparison with Other Data structures

Feature |
Expression Tree |
Array |
LinkedList |
Stack/Queue |
Binary Tree |

Structure |
Hierarchical | Linear | Linear | Linear | Hierarchical |

Element Order |
Precedence-based | Sequential | Sequential | LIFO/FIFO | Key-based |

Strengths |
Expression representation, evaluation, manipulation, optimization | Random access, sequential operations | Insertions, deletions | Ordering tasks | Searching, sorting |

Weaknesses |
Not ideal for random access or sequential operations | Limited for complex relationships | Traversal overhead | Limited representation capabilities | Precedence not inherent |

Applications |
Calculators, compilers, interpreters, databases, CAS, ML, regular expressions | Data storage, algorithms | Dynamic data structures | Task scheduling, expression parsing | Searching, sorting, tree traversal |

Advantages |
Visual representation, efficient evaluation, structure preservation, manipulation flexibility, optimization potential, symbolic reasoning | Fast random access, efficient sequential operations | Efficient insertions/deletions | Specific ordering capabilities | Efficient searching/sorting |

Disadvantages |
Overhead for simple expressions, potential for unbalanced trees | Limited expression representation | Traversal overhead for certain operations | Limited expression representation | Precedence not enforced |

### Complexity of Expression Tree

Section |
Complexity |
Explanation |

Construction |
Time: O(n) | Constructing the tree from an infix or postfix expression, where ‘n’ is the number of elements in the expression. |

Space: O(n) | Space complexity for storing nodes and intermediate data structures during construction. | |

Evaluation |
Time: O(n) | Evaluating an expression tree with ‘n’ nodes by traversing it once and performing operations at each node. |

Space: O(n) | Space complexity for evaluation due to recursive calls or stack space during tree traversal. | |

Traversal |
Time: O(n) | Traversing the tree, visiting each node once, with linear time complexity related to the number of nodes. |

Space: O(h) | Space complexity during traversal is O(h) due to the recursive function call stack, where ‘h’ is the tree height. | |

Storage |
Space: O(n) | Space is required to store the tree with ‘n’ nodes, proportional to the number of nodes in the expression tree. |

### Conclusion

Expression trees are a powerful and versatile tool for representing, evaluating, and manipulating expressions in a structured and efficient manner. Their visual clarity aids understanding and debugging, while flexibility allows them to handle various expression types. They find applications in diverse domains, from calculators and compilers to machine learning and artificial intelligence.

### Frequently Asked Questions (FAQs)

**Q1. What operations can expression trees represent?**

**Answer:-** Expression trees can represent various operations, including arithmetic (addition, subtraction, multiplication, division), logical (AND, OR, NOT), and other mathematical operations.

**Q2. Can an expression tree have multiple roots?**

**Answer:- **No, by definition, an expression tree is a binary tree with a single root node. It represents a single expression.

**Q3. Are there libraries or tools available to work with expression trees?**

**Answer:- **Yes, Numerous programming languages provide dedicated libraries and tools for handling expression trees. For instance, Python’s SymPy supports symbolic computation, C++ offers boost::expression for efficient tree handling, while Java has Jep for parsing and evaluating complex expressions. Similar functionalities exist in C# and MATLAB.

**Q4. Can an expression tree contain variables?**

**Answer:- **Yes, expression trees can represent expressions containing variables. The variables are considered as operands whose values can be substituted during evaluation.

### Recommended Articles

We hope that this EDUCBA information on “Expression Tree” was beneficial to you. You can view EDUCBA’s recommended articles for more information.