C ++:二叉树删除根节点

时间:2013-12-06 23:57:06

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

我有一个程序允许用户在二叉搜索树中添加和删除(以及其他内容)帐户。我已经测试/调试了这段代码并找到了导致问题的区域,我只是不太确定如何修复它。我可以添加帐户,我可以删除除根节点之外的所有帐户而不会出错。这是我的功能:

    if((current->llink==NULL && current->rlink != NULL) || (current->llink != NULL && current->rlink==NULL)){
        if(current->llink==NULL && current->rlink != NULL){
            if(trailcurrent->llink==current){
                trailcurrent->llink=current->rlink;
                delete current;
                current=NULL;
                cout << "Account #:" << acctNum << " has been removed from the list." << endl;
            }
            else{
                trailcurrent->rlink=current->rlink;
                delete current;
                current=NULL;
                cout << "Account #:" << acctNum << " has been removed from the list." << endl;
            }
        }
        else{
            if(trailcurrent->llink==current){
                trailcurrent->llink=current->llink;
                delete current;
                current=NULL;
                cout << "Account #:" << acctNum << " has been removed from the list." << endl;
            }
            else{
                trailcurrent->rlink=current->llink;
                delete current;
                current=NULL;
                cout << "Account #:" << acctNum << " has been removed from the list." << endl;
            }
        }
        return;
    }
    if(current->llink==NULL && current->rlink==NULL){
        if(trailcurrent->llink==current) // This 'if' statement crashes the program with a read violation when deleting the root.
            trailcurrent->llink=NULL;
        else
            trailcurrent->rlink=NULL;
        delete current;
        cout << "Account #:" << acctNum << " has been removed from the list." << endl;
        return;
    }
    if(current->llink != NULL && current->rlink != NULL){
        nodeType<accountType>* check=current->rlink;
        if((current->llink==NULL)&&(current->rlink==NULL)){
            current=check;
            delete check;
            current->rlink=NULL;
            cout << "Account #:" << acctNum << " has been removed from the list." << endl;
        }
        else{
            if((current->rlink)->llink!=NULL){
                nodeType<accountType>* leftCurrent;
                nodeType<accountType>* leftTrailCurrent;
                leftTrailCurrent=current->rlink;
                leftCurrent=(current->rlink)->llink;
                while(leftCurrent->llink != NULL){
                    leftTrailCurrent=leftCurrent;
                    leftCurrent=leftCurrent->llink;
                }
                current->info=leftCurrent->info;
                delete leftCurrent;
                leftTrailCurrent->llink=NULL;
                cout << "Account #:" << acctNum << " has been removed from the list." << endl;
            }
            else{
                nodeType<accountType>* temp=current->rlink;
                current->info=temp->info;
                current->rlink=temp->rlink;
                delete temp;
                cout << "Account #:" << acctNum << " has been removed from the list." << endl;
            }
        }
        return;
    }
}

对于导致我出问题的行,我是否必须在它之前有一个'if'语句,其中trailcurrent == NULL;?如果是这样,在'if'语句中我会做类似于

的事情
if(current->rlink>current->llink){}

0 个答案:

没有答案