Question: I need to change the expression class tree constructor to expression_tree(istream&); I have the expression_tree.h file and the expressionTree_tester.cpp file. Could someone please change the

I need to change the expression class tree constructor to expression_tree(istream&);

I have the expression_tree.h file and the expressionTree_tester.cpp file. Could someone please change the parameterized constructor to the one above and make it run in the test file.

********expression_tree.h***************

#include

#include //for isdigit

#include

#include

#include

struct exp_node

{

int number;

char * op;

exp_node *left_field, *right_field;

};

exp_node *root, *temp;

int eval(exp_node*);

void display(exp_node*);

using namespace std;

class expression_tree

{

expression_tree()

{

root = NULL;

root->left_field = NULL;

root->right_field = NULL;

}

expression_tree(char str)

{

exp_node *nnode, *temp;

nnode = new exp_node;

nnode->left_field = NULL;

nnode->right_field = NULL;

if ((int)str>48 && (int)str <= 56)

{

nnode->number = (int)str - 48;

}

else

{

nnode->number = 0;

strcpy(nnode->op, str);

}

if (root == NULL)

{

root = nnode;

}

else

{

while (1)

{

if (temp->right_field == NULL)

{

temp->right_field = nnode;

if (nnode->number == 0)

{

temp = temp->right_field;

}

break;

}

else if (temp->left_field == NULL)

{

temp->left_field = nnode;

if (nnode->number == 0)

{

temp = temp->left_field;

}

break;

}

else

{

temp = root;

while (temp->left_field != NULL)

temp = temp->left_field;

temp->left_field = nnode;

if (nnode->number == 0)

{

temp = temp->left_field;

}

break;

}

}

}//else

}

void display(exp_node *temp)

{

if (temp != NULL)

{

display(temp->left_field);

if (temp->number == 0)

cout << endl << temp->op;

else

cout << endl << temp->number;

display(temp->right_field);

}

}

int evaluate(exp_node *temp)

{

int l = 0; int o1, o2, res = 0; char a;

if (temp != NULL)

{

o1 = evaluate(temp->left_field);

if (temp->number != 0)

return temp->number;

else

{

o2 = evaluate(temp->right_field);

if (temp->number != 0)

return temp->number;

else

{

if ((int)temp->op == 43)

res = o1 + o2;

else if ((int)temp->op == 42)

res = o1*o2;

else if ((int)temp->op == 45)

res = o1 - o2;

else if ((int)temp->op == 47)

res = o1 / o2;

return res;

}

}

}

}

public:

exp_node* get_root()

{

return root;

}

};

*************expressionTree_tester.cpp****************************

#include "expression_tree.h"

#include

#include

#include

#include

using namespace std;

// displays binary expression tree

void pretty_print(const exp_node* node_ptr, int depth = 5)

// Library facilities used: iomanip, iostream

{

cout << setw(4 * depth) << ""; // indentation

if (node_ptr == nullptr) {

// fallen off the tree

cout << "[Empty]" << std::endl;

}

else if (node_ptr->left_field == nullptr) {

// a leaf with numeric data

cout << node_ptr->number;

cout << " [leaf]" << std::endl;

}

else {

// a nonleaf with operator

cout << node_ptr->op << std::endl;

pretty_print(node_ptr->right_field, depth + 1);

pretty_print(node_ptr->left_field, depth + 1);

}

}

// main interactive testing function

int main() {

cout << "Enter the postfix expression: ";

while (cin && cin.peek() != ' ') {

// allocate memory

expression_tree *expTree = new expression_tree(cin);

// display binary tree

pretty_print(expTree->get_root());

cout << " \t==> evaluates to " << eval(expTree->get_root()) << endl;

// release memory

delete expTree;

// clear input stream

cin.ignore();

cout << "Enter another postfix expression or to end: ";

}

cout << "This concludes the Binary Tree Math Postfix Expression program! ";

return EXIT_SUCCESS;

}

********************************************************************************************

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!