samedi 25 novembre 2017

What is causing a stack Overflow in my BST code?

my question is that I dont know exactly where I am going wrong, I believe it is the Recursive function. Getting another set of eyes can really help. I am lost. This is the error I get: Unhandled exception at 0x01159389 in BSTProgram: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00802FD4).

void Insert(const TK& newKey, const TD& newData)
{
    if (m_ptrRoot == nullptr)
    {
        m_ptrRoot = new Node<TK, TD>;
        m_ptrRoot->key = newKey;
        m_ptrRoot->data = newData;
        return;
    }
    else
    {
        RecursiveInsert(newKey, newData, m_ptrRoot);

    }
}

void RecursiveInsert(const TK& newKey, const TD& newData, Node<TK, TD>* ptrCurrent)
{
    if (ptrCurrent == nullptr)
    {
        ptrCurrent = new Node<TK, TD>;
        ptrCurrent->key = newKey;
        ptrCurrent->data = newData;
    }
    else if (ptrCurrent->ptrLeft == nullptr)
    {
        ptrCurrent->ptrLeft = new Node<TK, TD>;
        ptrCurrent->ptrLeft->key = newKey;
        ptrCurrent->ptrLeft->data = newData;
    }
    else if (ptrCurrent->ptrRight == nullptr)
    {
        ptrCurrent->ptrRight = new Node<TK, TD>;
        ptrCurrent->ptrRight->key = newKey;
        ptrCurrent->ptrRight->data = newData;
    }
    else
    {
        if (newKey < ptrCurrent->key)
        {
            RecursiveInsert(newKey, newData, ptrCurrent->ptrLeft);
        }
        if (newKey > ptrCurrent->key)
        {
            RecursiveInsert(newKey, newData, ptrCurrent->ptrRight);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire