简体   繁体   English

二叉树顺序遍历导致堆栈溢出

[英]Binary Tree In Order Traversal causing Stack Overflow

Ok so, I have a read method that is reading the values in correctly (all 7000), (hand written 15 values as a tree structure), doesn't create any errors. 好的,我有一个读取方法,可以正确读取值(全部为7000)(以树形结构手写15个值),不会产生任何错误。

However, when it comes to the output of the binary tree I am using the method as stated on several websites. 但是,当涉及到二叉树的输出时,我正在使用一些网站上所述的方法。

The error I am getting is a stack overflow, and I am assuming its due to the recursive calls and never breaking out, But I have no idea why this isn't working. 我得到的错误是堆栈溢出,我认为这是由于递归调用而从未发生,但是我不知道为什么这不起作用。

Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

Code Listed Below: 下面列出的代码:

// 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
        }
    }
}

You have a bug in BinaryTreeStorage::inorderPrint, your param _root is not used where intended: You always loop on root instead. 您在BinaryTreeStorage :: inorderPrint中有一个错误,您的参数_root不在预期的位置使用:您始终在root上循环。

hint: Avoid using similar names! 提示:避免使用相似的名称!

hint: Avoid using std to avoid bugs, unless you write std:: too often in nested templates. 提示:除非您经常在嵌套模板中编写std ::,否则请避免使用std来避免错误。

hint: Do not use _ at the beginning or end of names. 提示:请勿在名称的开头或结尾使用_。

hint: Do not compare with NULL: Write if(n) instead of if(n!=NULL). 提示:不要与NULL比较:写if(n)而不是if(n!= NULL)。

hint: Do not nest blocks when not needed: 提示:不需要时不要嵌套块:

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);
}
void BinaryTreeStorage::inorderPrint(ofstream& _out, node *_root) const
{
        if (_root != NULL)
        {
                // Inorder
                inorderPrint(_out, root->left);

In the above code, I can see _root defined but you're using root in your call (last line above). 在上面的代码中,我可以看到_root定义,但是您在调用中使用了root (上面的最后一行)。 I think that is causing the infinite loop. 我认为这导致了无限循环。

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

The depth of the call tree of inorderPrint is the same as the depth of the tree itself. inorderPrint的调用树的深度与树本身的深度相同。 It looks like you don't try to keep the tree balanced, so the depth can get as large as the size of the tree. 似乎您没有尝试使树保持平衡,因此深度可以变得与树的大小一样大。

There are a few ways of fixing this. 有几种解决方法。 You can make sure that the tree always remains balanced so that the depth grows logarithmically with the size of the tree. 您可以确保树始终保持平衡,以便深度随树的大小成对数增长。 Or you can make the tree threaded , which lets you visit the nodes iteratively. 或者,您可以使树成为线程 ,从而使您可以迭代地访问节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM