簡體   English   中英

二叉樹順序遍歷導致堆棧溢出

[英]Binary Tree In Order Traversal causing 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
        }
    }
}

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

在上面的代碼中,我可以看到_root定義,但是您在調用中使用了root (上面的最后一行)。 我認為這導致了無限循環。

構造樹節點時,是否確保將左右指針初始化為NULL?

inorderPrint的調用樹的深度與樹本身的深度相同。 似乎您沒有嘗試使樹保持平衡,因此深度可以變得與樹的大小一樣大。

有幾種解決方法。 您可以確保樹始終保持平衡,以便深度隨樹的大小成對數增長。 或者,您可以使樹成為線程 ,從而使您可以迭代地訪問節點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM