【问题标题】:Binary Tree only adding to the the root二叉树只添加到根
【发布时间】:2015-11-15 23:13:16
【问题描述】:

我正在用 C++ 编写一个简单的二叉树程序,现在它只存储在根节点输入的最新值,例如。如果我在树中输入 10,然后在树中输入 9,则 9 只是将 10 覆盖为根节点,因此树只存储值 9。

我在网上查看了多个 C++ 二叉树解决方案并尝试了他们的实现版本,但我仍然没有成功。

这是树中单个节点的结构

struct TreeNode{

    int value;
    TreeNode *left;
    TreeNode *right;

    TreeNode(int value){

        this -> value = value;
        left = NULL;
        right = NULL;

    }
};

到目前为止我的二叉树类

class IntTree{

private :

    TreeNode *root;

public :

    IntTree();
    TreeNode* getRoot();
    void insertValue(TreeNode *root, int intValue);
    TreeNode* searchTree(TreeNode *root, int intValue);
    void inOrder(TreeNode *root);
    void deleteValue(int intValue);
    void deleteTree(TreeNode *root);

};

插入方法

void IntTree::insertValue(TreeNode *root, int intValue){


if(root == NULL){

    root = new TreeNode(intValue);

}

else if(intValue == root->value){

    cout << "Value already exists in the tree" << endl;

}

else if(intValue < root->value){

    insertValue(root->left, intValue);

}

else{

    insertValue(root->right, intValue);

}   
}

然后在这样的菜单中简单地调用这个方法

cout << "Enter Value to Insert : " << endl;
input = readInt();
theTree.insertValue(theTree.getRoot(), input);

逻辑对我来说似乎都很好,除了我尝试不使用构造函数而只是单独设置变量,有两个函数用于插入一个只有 int 参数的函数,所以我不必使用getRoot() 以及其他一百万件我忘记的事情

【问题讨论】:

    标签: c++ tree binary-tree


    【解决方案1】:

    答案很简单,你正在修改的指针只是一个副本,所以这个副本在函数结束时被丢弃,你已经失去了内存。您需要对指针进行引用才能实际修改它(无需修改):

    void insertValue(TreeNode *& root, int intValue)
    

    【讨论】:

    • 我认为这与我仅修改副本有关,但我是指针新手,不知道该怎么做。对于它的作用仍然有点困惑,我知道使用 & 运算符将给出根指针的内存地址,但我不确定这会对我的程序有什么影响以及我必须如何更改我的其余部分程序来适应这个。如果我调用 insertValue 函数,我目前在行中遇到错误,因为我认为参数的类型不同。
    • 原理是,如果你想修改一个对象,你需要那个对象上的指针(或引用)。由于这是指向要修改的节点的指针,因此您需要该指针上的指针。使用引用具有相同的效果,但它只允许您更改函数签名(在您的 .h 和 .cpp 中),因此它更简单。在这种情况下 & 不是运算符,这意味着您需要引用,因此您无需更改任何其他内容(请参阅here)。有帮助吗?
    • 是的,这很有帮助,我明白你现在在做什么。但是,您说我只需要更改函数签名,但是这样做时仍然会出错。这些是错误,我得到了:no matching function for call to ‘IntTree::insertValue(TreeNode*, int&amp;)’ theTree.insertValue(theTree.getRoot(), input);no known conversion for argument 1 from ‘TreeNode*’ to ‘TreeNode*&amp;’
    • 这是我忘记的,因为你使用函数来获取你的根,你的函数还应该返回指针上的引用:TreeNode*&amp; getRoot();。否则,因为它写在错误消息中,它将采用临时指针(返回值)的地址,这是不可能的。
    • 非常感谢,您的回答成功了,并且您解释得很好,虽然我还必须在退货声明中添加,但它现在正在工作。
    【解决方案2】:

    这应该可行:

    新的 insertvalue 函数调用如下所示

    void insertValue(TreeNode **root, int intValue)
    {
      if(*root == NULL)
      {
          *root = newNode(intValue);
      }
      else if(intValue == (*root)->value)
      {
         cout << "Value already exists in the tree" << endl;
      }
      else if(intValue < (*root)->value)
      {
        insertValue(&(*(root))->left, intValue);
      }
      else
      {
        insertValue(&(*(root))->right, intValue);
      }   
    }
    int main()
    {
        //initial code
        insertvalue(&root,value) //root is a single pointer variable.
        //code for printing the tree
    }
    

    有许多不太复杂的方法来实现它。我刚刚修改了你的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      相关资源
      最近更新 更多