0

好的,我有一个读取方法可以正确读取值(全部 7000),(手写 15 个值作为树结构),不会产生任何错误。

但是,当涉及到二叉树的输出时,我使用的是几个网站上所述的方法。

我得到的错误是堆栈溢出,我假设它是由于递归调用并且从未爆发,但我不知道为什么这不起作用。

任何帮助表示赞赏,谢谢。

下面列出的代码:

// Read
void BinaryTreeStorage::read(ifstream& _fin)
{
        // Get first line with number of names in it
        string numberOfNamesString;
        getline(_fin, numberOfNamesString);

        // Loop through all the names
        string line;
        int num = 0;
        while (!_fin.eof())
        {
                getline(_fin, line);
                if (line != "")
                {
                        // Insert Value Here
                        if (root != NULL)
                        {
                                insert(line, root);
                        }
                        else
                        {
                                insert(line);
                        }
                }
        }
}

// Write
void BinaryTreeStorage::write(ofstream& _out) const
{
        inorderPrint(_out, root);
}

// inorderPrint
void BinaryTreeStorage::inorderPrint(ofstream& _out, node *_root) const
{
        if (_root != NULL)
        {
                // Inorder
                inorderPrint(_out, root->left);
                _out << root->nodeValue;
                cout << root->nodeValue << " ";
                inorderPrint(_out, root->right);
        }
}

// Insert if root is null
void BinaryTreeStorage::insert(string _nodeValueIn)
{
    if(root!=NULL)
        insert(_nodeValueIn, root);
    else
    {
        root=new node;
        root->nodeValue=_nodeValueIn;
        root->left=NULL;
        root->right=NULL;
    }
}

// Insert when root is not null
void BinaryTreeStorage::insert(string _nodeValueIn, node *leaf)
{
    if(_nodeValueIn< leaf->nodeValue)
    {
        if(leaf->left!=NULL)
            insert(_nodeValueIn, leaf->left);
        else
        {
            leaf->left=new node;
            leaf->left->nodeValue=_nodeValueIn;
            leaf->left->left=NULL;    //Sets the left child of the child node to null
            leaf->left->right=NULL;   //Sets the right child of the child node to null
        }  
  }
  else if(_nodeValueIn>=leaf->nodeValue)
  {
        if(leaf->right!=NULL)
            insert(_nodeValueIn, leaf->right);
        else
        {
            leaf->right=new node;
            leaf->right->nodeValue=_nodeValueIn;
            leaf->right->left=NULL;  //Sets the left child of the child node to null
            leaf->right->right=NULL; //Sets the right child of the child node to null
        }
    }
}
4

4 回答 4

2

您在 BinaryTreeStorage::inorderPrint 中有一个错误,您的参数_root没有在预期的地方使用:您总是在root上循环。

提示:避免使用相似的名称!

提示:避免使用 std以避免错误,除非您在嵌套模板中过于频繁地编写 std::。

提示:不要在名称的开头或结尾使用 _。

提示:不要与 NULL 比较:写 if(n) 而不是 if(n!=NULL)。

提示:不需要时不要嵌套块:

void BinaryTreeStorage::inorderPrint(std::ofstream& out, node *n) const
{
    if(!n) return;

    inorderPrint(out, n->left);
    out << n->nodeValue; // no separator??
    std::cout << n->nodeValue << " ";
    inorderPrint(out, n->right);
}
于 2012-04-30T21:26:11.457 回答
1
void BinaryTreeStorage::inorderPrint(ofstream& _out, node *_root) const
{
        if (_root != NULL)
        {
                // Inorder
                inorderPrint(_out, root->left);

在上面的代码中,我可以看到已_root定义,但您正在root调用中使用(上面的最后一行)。我认为这导致了无限循环。

于 2012-04-30T20:42:44.050 回答
0

当你构建你的树节点时,你是否确保左右指针被初始化为 NULL?

于 2012-04-30T20:18:29.223 回答
0

调用树inorderPrint的深度与树本身的深度相同。看起来您没有尝试保持树的平衡,因此深度可以与树的大小一样大。

有几种方法可以解决这个问题。您可以确保树始终保持平衡,以便深度与树的大小成对数增长。或者您可以使树threaded,这使您可以迭代地访问节点。

于 2012-04-30T20:50:24.233 回答