Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Construct an Expression Tree for a given Prefix Expression
What is Expression Tree?
An expression tree is a binary tree used to represent expressions. In an expression tree, internal nodes correspond to operators, and each leaf node corresponds to an operand. Let's see an expression and construct a tree for [5 + ((4+3)*2)].
Constructing an Expression Tree
Our task is to construct an expression tree from a prefix expression. We have given a character array arr[] representing a prefix expression, so we have to build an expression tree for the expression and then display the infix and postfix expressions of the created tree.
Input/Output Scenario
Following are the examples to understand the problem statement:
Input: arr[] = "+ 5 * + 4 3 2" Output: The Infix expression is: 5 + ((4+3)*2) The Postfix expression is: 5 4 3 + 2 * + Input: arr[] = "+45" Output: The Infix expression is: 4+5 The Postfix expression is: 45+
C++ Program to Construct an Expression Tree for a given Prefix Expression
Here is a C++ program to construct an expression tree for a prefix Expression and then print the infix and postfix expression:
#include <iostream>
#include <cstring>
using namespace std;
// Node structure for the expression tree
struct Node {
char data;
Node * left, * right;
};
// Recursively builds the expression tree from prefix expression
char * build(Node ** root, char * expr) {
if ( * expr == '\0') return expr;
// Create new node
* root = new Node;
( * root) -> data = * expr;
( * root) -> left = ( * root) -> right = nullptr;
// If it's an operator, build left and right subtrees
if ( * expr == '+' || * expr == '-' || * expr == '*' || * expr == '/') {
expr = build( & (( * root) -> left), expr + 1);
expr = build( & (( * root) -> right), expr);
} else {
// It's an operand, move to next character
expr = expr + 1;
}
return expr;
}
// Inorder traversal to print infix expression
void printInfix(Node * root) {
if (!root) return;
bool isOperator = root -> data == '+' || root -> data == '-' || root -> data == '*' || root -> data == '/';
if (isOperator) cout << "( ";
printInfix(root -> left);
cout << root -> data << " ";
printInfix(root -> right);
if (isOperator) cout << ") ";
}
// Postorder traversal to print postfix expression
void printPostfix(Node * root) {
if (!root) return;
printPostfix(root -> left);
printPostfix(root -> right);
cout << root -> data << " ";
}
int main() {
Node * root = nullptr;
char expr[] = "+5*+432"; // Equivalent to: + 5 * + 4 3 2
build( & root, expr);
cout << "The Infix expression is:\n";
printInfix(root);
cout << "\n";
cout << "The Postfix expression is:\n";
printPostfix(root);
cout << "\n";
return 0;
}
The above code generates the following infix and postfix expressions of the prefix expression ?
The Infix expression is: ( 5 + ( ( 4 + 3 ) * 2 ) ) The Postfix expression is: 5 4 3 + 2 * +