
- 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 Implement Expression Tree Algorithm
An expression tree is basically a binary which is used to represent expressions. In expression tree, internal nodes correspond to operators and each leaf node corresponds to an operand. Here is a C++ Program to implement the Expression Tree Algorithm which takes the postfix expression as an input and generates the corresponding expression tree traversed in inorder.
Algorithm
Begin function construct_expression_tree(): Flag = 1 when it is operand. Flag = -1 when it is operator. S = suffix[0] means read the first operand from the expression. For i = 0 and until s != 0 Check symbol is operand or operator. Call function void inorder() for inorder traversal. Print the results. Increment i End.
Example Code
#include <iostream> using namespace std; struct n//node declaration { char d; n *l; n *r; }; char pf[50]; int top = -1; n *a[50]; int r(char inputch)//check the symbol whether it is an operator or an operand. { 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)//push elements in stack { 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 inOrder(n *tree)//perform inorder traversal { if (tree != NULL) { inOrder(tree->l); cout << tree->d; inOrder(tree->r); } } int main(int argc, char **argv) { cout << "Enter Postfix Expression : "; cin >>pf; construct_expression_tree(pf); cout << "\nInfix Expression : "; inOrder(a[0]); return 0; }
Output
Enter Postfix Expression : 762*+6+ Infix Expression : 7+6*2+6
- Related Articles
- Algorithm to construct an Expression Tree in Data Structure
- C++ Program to Implement Kadane’s Algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement the RSA Algorithm
- C++ Program to Implement Coppersmith Freivald’s Algorithm
- C++ Program to Implement Modular Exponentiation Algorithm
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Extended Euclidean Algorithm
- C program to implement Euclid’ s algorithm
- C++ Program to Implement Ternary Tree
- C++ Program to Implement AVL Tree
- C++ Program to Implement B+ Tree
- C++ Program to Implement Cartesian Tree
- C++ Program to Implement Fusion Tree

Advertisements