C++ Program to Create Expression Tree from Postfix Expression

This is a C++ Program to create an expression tree and print the various traversals using postfix expression.

Here is source code of the C++ Program to Construct an Expression Tree for a Postfix Expression. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8.         char data;
  9.         node *left;
  10.         node *right;
  11. };
  12.  
  13. char postfix[35];
  14. int top = -1;
  15. node *arr[35];
  16.  
  17. int r(char inputchar)
  18. { //for checking symbol is operand or operator
  19.     if (inputchar == '+' || inputchar == '-' || inputchar == '*' || inputchar
  20.             == '/')
  21.         return (-1);
  22.     else if (inputchar >= 'a' || inputchar <= 'z')
  23.         return (1);
  24.     else if (inputchar >= 'A' || inputchar <= 'Z')
  25.         return (1);
  26.     else
  27.         return (-99); //for error
  28. }
  29.  
  30. //it is used for inseting an single element in//a tree, i.e. is pushing of single element.
  31. void push(node *tree)
  32. {
  33.     top++;
  34.     arr[top] = tree;
  35. }
  36.  
  37. node *pop()
  38. {
  39.     top--;
  40.     return (arr[top + 1]);
  41. }
  42.  
  43. void create_expr_tree(char *suffix)
  44. {
  45.     char symbol;
  46.     node *newl, *ptr1, *ptr2;
  47.     int flag; //flag=-1 when operator and flag=1 when operand
  48.     symbol = suffix[0]; //Read the first symbol from the postfix expr.
  49.     for (int i = 1; symbol != NULL; i++)
  50.     { //continue till end of the expr.
  51.         flag = r(symbol); //check symbol is operand or operator.
  52.         if (flag == 1)//if symbol is operand.
  53.         {
  54.             newl = new node;
  55.             newl->data = symbol;
  56.             newl->left = NULL;
  57.             newl->right = NULL;
  58.             push(newl);
  59.         }
  60.         else
  61.         { //If the symbol is operator//pop two top elements.
  62.             ptr1 = pop();
  63.             ptr2 = pop();
  64.             newl = new node;
  65.             newl->data = symbol;
  66.             newl->left = ptr2;
  67.             newl->right = ptr1;
  68.             push(newl);
  69.         }
  70.         symbol = suffix[i];
  71.     }
  72. }
  73.  
  74. void preOrder(node *tree)
  75. {
  76.     if (tree != NULL)
  77.     {
  78.         cout << tree->data;
  79.         preOrder(tree->left);
  80.         preOrder(tree->right);
  81.     }
  82. }
  83.  
  84. void inOrder(node *tree)
  85. {
  86.     if (tree != NULL)
  87.     {
  88.         inOrder(tree->left);
  89.         cout << tree->data;
  90.         inOrder(tree->right);
  91.     }
  92. }
  93.  
  94. void postOrder(node *tree)
  95. {
  96.     if (tree != NULL)
  97.     {
  98.         postOrder(tree->left);
  99.         postOrder(tree->right);
  100.         cout << tree->data;
  101.     }
  102. }
  103.  
  104. int main(int argc, char **argv)
  105. {
  106.     cout << "Enter Postfix Expression : ";
  107.     cin >> postfix;
  108.  
  109.     //Creation of Expression Tree
  110.     create_expr_tree(postfix);
  111.  
  112.     //Traversal Operation on expression tree
  113.     cout << "\nIn-Order Traversal :   ";
  114.     inOrder(arr[0]);
  115.  
  116.     cout << "\nPre-Order Traversal :  ";
  117.     preOrder(arr[0]);
  118.  
  119.     cout << "\nPost-Order Traversal : ";
  120.     postOrder(arr[0]);
  121.     return 0;
  122. }

Output:

$ g++ ExpressionTreePostfix.cpp
$ a.out
 
Enter Postfix Expression : 32+5*
 
In-Order Traversal :   3+2*5
Pre-Order Traversal :  *+325
Post-Order Traversal : 32+5*
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.