【问题标题】:C++ Eclipse: Binary Search Tree node->data = variable doesn't seem to workC++ Eclipse:二进制搜索树节点-> 数据 = 变量似乎不起作用
【发布时间】:2018-06-25 00:16:01
【问题描述】:

构建成功,但运行时停止

节点->数据=出价;

在下面的函数中。因此,输出成功打印到“Loading CSV file 888”cout 语句。我不确定是什么阻止了它运行该代码。

void BinarySearchTree::Insert(Bid bid) {
    // FIXME (2a) Implement inserting a bid into the tree
cout << "Loading CSV file 333" << endl;
Node* node = root;
cout << "Loading CSV file 888" << endl;
node->data = bid;
cout << "Loading CSV file 777" << endl;
if (root == NULL){
    //root->data = bid;
//root->left = 0;
//root->right = 0;
root = node;
node->left = NULL;
node->right = NULL;
cout << "Loading CSV file 444" << endl;
}
else
{
cout << "Loading CSV file 666" << endl;
Node* cur;
    cur = root;
    while (cur != NULL){
      if (strToDouble2(node->data.bidId) < strToDouble2(cur->data.bidId)){
         if (cur->left == NULL){
           cur->left = node;
           cur = NULL;
         }
         else{
           cur = cur->left;
         }
      }
      else
         if (cur->right == 0){
           cur->right = node;
           cur = NULL;
         }
         else{
           cur = cur->right;
         }
    node->left = NULL;
    node->right = NULL;
    cout << "Loading CSV file 5555" << endl;
    }
}
}

【问题讨论】:

    标签: c++ data-structures binary-tree binary-search-tree binary-search


    【解决方案1】:

    因为树是空的,所以root是空指针,node也是空指针,因为node = root;

    从存储要插入根节点的值开始是很奇怪的。
    如果要添加节点,代码应该在某处显示new Node,并且插入的值应该在该节点中。

    找出在哪里创建它作为练习。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 2020-03-23
      相关资源
      最近更新 更多