Project #211276 - Advanced c++

Computer Tutors

Subject Computer
Due By (Pacific Time) 11/11/2017 06:00 pm

a.) Create the expression_tree class (expressionTree.h) according to the textbook requirements using a binary tree approach. Include a private data member and public accessor method (get_root) for the root pointer. The expression_tree class should hava default constructor to create an empty binary tree and a parmeterized constructor to create the binary expression tree form an input stream containing a validpostfix expression: expression_tree(istream&);

**********Textbook requirements*************

This project deals with a simple kind of
expression tree, where there are two kinds of

(a) Leaf nodes, which contain a real number as
their entry;

(b) Nonleaf nodes, which contain either the character + or the
character * as their entry, and have exactly two children.

For this project, implement a class for expression
trees, including operations for building expression
trees. Also include a recursive function to “evaluate”
a non-empty expression tree using these rules:

(a) If the tree has only one node (which must be
a leaf), then the evaluation of the tree returns the real
number that is the node’s entry;

(b) If the tree has more than one node, and the
root contains +, then first evaluate the left subtree,
then evaluate the right subtree, and add the results.
If the root contains *, then evaluate the two subtrees
and multiply the results.

For example, consider the small expression
tree shown to the right. The left subtree evaluates
to 3+7, which is 10. The right subtree evaluates to
14. So the entire tree evaluates to 10*14,
which is 140.

***********End of Textbook Requirements*************

b) Each node of the binary expression_tree can contain either an integral numberic or character value in addition to the left and right subtree pointers. Use the following structure definition to represent this node:

struct exp_node {

int number;

char op;

exp_node* left_field;

exp_node* right_field;


c) Remember to properly allocate and  release memory as needed to populate the nodes of hte binary expression_tree. Thus, an expression_tree destructor should release all memory that has been allocated.

d) Include a recursive function to evaluate and return the value of a binary expression_tree as described in the textbook.

e) Use expressionTree_test.cpp file to test our expression_tree


#include "expression_tree.h"
#include <cstdlib>
#include <string>
#include <iostream>
#include <iomanip>
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() != '\n') {
                // allocate memory
                expression_tree *expTree = new expression_tree(cin);
                // display binary tree
                cout << "\n\t==> evaluates to " << evaluate(expTree->get_root()) << endl;
                // release memory
                delete expTree;
                // clear input stream
                cout << "Enter another postfix expression or <Enter> to end: ";
        cout << "This concludes the Binary Tree Math Postfix Expression program!\n\n";
        return EXIT_SUCCESS;


out of 1971 reviews

out of 766 reviews

out of 1164 reviews

out of 721 reviews

out of 1600 reviews

out of 770 reviews

out of 766 reviews

out of 680 reviews