
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Construct an Expression Tree for a Postfix Expression
An expression tree is basically a binary tree which is used to represent expressions. In expression tree, nodes correspond to the operator and each leaf node corresponds to the operand. This is a C++ program to construct an expression tree for a postfix Expression in inorder, preorder and postorder traversals.
Algorithm
Begin Function r() has a character variable as parameter. If the characters are + or - or * or / then Return will be -1 If the characters are from A to Z then Return will be 1. If the characters are from a to z then Return will be 1. Else Return -100. Function construct_expression_tree() to construct the expression tree Function push() to push values in the stack Function pop() to pop values from the stack Function preOrder() for pre-order traversal Function inOrder() for in-order traversal Function postOrder() for post-order traversal End.
Example Code
#include <iostream> using namespace std; struct n { char d; n *l; n *r; }; char pf[50]; int top = -1; n *a[50]; int r(char inputch) { if (inputch == '+' || inputch == '-' || inputch == '*' || inputch== '/') return (-1); else if (inputch >= 'A' || inputch <= 'Z') return (1); else if (inputch >= 'a' || inputch <= 'z') return (1); else return (-100); } void push(n *tree) { top++; a[top] = tree; } n *pop() { top--; return (a[top + 1]); } void construct_expression_tree(char *suffix) { char s; n *newl, *p1, *p2; int flag; s = suffix[0]; for (int i = 1; s != 0; i++) { flag = r(s); if (flag == 1) { newl = new n; newl->d = s; newl->l = NULL; newl->r = NULL; push(newl); } else { p1 = pop(); p2 = pop(); newl = new n; newl->d = s; newl->l = p2; newl->r = p1; push(newl); } s = suffix[i]; } } void preOrder(n *tree) { if (tree != NULL) { cout << tree->d; preOrder(tree->l); preOrder(tree->r); } } void inOrder(n *tree) { if (tree != NULL) { inOrder(tree->l); cout << tree->d; inOrder(tree->r); } } void postOrder(n *tree) { if (tree != NULL) { postOrder(tree->l); postOrder(tree->r); cout << tree->d; } } int main(int argc, char **argv) { cout << "Enter Postfix Expression : "; cin >> pf; construct_expression_tree(pf); cout << "\nIn-Order Traversal : "; inOrder(a[0]); cout << "\nPre-Order Traversal : "; preOrder(a[0]); cout << "\nPost-Order Traversal : "; postOrder(a[0]); return 0; }
Output
Enter Postfix Expression : 762*+6+ In-Order Traversal : 7+6*2+6 Pre-Order Traversal : ++7*626 Post-Order Traversal : 762*+6+
- Related Articles
- C++ Program to Construct an Expression Tree for a given Prefix Expression
- Python Program to Construct an Expression Tree of a given Expression
- Algorithm to construct an Expression Tree in Data Structure
- Evaluate Postfix Expression
- Convert Infix to Postfix Expression
- C Program to construct DFA for Regular Expression (a+aa*b)*
- Program to build and evaluate an expression tree using Python
- C++ Program to Implement Expression Tree Algorithm
- Construct the Regular expression for the given languages.
- Regular Expression "A" construct in Java
- Construct NFA with Epsilon moves for regular expression a+ba*.
- What is an expression tree in DBMS?
- Regular Expression "[^...]" construct in Java
- Construct a Finite Automata for the regular expression ((a+b)(a+b))*.
- Regular Expression "d" construct in Java

Advertisements