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]
4 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]4 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
In ancient times what did kings and tribals chiefs use to communicate
mel-nik [20]

Answer:

they used sign language to communicate and also pegion was used to communicate

8 0
3 years ago
Characteristics of successful entrepreneurs include
crimeas [40]
The answer would be A because grit means to fight hard to get what you want. Submission would be to give in to pressure and give up. Self-importance would be to only focus on yourself and no one else. A short attention span would not work out for you because you get distracted easily.
8 0
3 years ago
The linux/unix command __________ can be used to search for files or contents of files.
Nana76 [90]
The `grep` command is used for this. https://man7.org/linux/man-pages/man1/grep.1.html
8 0
2 years ago
Although saying "Cylinder IS A Circle" doesn't make sense, it can be implemented in Java using inheritance in a legitimate way.
allochka39001 [22]

Answer:

I've included in the codes the area to circle and volume to cylinder. The Volume method that was used, area() to compute, the volume of cylinder which shows that cylinder is an expansion of circle with an extra dimension (height)

Explanation:

Temp is tester class

public class Temp {

   public static void main(String[] args) {

       Circle c = new Circle(7);

       System.out.println("Area of circle with radius 7 is "+c.area());

       Cylinder c1 = new Cylinder(7,10);

       System.out.println("Cylinder volume with radius 7 and height 10: "+c1.volume());

   }

}

----------------------------------------------------------------------------------------------------

class Circle{

   int radius;

   Circle(int r){

       radius=r;

   }

   double area(){  //area of circle

       return 3.14*radius*radius;

   }

   public int getRadius() {

       return radius;

   }

   public void setRadius(int radius) {

       this.radius = radius;

   }

}

class Cylinder extends Circle{

   int height;

   Cylinder(int r, int h){

       super(r);

       height=h;

   }

   double volume(){  //volume of cylinder

       return area()*height;

   }

   public int getHeight() {  //accessor

       return height;

   }

   public void setHeight(int height) {  //mutator

       this.height = height;

   }

}

8 0
4 years ago
Digital art is created by using
enyata [817]

Answer:

D. Computers

Explanation:

Digital art is when you make art on electronic devices.

8 0
3 years ago
Other questions:
  • Prodeff, an online project management software, is available at a price of $1 per use. The software requires the user to enter t
    13·1 answer
  • O novo funcionário da equipe de desenvolvimento de sistemas está aprendendo os termos mais utilizados no dia a dia da empresa. A
    7·1 answer
  • When the wires are connected to the terminals of the battery, what causes electric current in the circuit?
    15·1 answer
  • Create a SavingsAccount class. Use a static data member annualInterestRate to store the annual interest rate for each of the sav
    10·1 answer
  • Which best describes the benefits of renting a home
    12·2 answers
  • If a small monster collector:- Has 16 small monster containment devices and intends to use all of them.
    10·1 answer
  • PLEASE HELP!! WILL GIVE BRAINLIEST!!
    14·2 answers
  • In Python in order for a sort to work, which of the following is required?
    14·2 answers
  • Ang Kabihasnang ito ay umusbong sa rehiyon ng Timog Mexico
    6·1 answer
  • What are the different types of topology?​ in details
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!