c ++二叉树地址边界错误SIGSEGV

时间:2017-06-23 00:29:46

标签: c++ binary-search-tree sigsegv

我试图将节点列表组织到我当前项目的二叉树中。编写代码就像在书中一样,但当流程在while循环中即将初始化当前指针与右或左假时,我得到地址错误。这是为什么?

这里是insert()fn:

template <class elemType>
void bSearchTree<elemType>::insert(const elemType& insertItem){

    binaryTreeNode<elemType> *current;
    binaryTreeNode<elemType> *trailCurrent;
    binaryTreeNode<elemType> *newNode;

    newNode = new binaryTreeNode<elemType>;
    assert(newNode != NULL);
    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){

                cout << "The insert item is already in the list-";
                cout << "duplicates are not allowed." << insertItem << endl;
                return;
            }else if(current->info > insertItem)
                current = current->lLink;
            else
                current = current->rLink;
        }

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

    }

    if(current->info != insertItem){

            totalNodes++;
        }
}

0 个答案:

没有答案