1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
serious [3.7K]
3 years ago
15

Create a Binary Expressions Tree Class and create a menu driven programyour program should be able to read multiple expressions

from a file and create expression trees for each expression, one at a timethe expression in the file must be in "math" notation, for example x+y*a/b.display the preorder traversal of a binary tree as a sequence of strings each separated by a tabdisplay the postorder traversal of a binary tree in the same form as aboveWrite a function to display the inorder traversal of a binary tree and place a (before each subtree and a )after each subtree. Don’t display anything for an empty subtree. For example, the expression tree should would be represented as ( (x) + ( ( (y)*(a) )/(b) ) )
Computers and Technology
1 answer:
solong [7]3 years ago
3 0

Answer:

Explanation:

Program:

#include<iostream>

#include <bits/stdc++.h>

using namespace std;

//check for operator

bool isOperator(char c)

{

switch(c)

{

case '+': case '-': case '/': case '*': case '^':

return true;

}

return false;

}

//Converter class

class Converter

{

private:

string str;

public:

//constructor

Converter(string s):str(s){}

//convert from infix to postfix expression

string toPostFix(string str)

{

stack <char> as;

int i, pre1, pre2;

string result="";

as.push('(');

str = str + ")";

for (i = 0; i < str.size(); i++)

{

char ch = str[i];

if(ch==' ') continue;

if (ch == '(')

as.push(ch);

else if (ch == ')')

{

while (as.size() != 0 && as.top() != '('){

result = result + as.top() + " ";

as.pop();

}

as.pop();

}

else if(isOperator(ch))

{

while (as.size() != 0 && as.top() != '(')

{

pre1 = precedence(ch);

pre2 = precedence(as.top());

if (pre2 >= pre1){

result = result + as.top() + " ";

as.pop();

}

else break;

}

as.push(ch);

}

else

{

result = result + ch;

}

}

while(as.size() != 0 && as.top() != '(') {

result += as.top() + " ";

as.pop();

}

return result;

}

//return the precedence of an operator

int precedence(char ch)

{

int choice = 0;

switch (ch) {

case '+':

choice = 0;

break;

case '-':

choice = 0;

break;

case '*':

choice = 1;

break;

case '/':

choice = 1;

break;

case '^':

choice = 2;

default:

choice = -999;

}

return choice;

}

};

//Node class

class Node

{

public:

string element;

Node *leftChild;

Node *rightChild;

//constructors

Node (string s):element(s),leftChild(nullptr),rightChild(nullptr) {}

Node (string s, Node* l, Node* r):element(s),leftChild(l),rightChild(r) {}

};

//ExpressionTree class

class ExpressionTree

{

public:

//expression tree construction

Node* covert(string postfix)

{

stack <Node*> stk;

Node *t = nullptr;

for(int i=0; i<postfix.size(); i++)

{

if(postfix[i]==' ') continue;

string s(1, postfix[i]);

t = new Node(s);

if(!isOperator(postfix[i]))

{

stk.push(t);

}

else

{

Node *r = nullptr, *l = nullptr;

if(!stk.empty()){

r = stk.top();

stk.pop();

}

if(!stk.empty()){

l = stk.top();

stk.pop();

}

t->leftChild = l;

t->rightChild = r;

stk.push(t);

}

}

return stk.top();

}

//inorder traversal

void infix(Node *root)

{

if(root!=nullptr)

{

cout<< "(";

infix(root->leftChild);

cout<<root->element;

infix(root->rightChild);

cout<<")";

}

}

//postorder traversal

void postfix(Node *root)

{

if(root!=nullptr)

{

postfix(root->leftChild);

postfix(root->rightChild);

cout << root->element << " ";

}

}

//preorder traversal

void prefix(Node *root)

{

if(root!=nullptr)

{

cout<< root->element << " ";

prefix(root->leftChild);

prefix(root->rightChild);

}

}

};

//main method

int main()

{

string infix;

cout<<"Enter the expression: ";

cin >> infix;

Converter conv(infix);

string postfix = conv.toPostFix(infix);

cout<<"Postfix Expression: " << postfix<<endl;

if(postfix == "")

{

cout<<"Invalid expression";

return 1;

}

ExpressionTree etree;

Node *root = etree.covert(postfix);

cout<<"Infix: ";

etree.infix(root);

cout<<endl;

cout<<"Prefix: ";

etree.prefix(root);

cout<<endl;

cout<< "Postfix: ";

etree.postfix(root);

cout<<endl;

return 0;

}

You might be interested in
The approved detail design resulting from the __________ serves as a basis for making the decision to begin production.
natita [175]
I am pretty sure, that complete answer looks like this: The approved detail design resulting from the <span>Critical Design Review</span> serves as a basis for making the decision to begin production. Critical Design Review is needed to ensure that system can meet stated performance including costs and risks.
8 0
3 years ago
Advantages of using Unicode to represent data
Alona [7]
Global source and binary.
Support for mixed-script computing environments.
Improved cross-platform data interoperability through a common codeset.
Space-efficient encoding scheme for data storage.
Reduced time-to-market for localized products.
Expanded market access.
5 0
2 years ago
The amount of time taken by each of 10 students in a class to complete an exam is an example of what type of data? a. Cannot be
vekshin1

Answer:d) Quantitative data

Explanation: Quantitative data is the data that is required for the measurement of count and then exhibited in the numeric values.It is expressed through the type of name, number,code, symbol etc. It is used in the questionnaire, surveys, interviews etc.

Other options are incorrect because time series data is data according to the time event and categorical data is based on the categories of event.Thus, the correct option is option(d).

8 0
2 years ago
When mysql automatically converts one data type to another, itâs known as a/an ______________________ conversion?
lubasha [3.4K]
<span>MySQL is an open-source relational database management system (RDBMS)</span>
When MySQL automatically converts one data type to another, is known as an implicit conversion. Implicit conversion refers to the mixing and matching data types within the same operation. Example for implicit conversion is when MySQL automatically converts numbers to strings and vice versa. 
5 0
3 years ago
1.5 question 3 on edhesieve
morpeh [17]

Answer:

ppl

Explanation:

asfafgdffsghsdfghdgsghdfghtrbd54445 ddfgg

7 0
3 years ago
Other questions:
  • Torque is defined as _____.
    7·1 answer
  • What will allow you to purchase software without receiving full rights to the program?
    14·1 answer
  • A label is any word that appears in a cell of a spreadsheet.<br> True<br> False
    5·2 answers
  • The Springfork Amateur Golf Club has a tournament every weekend. The club president
    7·1 answer
  • A ____ object is used to hold data that is retrieved from a database via the OleDbDataAdapter connection. a. DataRecord b. DataS
    12·2 answers
  • What can you do to help create a safe online environment?
    8·2 answers
  • Free 35 points!!!
    11·2 answers
  • What does IDLE stand for
    11·2 answers
  • Help me please I’m failing
    11·1 answer
  • A Chain of dry-cleaning outlets wants to improve its operations by using data from devices at individual locations to make real-
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!