Question: Add both preorder and post-order functions to Chp4_Postfix_Infix.cpp then test new functions C++ code is below: #include using namespace std; struct n { char d;

Add both preorder and post-order functions to Chp4_Postfix_Infix.cpp then test new functions

C++ code is below:

#include 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 if (inputch >= '0' || inputch <= '9') 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 inOrder(n *tree) { if (tree != NULL) { inOrder(tree->l); cout << tree->d; inOrder(tree->r); } } int main(int argc, char **argv) { cout << "Enter Postfix Expression like 762*+6+ : "; cin >> pf; construct_expression_tree(pf); cout << " In-Order Traversal : "; inOrder(a[0]); cout << endl; return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!