Question
The goal of this lab is to reinforce binary tree concepts. Specifically, this lab is to do problem #1 on page 534 of the text(Data
The goal of this lab is to reinforce binary tree concepts. Specifically, this lab is to do problem #1 on page 534 of the text(Data Structures and Other Objects Using C++ (4th Edition) ). A partial solution is located here. You need to complete this solution.
expression_tree.cpp
#include "expression_tree.h" #include
//Precondition: op is either "+" or "-" //Postcondition: expression tree with root containing op & left & right //as subtrees has been created expression_tree::expression_tree (const std::string& op, const expression_tree& left, const expression_tree& right) { assert ((op == "+") || (op == "*")); node = new binary_tree_node
//Postcondition: return whether all characters is s are digits bool all_digits (const string& s) { size_t i = 0; while (i < s.length() && isdigit (s[i])) i++; return i == s.length(); }
//Precondition: value represents an integer //Postcondition: expression tree with value in root and no children has //has been created expression_tree::expression_tree (const std::string& value) { node = new binary_tree_node
expression_tree::~expression_tree();
expression_tree::expression_tree (const expression_tree& other);
expression_tree::expression_tree& operator = (const expression_tree& other);
//Postcondition: returns value of arithmetic expression represented by expression tree int expression_tree::evaluate() const { // to be implemented }
expression._tree.h
#ifndef _EXPRESSION_TREE_H_ #define _EXPRESSION_TREE_H_
#include "bintree.h" #include
using namespace main_savitch_10;
class expression_tree { public: expression_tree (const std::string& op, const expression_tree& left, const expression_tree& right); // precondition: op is either "+" or "-" // postcondition: expression tree with root containing op & left & right // as subtrees has been created expression_tree (const std::string& value); // precondition: value represents an integer // postcondition: expression tree with value in root and no children has // has been create
~expression_tree();
expression_tree (const expression_tree& other);
expression_tree& operator = (const expression_tree& other); int evaluate() const; // return: value of arithmetic expression represented by expression tree
private: binary_tree_node
#endif
lab10.cpp
#include
using namespace std;
expression_tree build_expression_tree(); // return: expression tree
int main () { expression_tree t = build_expression_tree(); int value = t.evaluate(); cout << "value: " << value << endl; return EXIT_SUCCESS; }
expression_tree build_expression_tree() { expression_tree t1 ("1"); expression_tree t2 ("2"); expression_tree t3 ("3"); expression_tree t4 ("4"); expression_tree t5 ("5"); expression_tree t6 ("*", t1, t2); expression_tree t7 ("+", t4, t5); expression_tree t8 ("*", t3, t7); expression_tree t9 ("+", t6, t8); return t9; } /* binary_tree_node
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started