mardi 28 novembre 2017

Recursive function in program causing a stack overflow

My recursive function seems to be causing a stack overflow in my program. I do not know exactly where the error is because everything seems to be fine and initialized. I do not know where in my program I am going wrong. My question is that where is this error coming from and how do I fix it? Please if someone could help me out.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

template <typename TK, typename TD>
class Node      // done
{
public:
Node()
{
    ptrLeft = nullptr;
    ptrRight = nullptr;
}

~Node()
{
    if (ptrLeft != nullptr) { delete ptrLeft; }
    if (ptrRight != nullptr) { delete ptrRight; }
}

Node<TK, TD>* ptrLeft;

Node<TK, TD>* ptrRight;

//! The data stored by the node
TD data;

//! The key of this node
TK key;
};

template <typename TK, typename TD>
class BinarySearchTree
{
public:
BinarySearchTree()  // done
{
    m_ptrRoot = nullptr;
    m_nodeCount = 0;
}

~BinarySearchTree()     // done
{
    if (m_ptrRoot != nullptr) { delete m_ptrRoot; }
}

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;
        m_nodeCount++;
    }
    else
    {
        RecursiveInsert(newKey, newData, m_ptrRoot);
    }

}

string GetInOrder()     // done
{
    stringstream stream;
    GetInOrder(m_ptrRoot, stream);
    return stream.str();
}

string GetPreOrder()     // done
{
    stringstream stream;
    GetPreOrder(m_ptrRoot, stream);
    return stream.str();
}

string GetPostOrder()     // done
{
    stringstream stream;
    GetPostOrder(m_ptrRoot, stream);
    return stream.str();
}

private:

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;
        m_nodeCount++;
    }
    if (newKey < ptrCurrent->key)
    {
        if (ptrCurrent->ptrLeft == nullptr)
        {
            ptrCurrent->ptrLeft = new Node<TK, TD>;
            ptrCurrent->ptrLeft->key = newKey;
            ptrCurrent->ptrLeft->data = newData;
            m_nodeCount++;
        }
        else
        {
            RecursiveInsert(newKey, newData, ptrCurrent->ptrLeft);
        }
    }
    else if (newKey > ptrCurrent->key)
    {
        if (ptrCurrent->ptrRight == nullptr)
        {
            ptrCurrent->ptrRight = new Node<TK, TD>;
            ptrCurrent->ptrRight->key = newKey;
            ptrCurrent->ptrRight->data = newData;
            m_nodeCount++;
        }
        else
        {
            RecursiveInsert(newKey, newData, ptrCurrent->ptrRight);
        }
    }
}


void GetInOrder(Node<TK, TD>* ptrCurrent, stringstream& stream)
{
    if (ptrCurrent != nullptr)
    {
        GetInOrder(ptrCurrent->ptrLeft, stream);
        stream << ptrCurrent->key << " ";
        GetInOrder(ptrCurrent->ptrRight, stream);
    }
}

void GetPreOrder(Node<TK, TD>* ptrCurrent, stringstream& stream)
{
    if (ptrCurrent != nullptr)
    {
        stream << ptrCurrent->key << " ";
        GetPreOrder(ptrCurrent->ptrLeft, stream);
        GetPreOrder(ptrCurrent->ptrRight, stream);
    }
}

void GetPostOrder(Node<TK, TD>* ptrCurrent, stringstream& stream)
{
    if (ptrCurrent != nullptr)
    {
        GetPostOrder(ptrCurrent->ptrLeft, stream);
        GetPostOrder(ptrCurrent->ptrRight, stream);
        stream << ptrCurrent->key << " ";
    }
}


private:
Node<TK, TD>* m_ptrRoot;

int m_nodeCount;

friend class Tester;
};

#endif

Aucun commentaire:

Enregistrer un commentaire