为什么我的递归不会返回到前一个指针?

时间:2018-03-26 12:42:03

标签: c++ binary-search-tree

我正在进行一项任务,我们必须从二叉搜索树中创建一个20个问题类型的游戏。我们从文本文件中读取树,格式如下:

Does it walk on 4 legs?
Does it fly?
*centipede?
Is it an insect?
*bird?
*butterfly?
Does it purr?
Does it howl?
*mouse?
*dog?
*cat?

稍后,我将允许用户添加到此列表。但是,目前我无法准确地将列表读入二叉搜索树。我已经设置好了(我认为)它将使用递归并返回到前一个"当前"结束函数循环时的节点指针。但是,当前节点指针保持不变。

以下函数从文本文件中传递字符串的向量。

string line;
string guess;
bool start = true;

void buildTree(vector<string> gameData, Node* current, int &counter)
{
    //fill node with question or answer
    //recursive:
    //  add to the left until we encounter an asterisk
    //  add to the right
    line = gameData[counter];

    //if a question
    if (line[0] != '*')
    {
        if (current->getData().empty())
        {
            current->setData(line);
            cout << current->getData() << endl;
        }
        if (!start)
        {
            //if noChild is empty AND current isn't a guess, go to noChild
            if ((current->getNo()->getData().empty()) 
                && (current->isGuess() == false))
            {
                current = current->getNo();
            }
            //otherwise, go to yes
            else {
                current = current->getYes();
            }
        }
        while (counter < gameData.size())
        {
            if (!start) { counter++; }
            start = false;
            buildTree(gameData, current, counter);
        }
    }
    //if a guess
    else
    {
        //if data is full, go to no
        if (current->getData().empty() == false)
        {
            current = current->getNo();
        }
        //otherwise, go to yes
        else 
        {
            //current = current->getYes();
            for (int i = 1; i < line.size(); i++)
            {
                guess.push_back(line[i]);
            }
            current->setData(guess);
            guess.clear();
            cout << current->getData() << endl;
            counter++;
            current->setGuess(true);
        }
    }
}

0 个答案:

没有答案