diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/tree.cc | 21 | ||||
-rw-r--r-- | src/tree.h | 15 |
2 files changed, 22 insertions, 14 deletions
diff --git a/src/tree.cc b/src/tree.cc index 77d9f4e..8ccb108 100644 --- a/src/tree.cc +++ b/src/tree.cc @@ -28,17 +28,23 @@ namespace { namespace SimpleParser { -Tree::Tree(std::string term, const ConstantMap* constants): +Tree::Tree( + const std::string& term, + const ConstantMap* constants +): term_(term), - constants_(constants) { - this->root_node_ = this->buildTree(term); -} + constants_(constants), + node_collection_(), + root_node_{ this->buildTree(term) } { } + +Tree::Tree(const std::string& term): + Tree(term, nullptr) { } -double Tree::solve() { +double Tree::solve() const { return this->root_node_->solve(); } -std::string Tree::print() { +std::string Tree::print() const { std::stringstream out; out.precision(std::numeric_limits<double>::digits10); @@ -60,7 +66,8 @@ std::string Tree::print() { << "\"];" << std::endl; - if ( node->type() == NodeType::OPERATOR ) { + if ( node->rightChild != nullptr && + node->leftChild != nullptr ) { size_t j{}; for ( auto&& child : this->node_collection_ ) { @@ -11,21 +11,22 @@ namespace SimpleParser { class Tree { public: - explicit Tree(std::string); - Tree(std::string, const ConstantMap*); + Tree(const std::string&, const ConstantMap*); + explicit Tree(const std::string&); - double solve(); - std::string print(); + double solve() const; + std::string print() const; private: template <class NType, typename... Args> Node* addNode(Node**, Args&&... args); Node* buildTree(std::string); - std::string term_; - Node* root_node_; - std::vector<std::unique_ptr<Node>> node_collection_; + const std::string term_; const ConstantMap* constants_; + + std::vector<std::unique_ptr<Node>> node_collection_; + Node* const root_node_; }; } |