二进制树按顺序​​遍历导致堆栈溢出

时间:2012-04-30 20:14:45

标签: c++ tree binary-tree stack-overflow

好的,我有一个读取方法正确读取值(全部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 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

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

在上面的代码中,我看到_root已定义,但您在通话中使用root(上一行)。我认为这会导致无限循环。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

inorderPrint的调用树的深度与树本身的深度相同。看起来你不会试图保持树的平衡,所以深度可以达到树的大小。

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

相关问题