Моя функция удаления в BST даже не работает

0

Мне нужно создать функцию удаления, поэтому я просматриваю сеть, но я не могу ее исправить в своей программе. // в defs.h

struct notesTree 
{
    int nProdID;
    int nQuan; 
    int balance;            //will be used in AVL only, and be ignored in other cases.
    notesTree* pLeftChild;
    notesTree* pRightChild;
};

//в storebin.cpp

void RemoveNode(notesTree* n, int item)
{
    // Find the item 
    bool found = false;
    notesTree* predecessor=NULL;
    notesTree* current=n;
    if(current==NULL) return;
    while(current!=NULL)
    {
        if(current->nProdID==item)
        {
            predecessor = current;
            found = true;
            break;
        }
        else
        {
            predecessor = current;
            if(item > (current->nProdID))
                current=current->pRightChild;
            else
                current=current->pLeftChild;
        }
    }


    if(!found)
    {
        return;
    }
    // CASE 1: Removing a node with a single child
    if((current->pLeftChild==NULL && current->pRightChild != NULL) || (current->pLeftChild != NULL && current->pRightChild==NULL))
    {
        // Right Leaf Present, No Left Leaf
        if(current->pLeftChild==NULL && current->pRightChild != NULL)
        {
            // If predecessor left tree equals Node n
            if(predecessor->pLeftChild==current)
            {
                // then predecessor left tree becomes n right tree
                // and delete n
                predecessor->pLeftChild=current->pRightChild;
                delete current;
                current=NULL;
                cout<<item<<" has been removed from the Tree."<<endl;
            }
            // If predecessor right tree equals Node n
            else
            {
                // then predecessor right tree becomes n right tree
                // and delete n
                predecessor->pRightChild=current->pRightChild;
                delete current;
                current=NULL;
                cout<<item<<" has been removed from the Tree."<<endl;
            }
        }
        else // Left Leaf Present, No Right Leaf Present
        {
            if(predecessor->pLeftChild==current)
            {
                predecessor->pLeftChild=current->pLeftChild;
                delete current;
                current=NULL;
                cout<<item<<" has been removed from the Tree."<<endl;
            }
            else
            {
                predecessor->pRightChild=current->pLeftChild;
                delete current;
                current=NULL;
                cout<<item<<" has been removed from the Tree."<<endl;
            }
        }
        return;
    }
    // CASE 2: Removing a Leaf Node
    if(current->pLeftChild==NULL && current->pRightChild==NULL)
    {
        if(predecessor->pLeftChild==current)
            predecessor->pLeftChild=NULL;
        else
            predecessor->pRightChild=NULL;
        delete current;
        cout<<item<<" has been removed from the Tree."<<endl;
        return;
    }
    // CASE 3: Node has two children
    // Replace Node with smallest value in right subtree
    if(current->pLeftChild != NULL && current->pRightChild != NULL)
    {
        notesTree* check=current->pRightChild;
        if((current->pLeftChild==NULL)&&(current->pRightChild==NULL))
        {
            current=check;
            delete check;
            current->pRightChild==NULL;
            cout<<item<<" has been removed from the Tree."<<endl;
        }
        else // Right child has children
        {
            // If the node right child has a left child
            // Move all the way down left to locate smallest element
            if((current->pRightChild)->pLeftChild!=NULL)
            {
                notesTree* leftCurrent;
                notesTree* leftCurrentPred;
                leftCurrentPred=current->pRightChild;
                leftCurrent=(current->pRightChild)->pLeftChild;
                while(leftCurrent->pLeftChild != NULL)
                {
                    leftCurrentPred=leftCurrent;
                    leftCurrent=leftCurrent->pLeftChild;
                }
                current->nProdID=leftCurrent->nProdID;
                delete leftCurrent;
                leftCurrentPred->pLeftChild==NULL;
                cout<<item<<" has been removed from the Tree."<<endl;
            }
            else
            {
                notesTree* temp=current->pRightChild;
                current->nProdID=temp->nProdID;
                current->pRightChild=temp->pRightChild;
                delete temp;
                cout<<item<<" has been removed from the Tree."<<endl;
            }
        }
        return;
    }
}

Я называю это

void doReserveEvent(notesTree* &root, int eventCode)
{
    int tmpMSP = (eventCode % 10000)/10;
    int tmpSoLuong = eventCode%10;
    notesTree*pt;
    pt=TimMSP(root,tmpMSP);
    if(pt!=NULL)
    {
        pt->nQuan-=tmpSoLuong;
        if(pt->nQuan<=0)
        {
            RemoveNode(root,tmpMSP);
            return;
        }
    }

    //o
}

баланс =???..}. Неправильно ли оно в строке if (current == NULL) return;

  • 0
    Для чего это стоит, вы можете сделать это примерно в 25 строках кода, включая пробелы и комментарии. Вы делаете это очень тяжело для себя. Смотрите это в прямом эфире .
Теги:
binary-search-tree

1 ответ

1

Мне кажется, что одна проблема здесь

while(current!=NULL)
{
    if(current->nProdID==item)
    {
        predecessor = current; // <--- remove this line
        found = true;
        break;

То, как вы написали его предшественник, всегда равно текущему, когда вы находите предмет. Я полагаю, что предшественник должен равняться предыдущему значению тока.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню