Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given an expression tree, evaluate the expression and obtain a paranthesized for

ID: 3637210 • Letter: G

Question

Given an expression tree, evaluate the expression and obtain a paranthesized form of the expression.

Explanation / Answer

The code below prints the paranthesized form of a tree. infix_exp(p) { if(p) { printf("("); infix_exp(p->left); printf(p->data); infix_exp(p->right); printf(")"); } } Creating a binary tree for a postfix expression mynode *create_tree(char postfix[]) { mynode *temp, *st[100]; int i,k; char symbol; for(i=k=0; (symbol = postfix[i])!=''; i++) { temp = (mynode *) malloc(sizeof(struct node)); temp->value = symbol; temp->left = temp->right = NULL; if(isalnum(symbol)) st[k++] = temp; else { temp->right = st[--k]; temp->left = st[--k]; st[k++] = temp; } } return(st[--k]); } Evaluate a tree float eval(mynode *root) { float num; switch(root->value) { case '+' : return(eval(root->left) + eval(root->right)); break; case '-' : return(eval(root->left) - eval(root->right)); break; case '/' : return(eval(root->left) / eval(root->right)); break; case '*' : return(eval(root->left) * eval(root->right)); break; case '$' : return(eval(root->left) $ eval(root->right)); break; default : if(isalpha(root->value)) { printf("%c = ", root->value); scanf("%f", &num); return(num); } else { return(root->value - '0'); } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote