The program will take in a postfix expression and build an expression tree out o
ID: 3532582 • Letter: T
Question
The program will take in a postfix expression and build an expression tree out of it. It needs to use a stack class and an expression tree class. It also should print out the tree in prefix, infix, and postfix order and calculate the result of the expression.
only use + , -, * , and / operations (all require 2 operands).
Explanation / Answer
#include #include struct node{ char data; node *left; node *right; }; char postfix[35]; int top=-1; node *arr[35]; int r(char inputchar){ //for checking symbol is operand or operatorif(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/') return(-1); elseif(inputchar>='a' || inputchar='A' || inputchardata = symbol; newl->left = NULL; newl->right = NULL; push(newl); } else{ //If the symbol is operator//pop two top elements. ptr1=pop(); ptr2=pop(); newl = new node; newl->data = symbol; newl->left = ptr2; newl->right = ptr1; push(newl); } symbol=suffix[i]; } } void preOrder(node *tree){ if( tree!=NULL){ coutleft); preOrder(tree->right); } } void inOrder(node *tree){ if( tree!=NULL){ inOrder( tree->left); coutright); } } void postOrder(node *tree){ if( tree!=NULL){ postOrder( tree->left); postOrder( tree->right); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.