评估表达式二叉树c ++

时间:2015-12-01 20:54:51

标签: c++ binary-tree

我试图学习评估表达式的二叉树的这种实现。我无法运行它并看到输出。我怎么得到3 *(7 + 1)/ 4 +(17-5),结果是18。 这是链接http://math.hws.edu/eck/cs225/s03/binary_trees/

class ExpNode {
          // Represents a node of any type in an expression tree.
          // This is an "abstract" class, since it contains an undefined
          // function, value(), that must be defined in subclasses.
          // The word "virtual" says that the defintion can change
          // in a subclass.  The "= 0" says that this function has
          // no definition in this class.

     public:     

       virtual double value() = 0;  // Return the value of this node.

   }; // end class ExpNode


class ConstNode : public ExpNode {
          // Represents a node that holds a number.  (The
          // ": public ExpNode" says that this class is
          // a subclass of ExpNode.)

       double number;  // The number in the node.

     public:

       ConstNode( double val ) {
             // Constructor.  Create a node to hold val.
          number = val;
       }

       double value() {
             // The value is just the number that the node holds.
          return number;
       }

    }; // end class ConstNode


class BinOpNode : public ExpNode {
          // Represents a node that holds an operator.

       char op;        // The operator.
       ExpNode *left;   // The left operand.
       ExpNode *right;  // The right operand.

     public:

       BinOpNode( char op, ExpNode *left, ExpNode *right ) {
             // Constructor.  Create a node to hold the given data.
          this->op = op;
          this->left = left;
          this->right = right;
       }

       double value() {
             // To get the value, compute the value of the left and
             // right operands, and combine them with the operator.
           double leftVal = left->value();
           double rightVal = right->value();
           switch ( op ) {
               case '+':  return leftVal + rightVal;
               case '-':  return leftVal - rightVal;
               case '*':  return leftVal * rightVal;
               case '/':  return leftVal / rightVal;
            }
       }

    }; // end class BinOpNode

这是我尝试制作一个主要功能:

int main() {
    BinOpNode *opnode;
    opnode = new BinOpNode;
    opnode->value()=5;
    ExpNode *expnode;
    expnode = opnode;
    expnode->value();
    return 0;

}

它没有编译,这是错误

15:58:27 **** Incremental Build of configuration Debug for project ExpNode ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\ExpNode.o" "..\\src\\ExpNode.cpp" 
..\src\ExpNode.cpp: In function 'int main()':
..\src\ExpNode.cpp:60:15: error: no matching function for call to 'BinOpNode::BinOpNode()'
..\src\ExpNode.cpp:36:2: note: candidates are: BinOpNode::BinOpNode(char, ExpNode*, ExpNode*)
..\src\ExpNode.cpp:30:33: note:                 BinOpNode::BinOpNode(const BinOpNode&)
..\src\ExpNode.cpp:61:18: error: lvalue required as left operand of assignment

15:58:28 Build Finished (took 405ms)

3 个答案:

答案 0 :(得分:0)

没有类具有默认构造函数 value返回计算表达式的结果,并且需要在构造表达式时将表达式的必要部分作为参数传递。
(目前还不清楚您希望如何将值5分配给二进制表达式。)

你需要从叶子(它将是常量)构建一个树到根 例如,这里是表达式5 + 3

ConstNode five(5);
ConstNode three(3);
BinOpNode fiveplusthree('+', &five, &three);
std::cout << fiveplusthree.value(); // Should print 8

答案 1 :(得分:0)

我认为问题出在你的main()函数的逻辑中。

根据给定类的定义,首先应为表达式中的每个数字创建一个ConstNode类型的对象。然后,您应该为表达式中的每个运算符创建BinOpNode

顺便说一句,该表达式的评估结果为18,而不是82!

这样的事情:

//3*(7+1)/4+(17-5)  = 18
int main()
{
  BinOpNode *a, *b;
  a = new BinOpNode('+', new ConstNode(7), new ConstNode(1));
  a = new BinOpNode('*', new ConstNode(3), a);
  a = new BinOpNode('/', a, new ConstNode(4));
  b = new BinOpNode('-', new ConstNode(17), new ConstNode(5));
  b = new BinOpNode('+', a, b);
  cout << b->value();
}

PS:我们可以在ConstNode的构造函数中ExpNode的对象中传递BinOpNode的对象,因为ConstNode继承自ExpNode抽象基类。

答案 2 :(得分:0)

在C ++中,默认构造函数的工作很有趣。

如果您没有定义任何构造函数,则会为您生成默认构造函数:

class A {};

int main()
{
    A a; // perfectly fine

但是如果你定义任何其他构造函数,那些生成的构造函数就会消失:

class A
{
    A(int) { ... }
};

int main()
{
    A a; // ERROR!
}

在这种情况下,默认构造函数不存在,因为您定义了一个,并且编译器没有为您生成一个。

这是你的问题,因为在main,你有这一行:

opnode = new BinOpNode;

运行BinOpNode的默认构造函数。看看你的BinOpNode构造函数:

BinOpNode( char op, ExpNode *left, ExpNode *right )

嘿看:那不是默认的构造函数!

您有两种选择:在类中添加默认构造函数:

BinOpNode() { ... }

或在调用new时使用参数:

opnode = new BinOpNode(op, left, right);

祝你好运!