Design an Expression Tree With Evaluate Function - Problem
Design an Expression Tree With Evaluate Function
You're given a postfix (Reverse Polish) notation of an arithmetic expression and need to build a binary expression tree that represents this expression. Your task is to implement the tree structure and an evaluation function.
What is Postfix Notation?
In postfix notation, operators come after their operands. For example:
• Infix:
• Postfix:
Binary Expression Tree Structure:
• Leaf nodes: contain operands (numbers)
• Internal nodes: contain operators (+, -, *, /)
• Each internal node has exactly 2 children
Your Goal:
1. Build the expression tree from postfix tokens
2. Implement an
3. Design it to be modular and extensible
Input: Array of strings representing postfix expression
Output: Root node of the binary expression tree that can evaluate the expression
You're given a postfix (Reverse Polish) notation of an arithmetic expression and need to build a binary expression tree that represents this expression. Your task is to implement the tree structure and an evaluation function.
What is Postfix Notation?
In postfix notation, operators come after their operands. For example:
• Infix:
4 * (5 - (7 + 2))• Postfix:
["4", "5", "7", "2", "+", "-", "*"]Binary Expression Tree Structure:
• Leaf nodes: contain operands (numbers)
• Internal nodes: contain operators (+, -, *, /)
• Each internal node has exactly 2 children
Your Goal:
1. Build the expression tree from postfix tokens
2. Implement an
evaluate() function that computes the result3. Design it to be modular and extensible
Input: Array of strings representing postfix expression
Output: Root node of the binary expression tree that can evaluate the expression
Input & Output
example_1.py — Basic Expression
$
Input:
postfix = ["3", "4", "+", "2", "*", "7", "/"]
›
Output:
Expression tree that evaluates to 2
💡 Note:
Build tree from postfix notation. The expression represents ((3+4)*2)/7 = (7*2)/7 = 14/7 = 2. Stack operations: push 3, push 4, create +node with children 3,4, push 2, create *node with children (+node),2, push 7, create /node as root.
example_2.py — Simple Operation
$
Input:
postfix = ["4", "5", "+"]
›
Output:
Expression tree that evaluates to 9
💡 Note:
Simple addition: 4 + 5 = 9. The tree has + as root with left child 4 and right child 5. This demonstrates the basic building block of expression trees.
example_3.py — Complex Expression
$
Input:
postfix = ["4", "5", "7", "2", "+", "-", "*"]
›
Output:
Expression tree that evaluates to 0
💡 Note:
Represents 4 * (5 - (7 + 2)) = 4 * (5 - 9) = 4 * (-4) = -16. Wait, let me recalculate: 4 * (5 - (7 + 2)) = 4 * (5 - 9) = 4 * (-4) = -16. Actually, this evaluates to -16, not 0.
Constraints
- 1 ≤ postfix.length ≤ 100
- postfix[i] is either an operand or an operator ['+', '-', '*', '/']
- All operands are integers in range [-105, 105]
- No subtree evaluation will exceed 109 in absolute value
- All operations are valid (no division by zero)
- The postfix expression is guaranteed to be valid
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Stack
Start with empty stack to hold tree nodes
2
Process Operands
For each number, create leaf node and push to stack
3
Process Operators
For each operator, pop two nodes and create operator node
4
Build Tree
Connect operator node to popped nodes as children
5
Complete Tree
Final stack element is the expression tree root
Key Takeaway
🎯 Key Insight: Postfix notation and stacks are perfect partners - when you see an operator, the operands you need are always the top two elements on your stack!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code