让我的BST连接递归函数有些麻烦

时间:2019-04-21 06:14:07

标签: c++

我需要制作一个BST,以加载有关英语和莫尔斯电码转换的信息。这个insertnode函数的想法是递归地降低BST(左侧小于ASCII,右侧大于),找到英文字母的正确位置。我做得很好,顺着树下走,找到了正确的位置,并且在main的范围内正确创建了节点,但是无论出于什么原因,分配后左和右继续为nullptr。

我尝试使用引用更改指针的数量,但似乎无法找出问题所在。

int main(void)
{
ifstream morse; // The file with the translations
ifstream translate; // The file with what needs to be translated
morse.open("morse.txt");
translate.open("convert.txt");

// 1st order of business is to fill the tree from "morse" file, but first I need to declare our tree with the first char.
char letter;
string sound;

if (morse)
{
    morse.get(letter);
    getline(morse, sound); // will be \n
    getline(morse, sound);

    tree<char, string> morsetree(letter, sound); // now we have our tree with a root

    while (morse)
    {
        morse.get(letter);                // grab the english letter
        getline(morse, sound);            // grab the \n
        getline(morse, sound);            // grab the morse translation

        morsetree.insertnode(letter, sound, morsetree.getroot());
    }

    // table is now collected.
    morsetree.printtree(morsetree.getroot());

}

return 0;
}

template<class english, class morse>
treenode<english, morse> *makenode(english newletter, morse newsound) 
//out here for scope
{
    return new treenode<english, morse>(newletter, newsound);
}

上面是main和newnode函数的上下文。以下是给我带来麻烦的功能。

template<class english, class morse>
void tree<english, morse>::insertnode(english newletter, morse newsound, 
treenode<english, morse> *current) //START WITH ROOT
{
    //also need to think about the fact that english has upcase/lowcase while morse doesn't. Maybe implement that when searching the tree

/*
How this works: I need to make sure that the node goes into the exact place that it is supposed to be. my morse.txt is decently randomized so I shouldn't need
to worry about balancing it. all nodes will be inserted at the bottom of the tree, nothing in the middle. If the letter is less then go to the left, bigger go to the right.
Keep going until it finds a nullptr in which case the newletter has found it's position. This is done recursively.
*/

if (current == nullptr)
{
    current = makenode(newletter, newsound);
}

if (newletter < current->getletter())
{
    return insertnode(newletter, newsound, current->getleft());
}

if (newletter > current->getletter())
{
    return insertnode(newletter, newsound, current->getright());
}

//if none of these the letters already been put into the data. This shouldn't happen though unless I messed up morse.txt
}

下面是getleft()中的代码

template<class english, class morse>
treenode<english, morse> *treenode<english, morse>::getleft()
{
    return left;
}

我还将包括完整的treenode类以获取更多上下文。

template<class english, class morse> //these are the two template 
//variables being used, one for the english char and 1 for translated morse string.
class treenode
{
public:
treenode(english, morse);

english getletter();
morse getsound();
treenode *getleft();  //returns a pointer to the address of left
treenode *getright();

void setletter(english newletter);
void setsound(morse newsound);
void setleft(treenode *newleft);
void setright(treenode *newright);


private:
english letter;
morse sound;
treenode<english, morse> *left;
treenode<english, morse> *right;
};

0 个答案:

没有答案