二叉搜索树 - 复制每个项目

时间:2012-12-15 18:19:53

标签: c++ tree binary-tree binary-search-tree heap-corruption

我正在尝试复制给定的BST

让我说我得到了这个:

   2
  / \
 1   3

我想要做的是:

      2
     / \
    2   3
   /   /
  1   3
 /
1
void bTree::mulTree(tNode *tree)
{
    if (!tree)
        return;
    if (tree->left)
        tree->left->left = insert(tree->left->value);
    else
        tree->left = insert(tree->value);

    mulTree(tree->left->left);

    if (tree->value != tree->left->value)
    {
        tree->left->value = tree->value;
        mulTree(tree->right);
    }
}

使用这段代码我得左侧就像它应该是
但如果我试图在右侧进行更改,我会收到堆错误

"//mulTree(tree->right);"  

如果我跑这个我得到:
"HEAP: Free Heap block ... after it was freed"
但我不明白它已被释放的位置...... = \

我在此期间收到错误:

tree->left = insert(tree->value);

插入代码:

tNode* bTree::insert(int value)
{
    tNode *temp = new tNode();
    temp->value = value;
    return temp;
}

0 个答案:

没有答案