将重复键值插入二叉树

时间:2014-05-20 19:59:47

标签: c++ binary-search-tree

我试图为我的二叉搜索树类编写插入方法。我希望它能够将新值插入已经具有相同数据值的现有节点的树中,这必须导致在现有节点的右子树中创建新节点。这是我的代码。我在尝试编译时遇到未处理的异常错误。我不知道我在那里做错了什么。如果有人能解释编译器给我错误的原因,那就太好了。谢谢:))

template <class elemType>
void bSearchTreeType<elemType>::insert
                 (const elemType& insertItem)
{
    nodeType<elemType> *current; //pointer to traverse the tree
    nodeType<elemType> *trailCurrent; //pointer behind current
    nodeType<elemType> *newNode;  //pointer to create the node

    newNode = new nodeType<elemType>;
    newNode->info = insertItem;
    newNode->lLink = NULL;
    newNode->rLink = NULL;

    if (root == NULL)
        root = newNode;
    else
    {
        current = root;

        while (current != NULL)
        {
            trailCurrent = current;

            if (current->info == insertItem)
            {
                current = current->rLink;
                trailCurrent->rLink = newNode;
                if (newNode->info <= current->info) //this is where the compiler say the error is
                    newNode->rLink = current;
                else
                    newNode->lLink = current;
            }
            else if (current->info > insertItem)
                current = current->lLink;
            else
                current = current->rLink;
        }//end while

        if (trailCurrent->info < insertItem)
            trailCurrent->rLink = newNode;
        else if (trailCurrent->info > insertItem)
            trailCurrent->lLink = newNode;
    }
}//end insert

3 个答案:

答案 0 :(得分:1)

你应该和自己达成协议。这笔交易可以是这样的:

左子树的值小于或等于比根中的值小,等等。

在代码中你有:

if (trailCurrent->info < insertItem)
  trailCurrent->rLink = newNode;
交易之后会是这样的:

if (trailCurrent->info <= insertItem)
  trailCurrent->rLink = newNode;

答案 1 :(得分:0)

不确定这是否正确,但

trialCurrent = current;

trialCurrent等于当前

if(current->info == insertItem)
    current = current->rLink;
    trialCurrent->rlink = newnode;

当前等于current-&gt; rLink,那么trialCurrent-&gt; rLink是否等于当前?

答案 2 :(得分:0)

我很确定这是你要做的事情:

template <class elemType>
void bSearchTreeType<elemType>::insert(const elemType& insertItem)
{
    nodeType<elemType> **pp = &root;
    while (*pp)
    {
        if (insertItem < (*pp)->info)
            pp = &(*pp)->lLink;
        else if ((*pp)->info < insertItem)
            pp = &(*pp)->rLink);
        else break;
    }

    // note: this is cleaner if the the nodeType constructor
    //  is parameterized. (hint hint)
    nodeType<elemType> *p = new nodType<elemType>;
    p->info = insertItem;
    p->lLink = NULL;

    if (*pp)
    {
        p->rLink = (*pp)->rLink;
        (*pp)->rLink = p;
    }
    else
        *pp = p;
}

工作原理

为新的不存在的密钥找到插入点的基础是平凡的,所以我不会覆盖它们。然而,定位现有密钥的情况很有趣。我们使用指向指针来保存我们在走树时遇到的每个节点指针的地址。当我们匹配时,循环将退出,*pp为空。当发生这种情况时,我们的新节点将其右指针设置为匹配的右指针,匹配节点的右指针成为新节点。树的其余部分保持原样。

即。在此添加第二个7:

        5
   3        7
2     4  6     8

结果

        5
   3        7
2     4  6     7
                 8

然后添加另外3个结果:

         5
   3           7
2     3     6     7
        4           8

所有这一切,当然,假设我理解了这个问题。没有尝试旋转或平衡。我留给你了。