さて、私は正しく(すべて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
}
}
}
ようこそスタックオーバーフローに!見知らぬ人にあなたのコードのエラーを見つけることは生産的ではありません。デバッガやprintステートメントを使用して問題を特定(または少なくとも分離)してから、さらに具体的な質問に戻ってください(10行[テストケース](http: /sscce.org))。最後の関数には –
が挿入されていますが、多くは意味がありません。私は、無限再帰の可能性をたくさん見ることができます。しかし、私はすべてのことをトレースしていない、それはデバッガのためのものです。また、string.empty()を使用してstd :: stringが空であるかどうかを確認する必要があります –