Question

C++ Programming Implement a class ExprTree for representing expression trees, including: 1. The operations to build...

C++ Programming

Implement a class ExprTree for representing expression trees, including:

1. The operations to build an expression tree from a postfix arithmetic expression.
2. A recursive function to "evaluate" a non-empty expression tree using these rules:
   - If the tree has only one node (which must be a leaf), then the evaluation of the tree returns the real number (type double) that is the node's entry.
- If the tree has more tha one node, and the root contains an "operator" (+, -, %, /), then first evaluate the left subtree, then the right subtree, and apply the "operator" on these two values to get the result.

Your main function should perform the following tasks:
   1. Prompt for input of a postfix expression. Print the expression.
2. Parse the postfix expression and build the expression tree (Hint: using a Stack to store the operations while building the expression tree).
3. Evaluate the expression tree. Print the result.
   4. Repeat steps 1-3 for three postfix expressions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include<bits/stdc++.h>
using namespace std;

struct Node
{
char data;
Node* l, *r;
};

bool isOperatorOrNot(char c)
{
if (c == '+' || c == '-' ||
c == '*' || c == '/' ||
c == '^')
return true;
return false;
}

void inorderTraversal(Node *t)
{
if(t)
{
inorderTraversal(t->l);
cout<<t->data<<" " ;
inorderTraversal(t->r);
}
}

Node* createNewNode(int v)
{
Node *temp = new Node;
temp->l = temp->r = NULL;
temp->data = v;
return temp;
};

Node* createTree(char postfixExpre[])
{
stack<Node *> st;
Node *t, *t1, *t2;


for (int i=0; i<strlen(postfixExpre); i++)
{
  
if (!isOperatorOrNot(postfixExpre[i]))
{
t = createNewNode(postfixExpre[i]);
st.push(t);
}
else
{
t = createNewNode(postfixExpre[i]);

  
t1 = st.top();
st.pop();
t2 = st.top();
st.pop();

  
t->r = t1;
t->l = t2;


st.push(t);
}
}

  
t = st.top();
st.pop();

return t;
}

int main()
{

int i=0;
char postfixExpre1[100];
  


while(i<3)
{
printf("\nEnter Postfix Expression\n");
cin>>postfixExpre1;
cout<<"Expression is "<<postfixExpre1<<endl;
Node* r = createTree(postfixExpre1);
cout<<"infix expression is \n";
inorderTraversal(r);
i++;
}
  
  
  
  
  
return 0;
}

==================================================

akshay@akshay-Inspiron-3537:~/Chegg$ g++ exp.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out

Enter Postfix Expression
ab+
Expression is ab+
infix expression is
a + b
Enter Postfix Expression
bc+
Expression is bc+
infix expression is
b + c
Enter Postfix Expression
cd*
Expression is cd*
infix expression is
c * d

Add a comment
Know the answer?
Add Answer to:
C++ Programming Implement a class ExprTree for representing expression trees, including: 1. The operations to build...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming in Java Q: Build an expression tree for a given postfix expression. Then compute the ...

    Programming in Java Q: Build an expression tree for a given postfix expression. Then compute the value of the expression by doing an in-order traversal of the expression tree. For example: Your output should print the following: o The input is: "Expression" o The inorder traversal is: "inorder traversal" o The output of the expression is: "output" Please print the output in the same format. (example shown below) The input is: 1 68+ The inorder traversal is: 168 The output...

  • Why is there return in evalExpr function? What would be the base case? Prove that the...

    Why is there return in evalExpr function? What would be the base case? Prove that the algorithms printExpression and evalExpr print out and evaluate an arithmetic expression represented by a binary tree. Hint: Use induction and the recursive definition of a binary tree. Evaluate Arithmetic Expressions o Specialization of a postorderAlgorithm evalExpr(v) traversal if v.isExternalO a recursive method returning return v.elementO the value of a subtree else when visiting an internal node, combine the values of the subtrees x ←...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • You are to write a program name expressionTree.java that evaluates an infix expression entered by the...

    You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2)   One alphabetic character - "x" (representing a value to be supplied later). (3)   Binary operators (+, -, *, / and % (modulo)). (4)   Parentheses          You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

  • implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of...

    implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of single digit operands; the operators +, -, *, and / ; and parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces. Design and implement a class of infix calculators. Use the algorithms given in the chapter 6 to evaluate infix expressions as entered into the calculator. You must first convert the infix expression to postfix form and then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT