递归函数错误:“-var-create:无法创建变量对象”

时间:2019-10-29 22:07:18

标签: c++ binary-search-tree vscode-debugger

我制作了一个二叉树来存储2个字符“ R”和“ B”。插入和打印功能可以正常工作,但是在执行搜索功能时,在VS代码的调试器工具下查看时,指针变量会给出错误-var-create: unable to create variable object

节点结构代码:

struct Node
{
    Node * left;
    Node * right;
    char value;
    bool endSeq = false;
};

BST的搜索功能:

bool search(string k)
        {
            int len = k.length();
            bool found = false;
            searchHelper(root, k, len, found);
            if(found)
            {
                return true;
            }
            return false;
        }

Node * searchHelper(Node * p, string k, int & len, bool & found)
        {
            if(p == NULL)
            {
                return NULL;
            }
            else if(len == 0)
            {
                if(p->endSeq)
                {
                    found = true;       
                }
                return p;
            }
            else if(k[0] < p->value || (k[0] == 'B' && k[0] == p->value))
            {
                string kNew = k.substr(1, --len);
                if(p->endSeq)
                {
                    found = true;
                }
                else
                {
                    insertHelper(p->left, kNew, len);
                }
            }
            else if(k[0] > p->value || (k[0] == 'R' && k[0] == p->value))
            {
                string kNew = k.substr(1, --len);
                if(p->endSeq)
                {
                    found = true;
                }
                else
                {
                    insertHelper(p->right, kNew, len);   
                }
            }
            return p;
        }

1 个答案:

答案 0 :(得分:1)

您的代码有问题。您进行了错误的递归调用。而不是调用insertHelper,您应该调用searchHelper