Advertisement
Singasking

Untitled

Nov 20th, 2022
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.73 KB | None | 0 0
  1. //Program to take an infix expression(ie. (x+y)*3^5) , to convert it into postfix and then to generate its
  2. //expression tree from postfix expression
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. stack<char> st;
  7.  
  8.  
  9. int getPriority(char x) {
  10.     if (x=='^') { return 3;}
  11.     if(x=='*' or x=='/') { return 2; }
  12.     if(x=='+' or x=='-') { return 1; }
  13.     return 0; //ie. its an operand and not operator
  14. }
  15. vector<char> postfix;
  16. class Node{
  17.     public:
  18.         char data;
  19.         Node* left=NULL;
  20.         Node* right = NULL;
  21.         Node* next=NULL;
  22.         Node(char value) {
  23.             data = value;
  24.             left=NULL;
  25.             right=NULL;
  26.             next=NULL;
  27.         }
  28. };
  29.  
  30.  
  31. class Stack{
  32.    
  33.     public:
  34.         Node* head=NULL;
  35.         void push(Node* node) {
  36.              if(head==NULL) { head = node; }
  37.             node->next = head;
  38.             head = node;
  39.         }
  40.         Node* top() {return head; }
  41.  
  42.         void pop() {
  43.             Node* temp = head;
  44.             head = head->next;
  45.            // delete temp;
  46.         }
  47. };
  48.  
  49. class ExpTree{
  50.     Stack st;
  51.    
  52.     public:
  53.      Node* root=NULL;
  54.         void createExpTree(vector<char> postfix) {
  55.             for(auto x:postfix) {
  56.                 Node* temp = new Node(x);
  57.                 cout<<"New node: "<<temp->data<<endl;
  58.                 if(getPriority(temp->data)==0) {
  59.                     cout<<"Pushing this node to stack"<<endl;
  60.                     st.push(temp);
  61.                 } else {
  62.                     Node* a = st.top();
  63.                     cout<<"Right child of this node: "<<a->data<<endl;
  64.                     st.pop();
  65.                     Node* b = st.top();
  66.                       cout<<"Left child of this node: "<<b->data<<endl;
  67.                     st.pop();
  68.                     temp->right = a;
  69.                     temp->left = b;
  70.                     cout<<"Now pushing to stack"<<endl;
  71.                     st.push(temp);
  72.                 }
  73.  
  74.             }
  75.             root = st.top();
  76.  
  77.             cout<<"root is "<<root->data<<endl;
  78.            cout<<"left child of root is: "<<root->left->data<<endl;
  79.             st.pop();
  80.         }
  81.         void inorder(Node* node) {
  82.          //   cout<<node->data<<endl;
  83.            //// cout<<node->right->data<<endl;
  84.            // return;
  85.            if(node==NULL) { return; }
  86.             inorder(node->left);
  87.             cout<<node->data<<"";
  88.             inorder(node->right);
  89.         }
  90.          void postorder(Node* node) {
  91.          //   cout<<node->data<<endl;
  92.            //// cout<<node->right->data<<endl;
  93.            // return;
  94.            if(node==NULL) { return; }
  95.             postorder(node->left);
  96.              postorder(node->right);
  97.             cout<<node->data<<"";
  98.            
  99.         }
  100.  
  101.         void preorder(Node *node) {
  102.             if(node==NULL) { return;}
  103.             cout<<node->data<<"";
  104.             preorder(node->left);
  105.             preorder(node->right);
  106.         }
  107.         void evaluatePostorder(string postfix) {
  108.             float answer;
  109.             stack<float> st;
  110.             for(auto x:postfix) {
  111.                 cout<<"Char is : "<<x<<endl;
  112.                 if(getPriority(x)==0) {
  113.                     float y= x-'0';
  114.                     st.push(y);
  115.                     cout<<"Pushed "<<y<<" into stack";
  116.                 } else  {
  117.                     float a= st.top();
  118.  
  119.                     st.pop();
  120.                     float  b = st.top();
  121.                     cout<<"a: "<<a<<" b: "<<b<<endl;
  122.                     st.pop();
  123.                     float ans=0;
  124.                     if(x=='+') { ans = a+b; }
  125.                     if(x=='-') {ans = b-a; }
  126.                     if(x=='*') { ans = a*b; }
  127.                     if(x=='/') { ans = b/a; }
  128.                     if(x=='^') { ans = pow(b,a); }
  129.                     cout<<"After performing required operation: "<<ans<<endl;
  130.                     st.push(ans);
  131.  
  132.                 }
  133.             }
  134.             cout<<"ans is: "<<st.top() <<endl;
  135.         }
  136. };
  137.  
  138.  
  139. /**
  140. LOGIC---
  141. 1) If char c is '('- push it in the stack
  142. 2) If char c is ')'- keep on popping elements and inserting them into the postfix
  143. expression till the time ( is encountered
  144. 3) If char c is of higher priority- just insert it in stack, otherwise pop and insert the elements into the postfix
  145. expression till the time stack.top() is of lower priority
  146. 4) Finally, pop and insert all the remaining elements of stack into the postfix expression
  147.  */
  148. int main() {
  149. string infix;
  150. cin>>infix;
  151. for(char x:infix) {
  152.     if(x=='(') {
  153.         st.push('(');
  154.     }
  155.     else if(x==')') {
  156.         while(st.top()!='(') {
  157.             postfix.push_back(st.top());
  158.             st.pop();
  159.         }
  160.         st.pop();
  161.     } else {
  162.         //Is either a operand or operator
  163.         //if operand just push it
  164.         if(getPriority(x)==0) {
  165.             postfix.push_back(x);
  166.          } else {
  167.             while(st.empty()==false && getPriority(st.top())>getPriority(x)) {
  168.                    //Till the time we have higher priority elements in stack, just pop them and add them to postfix vector
  169.                     postfix.push_back(st.top());
  170.                     st.pop();
  171.             }
  172.             st.push(x);
  173.          }
  174.  
  175.     }
  176. }
  177. //Now add all the remaining elements of stack into postfix expression
  178. while(st.empty()==false) { postfix.push_back(st.top()); st.pop(); }
  179.  
  180. //Now print the result
  181. string post="";
  182. for(auto x:postfix) { cout<<x<<"";post+=x; }
  183. //Now generate expression tree corresponding to it
  184.  
  185.  
  186. ExpTree tree;
  187.  
  188. tree.createExpTree(postfix);
  189. cout<<"Inorder: ";
  190. tree.inorder(tree.root);
  191. cout<<endl;
  192. cout<<"Postorder: ";
  193. tree.postorder(tree.root);
  194. cout<<endl;
  195. cout<<"Preorder: ";
  196. tree.preorder(tree.root);
  197. tree.evaluatePostorder(post);
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement