C++ 二叉树到数组:数组中插入了错误的值

C++ 二叉树到数组:数组中插入了错误的值,c++,binary-tree,C++,Binary Tree,我不知道我做错了什么。我有3个函数将两个二叉树中的数据存储到数组中。我的问题是:arr2的一切都很好,但arr1的一切都不好。有人知道如何解决这个问题吗?谢谢你的帮助 编辑:第一个数组看起来还包含来自arr2的值和一些随机数 第一个函数创建数组并调用treeToArray void merge(Node* n1, Node* n2){ int l1 = getTreeSize(n1); cout << "array size " << l1 <

我不知道我做错了什么。我有3个函数将两个二叉树中的数据存储到数组中。我的问题是:arr2的一切都很好,但arr1的一切都不好。有人知道如何解决这个问题吗?谢谢你的帮助

编辑:第一个数组看起来还包含来自arr2的值和一些随机数

第一个函数创建数组并调用treeToArray

void merge(Node* n1, Node* n2){
    int l1 = getTreeSize(n1);   
    cout << "array size " << l1 << endl;
    int *arr1 = new int[l1];   
    int i = 0;
    treeToArray(n1, arr1, i);  //This array is not filled how it's supposed to be. 

    int l2 = getTreeSize(n2);
    cout << "array size " << l2 << endl;
    int *arr2 = new int[l2]; //corrected this, thanks!
    int j = 0;
    treeToArray(n2, arr2, j);

    for(int i = 0; i < l1; ++i)
        cout << "array" << arr1[i] << " ";

    merge(arr1, arr2, l1, l2);

}

treeToArray函数按值获取整数索引,这意味着不同调用之间没有通信

我已经在第一次调用中用实际的index值对代码进行了注释,但是如果您想遵循递归,可以在调试器中逐步验证这一点

void treeToArray(Node* n, int values[], int index) {
     // start first call with index = 0
     if(n == NULL)
          return;
     if(n->left != NULL)
          treeToArray(n->left, values, index);
     // we passed 0 to the left subtree call, and get nothing back
     // so index is still 0 here

     values[index] = n->data;
     // we just overwrote the left subtree's first element with our data
     index++;

     if(n->right != NULL)
          treeToArray(n->right, values, index);
     // the right subtree now always starts from 1 ...
}
如果将其更改为通过引用传递索引,则调用可以配合:

void treeToArray(Node* n, int values[], int& index) {
     // start first call with index = 0
     if(n == NULL)
          return;
     if(n->left != NULL)
          treeToArray(n->left, values, index);
     // the left subtree call used a reference to the same
     // index variable, so any modification is visible here too

     values[index] = n->data;
     // we write our node data after the left subtree
     // (using the final value of index from the left subtree call)
     index++;
     // this affects the index in any parent call

     if(n->right != NULL)
          treeToArray(n->right, values, index);
     // the right subtree also advances the same index value
}
请注意,您可以返回新的索引,但这是对现有代码的较小更改



作为参考,可以很容易地使用一些小树单独对该函数进行单元测试,并检查预期的输出。这将在您引入第二棵树和所有其他机制之前暴露错误。

您确定要
int*arr2=new int[3]合并
功能中的code>?谢谢!!它必须是l2,即n2的大小。但是问题没有解决。什么是
n1
n2
?n1和n2是树的根节点。对不起。我的意思是
n1
n2
的值是多少?由于您对这两个参数执行了相同的操作,因此这两个参数之间肯定存在一些差异。在
treeToArray()
中,左侧节点的
索引将与当前节点相同。这可能就是原因,它起作用了!我非常感激!我花了好几个小时才把它修好。
void treeToArray(Node* n, int values[], int index) {
     // start first call with index = 0
     if(n == NULL)
          return;
     if(n->left != NULL)
          treeToArray(n->left, values, index);
     // we passed 0 to the left subtree call, and get nothing back
     // so index is still 0 here

     values[index] = n->data;
     // we just overwrote the left subtree's first element with our data
     index++;

     if(n->right != NULL)
          treeToArray(n->right, values, index);
     // the right subtree now always starts from 1 ...
}
void treeToArray(Node* n, int values[], int& index) {
     // start first call with index = 0
     if(n == NULL)
          return;
     if(n->left != NULL)
          treeToArray(n->left, values, index);
     // the left subtree call used a reference to the same
     // index variable, so any modification is visible here too

     values[index] = n->data;
     // we write our node data after the left subtree
     // (using the final value of index from the left subtree call)
     index++;
     // this affects the index in any parent call

     if(n->right != NULL)
          treeToArray(n->right, values, index);
     // the right subtree also advances the same index value
}