From 0840f434541b57fbd6d3c9d7b2a8b127cc680912 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 1 Oct 2014 20:30:08 +0200 Subject: Moved child pointers to OperatorNode class * access was restricted by declaring them private and offering member methods such as "hasChildren" * only OperatorNode instances have children, there is no reason for wasting space by keeping empty pointers around in all other instances * this enabled the removal of all nullptr comparisons from tree construction * changed "Tree::addNode" factory method template to return a pointer to the type of the constructed node instance instead of a plain node pointer --- src/nodes.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/nodes.h') diff --git a/src/nodes.h b/src/nodes.h index 9559722..95bf772 100644 --- a/src/nodes.h +++ b/src/nodes.h @@ -13,11 +13,10 @@ class Node { public: virtual ~Node() {}; - virtual double solve() const = 0; - virtual std::string print() const = 0; - - Node* leftChild; - Node* rightChild; + virtual double solve() const = 0; + virtual std::string print() const = 0; + virtual bool hasChildren() const; + virtual bool isParentOf(Node*const) const; }; @@ -25,15 +24,21 @@ class OperatorNode: public Node { public: explicit OperatorNode(TokenType); - TokenType token() const; + virtual double solve() const; + virtual std::string print() const; + virtual bool hasChildren() const; + virtual bool isParentOf(Node*const) const; - virtual double solve() const; - virtual std::string print() const; + TokenType token() const; + void setChildren(Node*const, Node*const); private: const TokenType operator_; + Node* right_; + Node* left_; + }; class OperandNode: public Node { -- cgit v1.2.3