5 Best Ways to Build and Evaluate Expression Trees Using Python

Rate this post

πŸ’‘ Problem Formulation: Expression trees are tree data structures where the internal nodes represent operators and the leaf nodes represent operands. Such trees can be used to analyze and evaluate mathematical expressions. For instance, given an infix expression like (3 + 2) * 4, we want to build its expression tree and then evaluate the result, which should output 20.

Method 1: Using Recursive Descent Parsing

Recursive descent parsing is a top-down approach to building and evaluating expression trees that uses a set of recursive functions to process the expression. It is particularly simple to implement for expressions containing binary operators.

Here’s an example:

class Node:
    def __init__(self, value):
        self.left = None
        self.right = None
        self.value = value

def parse_expression(expr):
    # Parse and build the tree (left out for brevity)

def evaluate(node):
    if isinstance(node.value, int):
        return node.value
    elif node.value == '+':
        return evaluate(node.left) + evaluate(node.right)
    elif node.value == '*':
        return evaluate(node.left) * evaluate(node.right)

# Building the expression tree for (3 + 2) * 4
root = parse_expression("(3 + 2) * 4")

The output of this code snippet:


This code defines a basic Node class for the tree’s node structure. The function parse_expression would be implemented to build the tree, which is then traversed by the evaluate function to compute the expression’s value.

Method 2: Using Stack Based Parsing

Stack based parsing creates an expression tree by pushing operands to a stack until an operator is encountered, at which point nodes are popped from the stack to become children of the operator node.

Here’s an example:

def build_tree(expression):
    stack = []
    for token in expression:
        if token in '+-*':
            right = stack.pop()
            left = stack.pop()
            stack.append(Node(token, left, right))
    return stack.pop()

# Assuming postfix notation for simplicity: "3 2 + 4 *"
root = build_tree("32+4*")

The output of this code snippet:


This snippet shows a simplified version of stack based parsing that works with postfix notation. Each operator pops the top two elements off the stack to become its children, and the result is pushed back onto the stack.

Method 3: Using Shunting Yard Algorithm

The Shunting Yard algorithm, developed by Edsger Dijkstra, is a method for parsing mathematical expressions specified in infix notation. It can handle operator precedence and associativity while outputting in postfix notation useful for tree construction.

Here’s an example:

def shunting_yard(expression):
    # Converts infix to postfix (left out for brevity)

root = build_tree(shunting_yard("3 + 2 * 4"))

The output of this code snippet:


As this example suggests, once you have the expression in postfix notation by using the Shunting Yard algorithm, it can be passed to the stack-based tree-building function from Method 2. Keep in mind that the tree evaluates to 11 because the algorithm correctly handles operator precedence, unlike the previous methods without explicit precedence consideration.

Method 4: Using Object-Oriented Programming

Using object-oriented programming, we can define a class for each type of operator. Each class knows how to evaluate itself, which simplifies the code and makes it highly extensible for additional operators.

Here’s an example:

class AddNode(Node):
# ...
class MultiplyNode(Node):
# ...

def build_eval_tree(expression):
    # OOP-based tree building

root = build_eval_tree("(3 + 2) * 4")

The output of this code snippet:


This method involves individual classes for different types of operations (addition, multiplication), which override a common evaluate method. The tree is built in an object-oriented fashion with these specialized nodes.

Bonus One-Liner Method 5: Leveraging External Libraries

Python has a rich ecosystem that includes libraries for virtually any task, including building and evaluating expression trees. One such library is SymPy which can build and evaluate expressions very effectively.

Here’s an example:

from sympy import sympify

# The sympify function parses an expression and automatically builds the tree
expr_tree = sympify("(3 + 2) * 4")

The output of this code snippet:


The SymPy library provides the sympify function that translates a string expression into a SymPy object that represents the expression tree. Calling evalf() on this object evaluates the expression.


  • Method 1: Recursive Descent Parsing. Simple, intuitive, but requires a good understanding of parsing theory. Not suitable for complex expressions with multiple levels of operator precedence.
  • Method 2: Stack Based Parsing. More concise and easier to handle postfix expressions. However, it assumes that the expression is already in postfix notation and doesn’t directly process infix expressions.
  • Method 3: Shunting Yard Algorithm. Robust, handles operator precedence and can easily be combined with stack-based parsing. Implementation is more complex than the two previous methods.
  • Method 4: Object-Oriented Programming. Highly extensible and clean design but may be overkill for simple expressions and introduces some overhead due to larger class structures.
  • Method 5: Leveraging External Libraries. Extremely easy to use with minimal code; however, it relies on external dependencies and may not suit environments where such libraries are not available.