计算表达式二叉树 C++

evaluate expression binary tree c++

我试图学习这个计算表达式的二叉树的实现。我无法 运行 它并看到输出。我如何得到 3*(7+1)/4+(17-5),结果是 18。 这里是linkhttp://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)

None 的 类 具有默认构造函数。
value returns表达式的求值结果,构造表达式时需要将必要的部分作为参数传递。
(目前尚不清楚如何将值 5 分配给 binary 表达式。)

您需要从叶子(将是常量)到根构建一棵树。
例如,表达式 5 + 3:

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

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

根据给定的 classes 的定义,首先您应该为表达式中的每个 number 创建一个 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:当 BinOpNode 的构造函数中需要 ExpNode 的对象时,我们可以传递 class ConstNode 的对象作为 ConstNode 继承自 ExpNode 抽象基础 class.

在 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 )

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

您有两个选择:要么将默认构造函数添加到 class:

BinOpNode() { ... }

或调用时使用参数 new:

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

祝你好运!