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
50 points please!!
MA_775_DIABLO [31]

Answer:

poopypoopypoopypoopy

6 0
3 years ago
A single set of hardware, software, databases, telecommunications, people, and procedures that are configured to collect, manipu
jok3333 [9.3K]

Answer:

"Computer Based Information system (CBIS)" is the correct answer for the above question.

Explanation:

  • CBIS means about that system that is used to produce the information or process the information and it is computer-based because CB stands for computer-based and IS stands for an information system.
  • It means that a system that helps by the computer and it is used to process the information then it can be called CBIS.
  • To process the information in a computer system there is a need for software, people, database, hardware and processor which is a set of CBIS.
  • The above question also wants to ask about the system which is a set of software, people, database, hardware and processor then it is known as CBIS which is described above. Hence the answer is CBIS.

4 0
3 years ago
In a print statement, you can set the __________ argument to a space or empty string to stop the output from advancing to a new
jeka94

In a print statement, you can set the End argument to a space or empty string to stop the output from advancing to a new line.

<h3>What is a Print statement?</h3>

A print "statement" is known to be a kind of statement that looks like a call to the make (print) or println process of the System.

Note that In a print statement, you can set the End argument to a space or empty string to stop the output from advancing to a new line so that it can work properly.

Learn more about  print statement from

brainly.com/question/25220385

#SJ1

6 0
2 years ago
PLEASE HELP!!! What are the benefits of online notebooks? Check all that apply.
Kipish [7]

They allow users to store files.

They allow users to share files.

They help users organize assignments.

They allow users to clip information from web pages.

4 0
2 years ago
Read 2 more answers
What should you do when an error message pops up on the screen?
Shtirlitz [24]
The best thing to do when an error message appears when it is not supposed to, as in if you are blocked by an administrator and it appears then its supposed to, it is best to contact someone who can investigate whether or not something is happening to your computer. That could be a parent, if they are good with computers, or just tech support at a store, provided they also know computers. Most of the time your computer just makes a mistake so nothing to worry about, but make sure to have it checked just to be sure. 
7 0
3 years ago
Other questions:
  • How might an engineer test a computer chair for comfort?
    10·1 answer
  • While performing an automatic transmission service at 60,000 miles, the technician notices a grayish sludge on the magnet in the
    15·1 answer
  • Use HTML to create a web page
    10·1 answer
  • In the 1880’s advancements in technology and processes made photography available to the general public. Who is considered the m
    12·1 answer
  • Bob has 2 candy bars he is fat what hapennes
    5·2 answers
  • Suppose Client A initiates a Telnet session with Server S. At about the same time, Client B also initiates a Telnet session with
    13·1 answer
  • Whose job is it to ensure that a group stays focused and on schedule?
    8·1 answer
  • Sdq1fdszgdSHTGafeges'
    15·1 answer
  • a client has requested adjustments to the arrangement and placement of elements on an image. what does the client want changed?
    9·1 answer
  • Explain the pros and cons of touchscreen, non-touchscreen, and hybrid devices:
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!