0

わかりましたので、値を正しく (すべて 7000) 読み取り、(ツリー構造として 15 個の値を手書き)、エラーを作成しない read メソッドがあります。

ただし、バイナリ ツリーの出力に関しては、いくつかの Web サイトで説明されている方法を使用しています。

私が得ているエラーはスタックオーバーフローであり、再帰呼び出しが原因であり、決して発生しないと想定していますが、なぜこれが機能しないのかわかりません。

どんな助けでも大歓迎です、ありがとう。

以下のコード:

// 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 にバグがあります。param _rootが意図した場所で使用されていません。代わりに常にルートでループします。

ヒント: 類似した名前は使用しないでください。

ヒント:ネストされたテンプレートで std:: を頻繁に記述しない限り、バグを避けるためにstd を使用しないでください。

ヒント: 名前の先頭または末尾に _ を使用しないでください。

ヒント: NULL と比較しないでください: if(n!=NULL) の代わりに if(n) を書きます。

ヒント: 必要のないブロックは入れ子にしないでください:

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深さは、ツリー自体の深さと同じです。木のバランスを保とうとしていないように見えるので、深さが木のサイズと同じくらい大きくなる可能性があります。

これを修正するにはいくつかの方法があります。ツリーのサイズに応じて深さが対数的に大きくなるように、ツリーが常にバランスを保つようにすることができます。または、ツリーをスレッド化して、ノードに繰り返しアクセスできるようにすることもできます。

于 2012-04-30T20:50:24.233 に答える